当前位置:   article > 正文

【运维知识大神篇】运维人必学的Docker教程1(Docker安装部署+Docker镜像管理+容器管理常用命令+搭建docker的本地yum源+windows系统安装docker环境)_docker运维

docker运维

本篇文章开始给大家介绍Docker,这个是我非常喜欢的一个服务,介绍给大家,希望大家也能喜欢!

目录

何为容器

Docker环境安装部署

一、yum安装(CentOS7.9)

二、rpm包安装(CentOS)

三、卸载docker环境

四、安装指定的docker版本

五、Ubuntu安装docker环境

六、使用deb安装docker

七、Ubuntu卸载docker环境

八、二进制安装docker在Linux系统(CentOS,Ubuntu)

1、部署流程

2、二进制部署脚本

Docker镜像管理基础

一、查看镜像

二、给镜像打标签

三、本地镜像迁移(导入导出)

四、下载镜像

五、删除镜像

六、自定义镜像

容器管理常用命令

一、查看容器

二、运行容器

三、连接到指定的容器

四、删除所有的容器

五、查看容器的IP地址,查看IPAddress字段即可

六、案例:使用docker部署ES节点

扩展

一、搭建私有的yum仓库,实现docker的私有仓库部署,要求可以指定版本

二、在windows上安装docker环境


何为容器

所谓的容器技术就是能够将计算机的程序及其所依赖的库进行打包的一种技术手段,方便进行传输,部署,运行;容器可以实现帮我们将程序和其所依赖的库文件进行打包。

Docker环境安装部署

一、yum安装(CentOS7.9)

1、卸载旧的docker版本

  1. yum remove docker \
  2.                   docker-client \
  3.                   docker-client-latest \
  4.                   docker-common \
  5.                   docker-latest \
  6.                   docker-latest-logrotate \
  7.                   docker-logrotate \
  8.                   docker-engine

2、配置docker软件源

[root@Docker01 ~]# curl -s -o /etc/yum.repos.d/docker-ce.repo https://download.docker.com/linux/centos/docker-ce.repo

3、安装docker

[root@Docker01 ~]# yum -y install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

4、启动docker服务

[root@Docker01 ~]# systemctl enable --now docker

5、查看docker服务

  1. [root@Docker01 ~]# docker version #在服务端查看详细信息
  2. Client: Docker Engine - Community
  3. Version: 24.0.2
  4. API version: 1.43
  5. Go version: go1.20.4
  6. Git commit: cb74dfc
  7. Built: Thu May 25 21:55:21 2023
  8. OS/Arch: linux/amd64
  9. Context: default
  10. Server: Docker Engine - Community
  11. Engine:
  12. Version: 24.0.2
  13. API version: 1.43 (minimum version 1.12)
  14. Go version: go1.20.4
  15. Git commit: 659604f
  16. Built: Thu May 25 21:54:24 2023
  17. OS/Arch: linux/amd64
  18. Experimental: false
  19. containerd:
  20. Version: 1.6.21
  21. GitCommit: 3dce8eb055cbb6872793272b4f20ed16117344f8
  22. runc:
  23. Version: 1.1.7
  24. GitCommit: v1.1.7-0-g860f061
  25. docker-init:
  26. Version: 0.19.0
  27. GitCommit: de40ad0
  28. [root@Docker01 ~]# docker -v #在本地查看是否有docker
  29. Docker version 24.0.2, build cb74dfc

6、验证docker是否安装成功

  1. [root@Docker01 ~]# docker run hello-world
  2. Unable to find image 'hello-world:latest' locally
  3. latest: Pulling from library/hello-world
  4. 719385e32844: Pull complete
  5. Digest: sha256:fc6cf906cbfa013e80938cdf0bb199fbdbb86d6e3e013783e5a766f50f5dbce0
  6. Status: Downloaded newer image for hello-world:latest
  7. Hello from Docker!
  8. This message shows that your installation appears to be working correctly.
  9. To generate this message, Docker took the following steps:
  10. 1. The Docker client contacted the Docker daemon.
  11. 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
  12. (amd64)
  13. 3. The Docker daemon created a new container from that image which runs the
  14. executable that produces the output you are currently reading.
  15. 4. The Docker daemon streamed that output to the Docker client, which sent it
  16. to your terminal.
  17. To try something more ambitious, you can run an Ubuntu container with:
  18. $ docker run -it ubuntu bash
  19. Share images, automate workflows, and more with a free Docker ID:
  20. https://hub.docker.com/
  21. For more examples and ideas, visit:
  22. https://docs.docker.com/get-started/

7、新手必备自动补全功能

docker --help可以查看使用方式,安装自动补全后按tab即可

  1. [root@Docker01 ~]# yum -y install bash-completion
  2. [root@Docker01 ~]# source /usr/share/bash-completion/bash_completion
  3. [root@Docker01 ~]# docker #按tab键即显示
  4. attach help pause stack
  5. build history plugin start
  6. builder image port stats
  7. buildx images ps stop
  8. commit import pull swarm
  9. compose info push system
  10. config inspect rename tag
  11. container kill restart top
  12. context load rm trust
  13. cp login rmi unpause
  14. create logout run update
  15. diff logs save version
  16. events manifest search volume
  17. exec network secret wait
  18. export node service

8、配置镜像加速

参考链接:https://cr.console.aliyun.com/cn-hangzhou/instances/mirrors

  1. [root@Docker01 ~]# mkdir -p /etc/docker
  2. tee /etc/docker/daemon.json <<-'EOF'
  3. {
  4. "registry-mirrors": ["https://xg3udbel.mirror.aliyuncs.com"]
  5. }
  6. EOF
  7. [root@Docker01 ~]# systemctl daemon-reload
  8. [root@Docker01 ~]# systemctl restart docker

9、查看镜像加速是否配置成功

  1. [root@Docker02 ~]# docker info | grep "Registry Mirrors" -A 1
  2. Registry Mirrors:
  3. https://xg3udbel.mirror.aliyuncs.com/

二、rpm包安装(CentOS)

1、下载相应的rpm包

rpm下载链接:https://download.docker.com/linux/centos/7/x86_64/stable/Packages/

可以利用它做本地yum仓库,我这边准备了24.0.0.2的rpm压缩包,下载链接放在文末

2、基于现成的rpm包进行安装

[root@Docker02 ~]# unzip koten-docker-24_0_2.zip && yum -y localinstall koten-docker-24_0_2/*.rpm

3、启动docker服务

[root@Docker02 ~]# systemctl enable --now docker

4、开启自动补全功能

安装的rpm包里有就不用下载了

[root@Docker02 ~]# source /usr/share/bash-completion/bash_completion

5、配置镜像加速

  1. [root@Docker02 ~]# mkdir -p /etc/docker
  2. [root@Docker02 ~]# tee /etc/docker/daemon.json <<-'EOF'
  3. {
  4. "registry-mirrors": ["https://xg3udbel.mirror.aliyuncs.com"]
  5. }
  6. EOF
  7. [root@Docker02 ~]# systemctl daemon-reload
  8. [root@Docker02 ~]# systemctl restart docker

6、查看镜像加速是否配置成功

  1. [root@Docker02 ~]# docker info | grep "Registry Mirrors" -A 1
  2. Registry Mirrors:
  3. https://xg3udbel.mirror.aliyuncs.com/

三、卸载docker环境

1、卸载软件包

[root@Docker01 ~]# yum -y remove docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin docker-ce-rootless-extras

2、删除数据目录

[root@Docker01 ~]# rm -rf /var/lib/docker /var/lib/containerd

四、安装指定的docker版本

1、添加docker-ce的软件源

[root@Docker01 ~]# curl -s -o /etc/yum.repos.d/docker-ce.repo https://download.docker.com/linux/centos/docker-ce.repo

2、查看软件源中的docker版本

[root@Docker01 ~]# yum list docker-ce --showduplicates | sort -r

3、安装指定的docker版本

[root@Docker01 ~]# yum -y install docker-ce-20.10.24 docker-ce-cli-20.10.24 containerd.io 

4、配置镜像加速(同上)

5、配置自动补全

  1. [root@Docker01 ~]# yum -y install bash-completion
  2. [root@Docker01 ~]# source /usr/share/bash-completion/bash_completion

6、启动docker服务

[root@Docker01 ~]# systemctl enable --now docker

7、查看docker版本

  1. [root@Docker01 ~]# docker version
  2. Client: Docker Engine - Community
  3. Version: 20.10.24
  4. API version: 1.41
  5. Go version: go1.19.7
  6. Git commit: 297e128
  7. Built: Tue Apr 4 18:22:57 2023
  8. OS/Arch: linux/amd64
  9. Context: default
  10. Experimental: true
  11. Server: Docker Engine - Community
  12. Engine:
  13. Version: 20.10.24
  14. API version: 1.41 (minimum version 1.12)
  15. Go version: go1.19.7
  16. Git commit: 5d6db84
  17. Built: Tue Apr 4 18:21:02 2023
  18. OS/Arch: linux/amd64
  19. Experimental: false
  20. containerd:
  21. Version: 1.6.21
  22. GitCommit: 3dce8eb055cbb6872793272b4f20ed16117344f8
  23. runc:
  24. Version: 1.1.7
  25. GitCommit: v1.1.7-0-g860f061
  26. docker-init:
  27. Version: 0.19.0
  28. GitCommit: de40ad0
  29. [root@Docker01 ~]# docker -v #等同于docker --version
  30. Docker version 20.10.24, build 297e128

五、Ubuntu安装docker环境

1、卸载旧的docker版本

root@docker03:~# for pkg in docker.io docker-doc docker-compose podman-docker containerd runc; do  apt-get remove $pkg; done

2、切换国内的软件源

  1. root@docker03:~# cat > /etc/apt/sources.list <<EOF
  2. deb https://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
  3. deb-src https://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
  4. deb https://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse
  5. deb-src https://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse
  6. deb https://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
  7. deb-src https://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
  8. # deb https://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse
  9. # deb-src https://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse
  10. deb https://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
  11. deb-src https://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
  12. EOF

3、更新软件源及证书

  1. root@docker03:~# apt-get update
  2. root@docker03:~# apt-get install ca-certificates curl gnupg

4、添加Docker的官方GPG密钥

  1. root@docker03:~# install -m 0755 -d /etc/apt/keyrings
  2. root@docker03:~# curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
  3. File '/etc/apt/keyrings/docker.gpg' exists. Overwrite? (y/N) y
  4. root@docker03:~# chmod a+r /etc/apt/keyrings/docker.gpg

5、配置Ubuntu的docker软件源

  1. root@docker03:~# cat > /etc/apt/sources.list.d/docker.list <<EOF
  2. deb [arch=amd64 signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu focal stable
  3. EOF

6、更新软件源

root@docker03:~# apt-get update

7、安装软件源

root@docker03:~# apt-get -y install docker-ce docker-ce-cli containerd.io

8、配置镜像加速(同上)

  1. root@docker03:~# mkdir -p /etc/docker
  2. root@docker03:~# tee /etc/docker/daemon.json <<-'EOF'
  3. {
  4. "registry-mirrors": ["https://xg3udbel.mirror.aliyuncs.com"]
  5. }
  6. EOF
  7. root@docker03:~# systemctl daemon-reload
  8. root@docker03:~# systemctl restart docker

9、设置开机启动docker服务

root@docker03:~# systemctl enable --now docker

10、验证docker版本

  1. root@docker03:~# docker -v
  2. Docker version 24.0.2, build cb74dfc

六、使用deb安装docker

deb下载链接:https://download.docker.com/linux/ubuntu/dists/focal/pool/stable/amd64/

deb类似于CentOS的rpm包,不再演示。https://download.docker.com/linux/ubuntu/dists/focal/pool/stable/amd64/

七、Ubuntu卸载docker环境

1、卸载软件包

root@docker03:~# apt-get -y purge docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin docker-ce-rootless-extras

2、删除数据目录

root@docker03:~# rm -rf /var/lib/docker /var/lib/containerd

八、二进制安装docker在Linux系统(CentOS,Ubuntu)

1、部署流程

安装就下载解压安装包,卸载的话直接删除软件包即可

1、下载软件包

root@docker03:~# wget https://download.docker.com/linux/static/stable/x86_64/docker-20.10.24.tgz

2、解压软件包

  1. root@docker03:~# mkdir -pv /koten/softwares
  2. mkdir: created directory '/koten'
  3. mkdir: created directory '/koten/softwares'
  4. root@docker03:~# tar xf docker-20.10.24.tgz -C /koten/softwares/

3、创建符号链接到环境变量

  1. root@docker03:~# ln -sv /koten/softwares/docker/* /usr/bin/
  2. '/usr/bin/containerd' -> '/koten/softwares/docker/containerd'
  3. '/usr/bin/containerd-shim' -> '/koten/softwares/docker/containerd-shim'
  4. '/usr/bin/containerd-shim-runc-v2' -> '/koten/softwares/docker/containerd-shim-runc-v2'
  5. '/usr/bin/ctr' -> '/koten/softwares/docker/ctr'
  6. '/usr/bin/docker' -> '/koten/softwares/docker/docker'
  7. '/usr/bin/dockerd' -> '/koten/softwares/docker/dockerd'
  8. '/usr/bin/docker-init' -> '/koten/softwares/docker/docker-init'
  9. '/usr/bin/docker-proxy' -> '/koten/softwares/docker/docker-proxy'
  10. '/usr/bin/runc' -> '/koten/softwares/docker/runc'

4、启动docker服务

  1. root@docker03:~# nohup dockerd &>/koten/softwares/docker/docker.log &
  2. [1] 35332

5、测试docker是否正常

  1. root@docker03:~# docker run hello-world
  2. Unable to find image 'hello-world:latest' locally
  3. latest: Pulling from library/hello-world
  4. 2db29710123e: Pull complete
  5. Digest: sha256:2498fce14358aa50ead0cc6c19990fc6ff866ce72aeb5546e1d59caac3d0d60f
  6. Status: Downloaded newer image for hello-world:latest
  7. Hello from Docker!
  8. This message shows that your installation appears to be working correctly.
  9. To generate this message, Docker took the following steps:
  10. 1. The Docker client contacted the Docker daemon.
  11. 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
  12. (amd64)
  13. 3. The Docker daemon created a new container from that image which runs the
  14. executable that produces the output you are currently reading.
  15. 4. The Docker daemon streamed that output to the Docker client, which sent it
  16. to your terminal.
  17. To try something more ambitious, you can run an Ubuntu container with:
  18. $ docker run -it ubuntu bash
  19. Share images, automate workflows, and more with a free Docker ID:
  20. https://hub.docker.com/
  21. For more examples and ideas, visit:
  22. https://docs.docker.com/get-started/

2、二进制部署脚本

通过运行此脚本,可以实现自动部署docker,可以通过简单修改脚本实现自定义docker版本,注意在centos可以通过sh运行,但是ubuntu需要用bash或者路径运行

  1. [root@Docker00 ~]# mkdir download
  2. [root@Docker00 ~]# mkdir /koten/softwares
  3. [root@Docker01 download]# ls #准备自动补全、镜像加速、开机自启动脚本
  4. daemon.json docker docker.service
  5. [root@Docker00 ~]# cat install-docker.sh
  6. #!/bin/bash
  7. DOCKER_VERSION=19.03.15 #可以自定义docker版本
  8. FILENAME=docker-${DOCKER_VERSION}.tgz
  9. URL=https://download.docker.com/linux/static/stable/x86_64
  10. DOWNLOAD=./download
  11. BASE_DIR=/koten/softwares
  12. # 定义安装函数
  13. function InstallDocker(){
  14. # 判断文件是否存在,若不存在则下载软件包
  15. if [ ! -f ${DOWNLOAD}/${FILENAME} ]; then
  16. wget ${URL}/${FILENAME} -O ${DOWNLOAD}/${FILENAME}
  17. fi
  18. # 判断解压路径是否存在
  19. if [ ! -d ${BASE_DIR} ]; then
  20. install -d ${BASE_DIR}
  21. fi
  22. # 解压软件包到指定目录
  23. tar xf ${DOWNLOAD}/${FILENAME} -C ${BASE_DIR}
  24. # 创建软连接
  25. ln -svf ${BASE_DIR}/docker/* /usr/bin/
  26. # 自动补全功能
  27. cp $DOWNLOAD/docker /usr/share/bash-completion/completions/docker
  28. source /usr/share/bash-completion/completions/docker
  29. # 配置镜像加速
  30. install -d /etc/docker
  31. cp $DOWNLOAD/daemon.json /etc/docker/daemon.json
  32. # 开机自启动脚本
  33. cp download/docker.service /usr/lib/systemd/system/docker.service
  34. systemctl daemon-reload
  35. systemctl enable --now docker
  36. docker version
  37. tput setaf 3
  38. echo "安装成功!"
  39. tput setaf 2
  40. }
  41. # 卸载docker
  42. function UninstallDocker(){
  43. # 停止docker服务
  44. systemctl disable --now docker
  45. # 卸载启动脚本
  46. rm -f /usr/lib/systemd/system/docker.service
  47. # 清空程序目录
  48. rm -rf ${BASE_DIR}/docker
  49. # 清空数据目录
  50. rm -rf /var/lib/{docker,containerd}
  51. # 清除符号链接
  52. rm -f /usr/bin/{containerd,containerd-shim,containerd-shim-runc-v2,ctr,docker,dockerd,docker-init,docker-proxy,runc}
  53. # 使得终端变粉色
  54. tput setaf 5
  55. echo "卸载成功!"
  56. tput setaf 7
  57. }
  58. # 程序的入口函数
  59. function main(){
  60. # 判断传递的参数
  61. case $1 in
  62. "install")
  63. InstallDocker
  64. ;;
  65. "remove")
  66. UninstallDocker
  67. ;;
  68. *)
  69. echo "Invalid parameter, Usage: $0 install|remove"
  70. ;;
  71. esac
  72. }
  73. # 向入口函数传参
  74. main $1
  75. [root@Docker00 ~]# sh deploy.sh install
  76. [root@Docker00 ~]# docker -v
  77. Docker version 19.03.15, build 99e3ed8

Docker镜像管理基础

一、查看镜像

查看现有的镜像列表

  1. root@docker03:~# docker image ls
  2. REPOSITORY TAG IMAGE ID CREATED SIZE
  3. hello-world latest feb5d9fea6a5 20 months ago 13.3kB
  4. root@docker03:~# docker images
  5. REPOSITORY TAG IMAGE ID CREATED SIZE
  6. hello-world latest feb5d9fea6a5 20 months ago 13.3kB

只查看镜像的ID

  1. root@docker03:~# docker image ls -q
  2. feb5d9fea6a5

二、给镜像打标签

  1. root@docker03:~# docker image ls
  2. REPOSITORY TAG IMAGE ID CREATED SIZE
  3. hello-world latest feb5d9fea6a5 20 months ago 13.3kB
  4. root@docker03:~# docker image tag hello-world koten-hello-world:v1.1
  5. root@docker03:~# docker image ls
  6. REPOSITORY TAG IMAGE ID CREATED SIZE
  7. hello-world latest feb5d9fea6a5 20 months ago 13.3kB
  8. koten-hello-world v1.1 feb5d9fea6a5 20 months ago 13.3kB

三、本地镜像迁移(导入导出)

导出镜像到一个tar包的两种方式

  1. root@docker03:~# docker image save koten-hello-world:v1.1 > docker-koten-hello-world.tar.gz
  2. root@docker03:~# docker image save --output docker-koten-hello-world2.tar.gz koten-hello-world:v1.1
  3. root@docker03:~# ls
  4. docker-koten-hello-world2.tar.gz
  5. docker-koten-hello-world.tar.gz

从一个tar包导入镜像的两种方式

  1. root@docker03:~# scp *.tar.gz 10.0.0.201:/root/
  2. [root@Docker01 ~]# docker image load < docker-koten-hello-world.tar.gz
  3. e07ee1baac5f: Loading layer 14.85kB/14.85kB
  4. Loaded image: koten-hello-world:v1.1
  5. [root@Docker01 ~]# docker image load -i docker-koten-hello-world2.tar.gz
  6. Loaded image: koten-hello-world:v1.1
  7. [root@Docker01 ~]# docker image ls
  8. REPOSITORY TAG IMAGE ID CREATED SIZE
  9. koten-hello-world v1.1 feb5d9fea6a5 20 months ago 13.3kB

镜像批量导入

for i in `ls *.tar.gz`; do docker load -i $i ;done

镜像批量导出

docker image save -o xxx.tar.gz  `docker images|awk 'NR>1{print $1":"$2}'`

忘记NR可以大于1了,老想着awk不能变量,还用了for循环,唉,三剑客还得练!!!

四、下载镜像

下载一个nginx版本为1.14.2的镜像

  1. root@docker03:~# docker pull nginx:1.14.2
  2. 1.14.2: Pulling from library/nginx
  3. 27833a3ba0a5: Pull complete
  4. 0f23e58bd0b7: Pull complete
  5. 8ca774778e85: Pull complete
  6. Digest: sha256:f7988fb6c02e0ce69257d9bd9cf37ae20a60f1df7563c3a2a6abe24160306b8d
  7. Status: Downloaded newer image for nginx:1.14.2
  8. docker.io/library/nginx:1.14.2

下载一个mysql版本号为8.0的镜像

root@docker03:~# docker pull mysql:8.0 

下载一个tomcat9的镜像

root@docker03:~# docker pull tomcat:9

也可以去第三方镜像仓库下载镜像

root@docker03:~# docker pull docker.elastic.co/elasticsearch/elasticsearch:7.17.5

五、删除镜像

删除指定的镜像的名称

  1. root@docker03:~# docker image rm hello-world
  2. Untagged: hello-world:latest
  3. Untagged: hello-world@sha256:2498fce14358aa50ead0cc6c19990fc6ff866ce72aeb5546e1d59caac3d0d60f

基于镜像ID强制删除镜像,尽管该镜像被其他镜像或容器引用

  1. root@docker03:~# docker image rm -f feb5d9fea6a5
  2. Untagged: koten-hello-world:v1.1
  3. Deleted: sha256:feb5d9fea6a5e9606aa995e879d862b825965ba48de054caab5ef356dc6b3412

删除本地的所有镜像

  1. root@docker03:~# docker image rm -f `docker images -q`
  2. Untagged: nginx:1.14.2
  3. Untagged: nginx@sha256:f7988fb6c02e0ce69257d9bd9cf37ae20a60f1df7563c3a2a6abe24160306b8d
  4. Deleted: sha256:295c7be079025306c4f1d65997fcf7adb411c88f139ad1d34b537164aa060369
  5. Deleted: sha256:19606512dfe192788a55d7c1efb9ec02041b4e318587632f755c5112f927e0e3
  6. Deleted: sha256:0b83495b3ad3db8663870c3babeba503a35740537a5e25acdf61ce6a8bdad06f
  7. Deleted: sha256:5dacd731af1b0386ead06c8b1feff9f65d9e0bdfec032d2cd0bc03690698feda
  8. root@docker03:~# docker images
  9. REPOSITORY TAG IMAGE ID CREATED SIZE

可以把删除本地的所有镜像这种常用的命令写入Xshell会话的快速命令

如果勾选追加CR,点击后就会输入命令会自动回车,如果不勾选就只会输入命令停留,所以新建危险的快速命令不推荐勾选 

 

添加后快速命令栏会显示,使用时记得命令行中不要有其他命令,因为快速命令是在当前命令后面追加并不是替换

点击以下即可自动运行

六、自定义镜像

自己做个镜像出来,后面文章再写!!

容器管理常用命令

一、查看容器

查看现有的容器列表

  1. root@docker03:~# docker ps
  2. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

查看所有容器运行的所有状态

  1. root@docker03:~# docker ps -a
  2. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  3. a027a219bddc hello-world "/hello" 31 seconds ago Exited (0) 30 seconds ago pensive_villani
  4. 6351a82d0715 hello-world "/hello" 24 minutes ago Exited (0) 24 minutes ago wizardly_bardeen

查看最新的容器id

  1. root@docker03:~# docker ps -ql
  2. 6351a82d0715

查看最新的一个容器创建的信息,无论该容器处于什么状态均可查看

  1. root@docker03:~# docker ps -l
  2. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  3. a027a219bddc hello-world "/hello" 44 seconds ago Exited (0) 43 seconds ago pensive_villani

二、运行容器

将容器放在前台运行

  1. [root@Docker01 ~]# docker run nginx:1.14.2
  2. [root@Docker01 ~]# docker container inspect `docker container ps -lq` #查看容器IP地址为172.17.0.3
  3. [root@Docker01 ~]# curl 172.17.0.3
  4. <!DOCTYPE html>
  5. <html>
  6. <head>
  7. <title>Welcome to nginx!</title>
  8. <style>
  9. body {
  10. width: 35em;
  11. margin: 0 auto;
  12. font-family: Tahoma, Verdana, Arial, sans-serif;
  13. }
  14. </style>
  15. </head>
  16. <body>
  17. <h1>Welcome to nginx!</h1>
  18. <p>If you see this page, the nginx web server is successfully installed and
  19. working. Further configuration is required.</p>
  20. <p>For online documentation and support please refer to
  21. <a href="http://nginx.org/">nginx.org</a>.<br/>
  22. Commercial support is available at
  23. <a href="http://nginx.com/">nginx.com</a>.</p>
  24. <p><em>Thank you for using nginx.</em></p>
  25. </body>
  26. </html>
  27. [root@Docker01 ~]# docker run nginx:1.14.2
  28. 172.17.0.1 - - [06/Jun/2023:13:54:12 +0000] "GET / HTTP/1.1" 200 612 "-" "curl/7.29.0" "-"
  29. ......

将容器放在后台运行

[root@Docker01 ~]# docker run -d nginx:1.14.2

交互式运行

  1. #-i表示交互式,有了-i才不会闪退,-t表示分配终端,有了-t才是横着显示命令行
  2. [root@Docker01 ~]# docker container run -i -t centos:7
  3. Unable to find image 'centos:7' locally
  4. 7: Pulling from library/centos
  5. 2d473b07cdd5: Pull complete
  6. Digest: sha256:9d4bcbbb213dfd745b58be38b13b996ebb5ac315fe75711bd618426a630e0987
  7. Status: Downloaded newer image for centos:7
  8. [root@e7ea9bc74cdd /]# ls /
  9. anaconda-post.log etc lib64 opt run sys var
  10. bin home media proc sbin tmp
  11. dev lib mnt root srv usr
  12. [root@e7ea9bc74cdd /]# pwd
  13. /
  14. [root@e7ea9bc74cdd /]# exit
  15. exit
  16. [root@Docker01 ~]#

给容器起名字

  1. [root@Docker01 ~]# docker container run -itd --name linux centos:7
  2. 1c9b80bcb071c2ad342ac9c6744c6b03a688870fd0561608e4cd269fcfe8fa3c
  3. [root@Docker01 ~]# docker ps
  4. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  5. 1c9b80bcb071 centos:7 "/bin/bash" 36 seconds ago Up 35 seconds linux

-e表示向容器传递环境变量

  1. [root@Docker01 ~]# docker run -itd -e NAME=koten -e HOBBY=docker --name linux01 centos:7
  2. b04b31e444a4e14f05722a6151f0421fa2740f788435adea9adc05738c3f3ed
  3. [root@Docker01 ~]# docker ps
  4. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  5. b04b31e444a4 centos:7 "/bin/bash" 45 seconds ago Up 44 seconds linux01
  6. 1c9b80bcb071 centos:7 "/bin/bash" 2 minutes ago Up 2 minutes linux

 可以不进入容器,也可以在外面执行,类似于ssh执行命令,也类似于不进入mysql,在mysql外面执行命令

  1. [root@Docker01 ~]# docker run centos:7 env
  2. PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
  3. HOSTNAME=98c175050f7b
  4. HOME=/root

将nginx容器的80端口通过10.0.0.201网卡暴露在外面,底层是iptables做的地址映射

  1. [root@Docker01 ~]# docker run -d -p 10.0.0.201:80:80 nginx:1.14.2
  2. 2f6a69025cf3abe92232b4da3b893dd5b9e4a3f51ad9708be61cd8572fd6163b

三、连接到指定的容器

  1. [root@Docker01 ~]# docker container exec -it linux bash
  2. [root@1c9b80bcb071 /]# exit
  3. exit
  4. [root@Docker01 ~]#

四、删除所有的容器

与上面删除所有的镜像一样,也可以加入快速命令

docker container rm -f `docker container ps -qa`

五、查看容器的IP地址,查看IPAddress字段即可

  1. # docker container inspect 容器名称|容器的ID
  2. [root@Docker01 ~]# docker container inspect `docker container ps -lq`
  3. ......
  4. "NetworkSettings": {
  5. "Bridge": "",
  6. "SandboxID": "830197d273f10fced77e650b41b2159ec7bebcc49d2acaa8c885b48c30e76434",
  7. "HairpinMode": false,
  8. "LinkLocalIPv6Address": "",
  9. "LinkLocalIPv6PrefixLen": 0,
  10. "Ports": {
  11. "80/tcp": null
  12. },
  13. "SandboxKey": "/var/run/docker/netns/830197d273f1",
  14. "SecondaryIPAddresses": null,
  15. "SecondaryIPv6Addresses": null,
  16. "EndpointID": "a6b3bb8e725ed536a98b009e36a25a2718b17dc17d1882170cbceea56bcb373c",
  17. "Gateway": "172.17.0.1",
  18. "GlobalIPv6Address": "",
  19. "GlobalIPv6PrefixLen": 0,
  20. "IPAddress": "172.17.0.3",
  21. "IPPrefixLen": 16,
  22. "IPv6Gateway": "",
  23. "MacAddress": "02:42:ac:11:00:03",
  24. "Networks": {
  25. "bridge": {
  26. "IPAMConfig": null,
  27. "Links": null,
  28. "Aliases": null,
  29. "NetworkID": "b5012c44124488b1cb84aee6927b4a040e8a6db82b09096991518bec79c74cf9",
  30. "EndpointID": "a6b3bb8e725ed536a98b009e36a25a2718b17dc17d1882170cbceea56bcb373c",
  31. "Gateway": "172.17.0.1",
  32. "IPAddress": "172.17.0.3",
  33. "IPPrefixLen": 16,
  34. "IPv6Gateway": "",
  35. "GlobalIPv6Address": "",
  36. "GlobalIPv6PrefixLen": 0,
  37. "MacAddress": "02:42:ac:11:00:03",
  38. "DriverOpts": null
  39. }
  40. }
  41. }
  42. }
  43. ]

六、案例:使用docker部署ES节点

参考连接:https://www.elastic.co/guide/en/elasticsearch/reference/7.17/docker.html

下载镜像

docker pull docker.elastic.co/elascsearch/elasticsearch:7.17.5

启动ES服务

docker run -d --name koten-es7 -p 9200:9200 -e "discovery.type=single-node" docker.elastic.co/elasticsearch/elasticsearch:7.17.5

扩展

一、搭建私有的yum仓库,实现docker的私有仓库部署,要求可以指定版本

可以参考我之前写的文章:【运维知识进阶篇】一键部署yum本地仓库_一键安装yum-CSDN博客

真正的yum仓库有两点要求,一个是可以安装任意版本,一个是可以解决依赖问题。

我们可以通过运行一个脚本自动部署本地yum仓库,但是考虑到是部署docker的yum仓库,所以我们需要增加docker的rpm包在我们的仓库,其他的软件可以不需要。

1、配置虚拟机

2、下载docker的rpm包到/Packages目录,还有一些依赖包和docker补全的包

  1. [root@Docker00 ~]# mkdir /Packages/
  2. [root@Docker00 ~]# cd /Packages/
  3. [root@Docker00 Packages]# wget -r -l 1 -nd -nc -np -A rpm https://download.docker.com/linux/centos/7/x86_64/stable/Packages/ #下载该网址下的所有rpm包
  4. [root@Docker00 test]# ls /Packages/
  5. ......
  6. docker-scan-plugin-0.21.0-3.el7.x86_64.rpm
  7. docker-scan-plugin-0.23.0-1.el7.x86_64.rpm
  8. docker-scan-plugin-0.23.0-3.el7.x86_64.rpm
  9. docker-scan-plugin-0.7.0-3.el7.x86_64.rpm
  10. docker-scan-plugin-0.8.0-3.el7.x86_64.rpm
  11. docker-scan-plugin-0.9.0-3.el7.x86_64.rpm
  12. [root@Docker00 ~]# ls /Packages|wc -l
  13. 259

3、编写脚本并执行

  1. [root@Docker00 ~]# cat deploy_docker.sh
  2. #!/bin/bash
  3. # 安装epel源
  4. yum -y install epel-release
  5. # 安装nginx
  6. yum -y install nginx
  7. # 创建目录,准备Nginx默认站点
  8. mkdir /share
  9. chown -R nginx:nginx /share
  10. # 删除Nginx默认站点,并在其目录中创建yum.conf文件进行yum仓库目录索引
  11. rm /etc/nginx/conf.d/default.conf
  12. cat > /etc/nginx/conf.d/yum.conf <<EOF
  13. server {
  14. listen 80 default_server;
  15. root /share;
  16. index index.html index.htm;
  17. autoindex on;
  18. autoindex_exact_size on;
  19. autoindex_localtime on;
  20. }
  21. EOF
  22. # 准备yum仓库存储目录
  23. mkdir /share/packages
  24. chown -R nginx:nginx /share/packages
  25. # 安装createrepo
  26. yum -y install createrepo
  27. # 复制rpm包到本地yum仓库
  28. cp -a /Packages/* /share/packages/
  29. # 构建yum仓库
  30. createrepo /share/packages/
  31. # 启动nginx服务,并将服务加入开机启动项中
  32. systemctl start nginx
  33. systemctl enable nginx
  34. [root@Docker00 ~]# sh deploy_docker.sh
  35. ......
  36. Spawning worker 0 with 259 pkgs
  37. Workers Finished
  38. Saving Primary metadata
  39. Saving file lists metadata
  40. Saving other metadata
  41. Generating sqlite DBs
  42. Sqlite DBs complete

4、浏览器访问,可以看到我们的rpm包

5、客户端配置yum源,使用本地yum仓库测试安装指定版本

  1. [root@Docker02 ~]# cat /etc/yum.repos.d/docker_local.repo
  2. [local]
  3. name=docker_local
  4. baseurl=http://10.0.0.200/packages
  5. enabled=1
  6. [root@Docker04 ~]# yum -y install docker-ce-24.0.0 docker-ce-cli-24.0.0 containerd.io
  7. ......
  8. Dependencies Resolved
  9. =====================================================
  10. Package Arch Version Repository
  11. Size
  12. =====================================================
  13. Installing:
  14. containerd.io x86_64 1.6.21-3.1.el7 local 34 M
  15. docker-ce x86_64 3:24.0.0-1.el7 local 24 M
  16. docker-ce-cli x86_64 1:24.0.0-1.el7 local 13 M
  17. Installing for dependencies:
  18. docker-buildx-plugin
  19. x86_64 0.10.5-1.el7 local 12 M
  20. docker-ce-rootless-extras
  21. x86_64 24.0.2-1.el7 local 9.1 M
  22. docker-compose-plugin
  23. x86_64 2.18.1-1.el7 local 12 M
  24. Transaction Summary
  25. =====================================================
  26. ......
  27. Complete!

二、在windows上安装docker环境

其实windows没有办法用docker,能用是因为安装了Linux内核,注意安装环境和后续使用都需要开启Hyper-V,在控制面板,程序和功能,启用或关闭windows功能。开启后需要重启windows电脑

注意开启它可能会影响VMware的正常使用,所以用VM虚拟机的使用记得关闭它哈,docker在windows上用的少,可能打镜像包会用到,主要还是在linux上用,打镜像包可以把代码传到自己虚拟机上,在虚拟机上再拉下来,这样速度也很快

之前镜像加速器的网址有在windows安装docker的方法,可以作为参考

打开这个软件,修改安装路径,一路next即可 

  

也可以直接在docker官网找到docker的windows版本下载!

https://www.docker.com/


Docker-24.0.2-rpm包下载链接:https://pan.baidu.com/s/1qAzlLiSQW59Hhsq5GUaW-A?pwd=2uox 

我是koten,10年运维经验,持续分享运维干货,感谢大家的阅读和关注!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/655616
推荐阅读
相关标签
  

闽ICP备14008679号