Docker安装

环境需求

Linux,且要求内核3.0以上。

  1. # 查看内核版本
  2. [root@VM-8-9-centos ~]# uname -a
  3. Linux VM-8-9-centos 4.18.0-305.3.1.el8.x86_64 #1 SMP Tue Jun 1 16:14:33 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux
  4. # 查看发行版(本文档采用centos8)
  5. [root@VM-8-9-centos ~]# cat /etc/os-release
  6. NAME="CentOS Linux"
  7. VERSION="8 (Core)"
  8. ID="centos"
  9. ID_LIKE="rhel fedora"
  10. VERSION_ID="8"
  11. PLATFORM_ID="platform:el8"
  12. PRETTY_NAME="CentOS Linux 8 (Core)"
  13. ANSI_COLOR="0;31"
  14. CPE_NAME="cpe:/o:centos:centos:8"
  15. HOME_URL="https://www.centos.org/"
  16. BUG_REPORT_URL="https://bugs.centos.org/"
  17. CENTOS_MANTISBT_PROJECT="CentOS-8"
  18. CENTOS_MANTISBT_PROJECT_VERSION="8"
  19. REDHAT_SUPPORT_PRODUCT="centos"
  20. REDHAT_SUPPORT_PRODUCT_VERSION="8"

安装

详细安装参考文档,下面只记录命令。https://docs.docker.com/engine/install/centos/

  1. # 卸载已存在的docker
  2. sudo yum remove docker \
  3. docker-client \
  4. docker-client-latest \
  5. docker-common \
  6. docker-latest \
  7. docker-latest-logrotate \
  8. docker-logrotate \
  9. docker-engine
  10. # 安装yum工具
  11. yum install -y yum-utils
  12. # 配置docker repo的国内地址
  13. yum-config-manager \
  14. --add-repo \
  15. https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
  16. # 刷新yum索引
  17. yum makecache fast
  18. # 安装docker相关的 docker-ce 社区版(ee是企业版,要收费)
  19. yum install docker-ce docker-ce-cli containerd.io
  20. # 启动docker
  21. systemctl start docker
  22. # 使用docker version查看是否按照成功
  23. [root@VM-8-9-centos ~]# docker version
  24. Client: Docker Engine - Community
  25. Version: 20.10.9
  26. API version: 1.41
  27. Go version: go1.16.8
  28. Git commit: c2ea9bc
  29. Built: Mon Oct 4 16:08:25 2021
  30. OS/Arch: linux/amd64
  31. Context: default
  32. Experimental: true
  33. Server: Docker Engine - Community
  34. Engine:
  35. Version: 20.10.9
  36. API version: 1.41 (minimum version 1.12)
  37. Go version: go1.16.8
  38. Git commit: 79ea9d3
  39. Built: Mon Oct 4 16:06:48 2021
  40. OS/Arch: linux/amd64
  41. Experimental: false
  42. containerd:
  43. Version: 1.4.11
  44. GitCommit: 5b46e404f6b9f661a205e28d59c982d3634148f8
  45. runc:
  46. Version: 1.0.2
  47. GitCommit: v1.0.2-0-g52b36a2
  48. docker-init:
  49. Version: 0.19.0
  50. GitCommit: de40ad0

卸载Docker

  1. # 卸载依赖
  2. yum remove docker-ce docker-ce-cli containerd.io
  3. # 删除资源,/var/lib/docker 是docker的默认工作路径!
  4. rm -rf /var/lib/docker

配置Docker仓库加速地址

由于我用的是腾讯云轻量应用服务器,所以就使用腾讯云的加速,使用阿里云的可以去搜一下。

  1. # 创建一个文件
  2. vim /etc/docker/daemon.json
  3. # 输入加速地址
  4. {
  5. "registry-mirrors": [
  6. "https://mirror.ccs.tencentyun.com"
  7. ]
  8. }
  9. # 重启Docker
  10. sudo systemctl restart docker

Docker工作流程

docker架构图

image.png

Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure so you can deliver software quickly. With Docker, you can manage your infrastructure in the same ways you manage your applications. By taking advantage of Docker’s methodologies for shipping, testing, and deploying code quickly, you can significantly reduce the delay between writing code and running it in production.

Docker是一个CS结构的应用管理平台,开发者通过Docker客户端连接上Docker服务器后,就可以在不同的基础环境中运行应用,因为Docker对基础环境提供了抽象。
从图中可以看出,如果想使用Docker运行一个容器。需要先从docker hub上拉取一个镜像,然后启动这个镜像就可以让其作为一个容器运行起来。

Docker命令

帮助命令

docker基本信息

  1. [root@VM-8-9-centos ~]# docker info
  2. Client:
  3. Context: default
  4. Debug Mode: false
  5. Plugins:
  6. app: Docker App (Docker Inc., v0.9.1-beta3)
  7. buildx: Build with BuildKit (Docker Inc., v0.6.3-docker)
  8. scan: Docker Scan (Docker Inc., v0.8.0)
  9. Server:
  10. Containers: 0
  11. Running: 0
  12. Paused: 0
  13. Stopped: 0
  14. Images: 3
  15. Server Version: 20.10.9
  16. Storage Driver: overlay2
  17. Backing Filesystem: extfs
  18. Supports d_type: true
  19. Native Overlay Diff: true
  20. userxattr: false
  21. Logging Driver: json-file
  22. Cgroup Driver: cgroupfs
  23. Cgroup Version: 1
  24. Plugins:
  25. Volume: local
  26. Network: bridge host ipvlan macvlan null overlay
  27. Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
  28. Swarm: inactive
  29. Runtimes: io.containerd.runc.v2 io.containerd.runtime.v1.linux runc
  30. Default Runtime: runc
  31. Init Binary: docker-init
  32. containerd version: 5b46e404f6b9f661a205e28d59c982d3634148f8
  33. runc version: v1.0.2-0-g52b36a2
  34. init version: de40ad0
  35. Security Options:
  36. seccomp
  37. Profile: default
  38. Kernel Version: 4.18.0-305.3.1.el8.x86_64
  39. Operating System: CentOS Linux 8 (Core)
  40. OSType: linux
  41. Architecture: x86_64
  42. CPUs: 2
  43. Total Memory: 3.649GiB
  44. Name: VM-8-9-centos
  45. ID: RWPV:56TQ:L2XR:PRWM:UYGT:A2MU:PJXQ:BUFW:7TUK:AEH2:XOA7:BTAT
  46. Docker Root Dir: /var/lib/docker
  47. Debug Mode: false
  48. Registry: https://index.docker.io/v1/
  49. Labels:
  50. Experimental: false
  51. Insecure Registries:
  52. 127.0.0.0/8
  53. Registry Mirrors:
  54. https://mirror.ccs.tencentyun.com/
  55. Live Restore Enabled: false

从上面输出的信息中可以看出来Docker的基本信息,比如docker的版本,当前docker拥有几个镜像,Linux的版本,机子的信息,镜像仓库的地址。

docker帮助命令

  1. # 查看docker的命令
  2. docker help
  3. # 查看命令的帮助文档
  4. docker 命令 --help

镜像命令

搜索镜像

  1. [root@VM-8-9-centos ~]# docker search mysql
  2. NAME DESCRIPTION STARS OFFICIAL AUTOMATED
  3. mysql MySQL is a widely used, open-source relation 11521 [OK]
  4. mariadb MariaDB Server is a high performing open sou 4373 [OK]
  5. mysql/mysql-server Optimized MySQL Server Docker images. Create 851 [OK]
  6. centos/mysql-57-centos7 MySQL 5.7 SQL database server 91
  7. mysql/mysql-cluster Experimental MySQL Cluster Docker images. Cr 88
  8. centurylink/mysql Image containing mysql. Optimized to be link 59 [OK]
  9. databack/mysql-backup Back up mysql databases to... anywhere! 51
  10. prom/mysqld-exporter 42 [OK]
  11. deitch/mysql-backup REPLACED! Please use http://hub.docker.com/r 41 [OK]
  12. tutum/mysql Base docker image to run a MySQL database se 35
  13. linuxserver/mysql A Mysql container, brought to you by LinuxSe 32
  14. schickling/mysql-backup-s3 Backup MySQL to S3 (supports periodic backup 31 [OK]
  15. mysql/mysql-router MySQL Router provides transparent routing be 23
  16. centos/mysql-56-centos7 MySQL 5.6 SQL database server 20
  17. arey/mysql-client Run a MySQL client from a docker container 18 [OK]
  18. fradelg/mysql-cron-backup MySQL/MariaDB database backup using cron tas 16 [OK]
  19. yloeffler/mysql-backup This image runs mysqldump to backup data usi 7 [OK]
  20. genschsa/mysql-employees MySQL Employee Sample Database 7 [OK]
  21. openshift/mysql-55-centos7 DEPRECATED: A Centos7 based MySQL v5.5 image 6
  22. idoall/mysql MySQL is a widely used, open-source relation 3 [OK]
  23. devilbox/mysql Retagged MySQL, MariaDB and PerconaDB offici 3
  24. jelastic/mysql An image of the MySQL database server mainta 2
  25. ansibleplaybookbundle/mysql-apb An APB which deploys RHSCL MySQL 2 [OK]
  26. widdpim/mysql-client Dockerized MySQL Client (5.7) including Curl 1 [OK]
  27. centos/mysql-80-centos7 MySQL 8.0 SQL database server 1

除了使用docker search搜索,我们还可以去docker hub上搜索,一般最常用的还是去docker hub上搜索。

下载镜像

  1. # 下载镜像是可以携带版本号,也可以不携带,如果不携带默认使用最新版,但是使用时一般都会携带版本号。
  2. # docker pull 镜像名:tag
  3. [root@VM-8-9-centos ~]# docker pull nginx:1.21.3
  4. 1.21.3: Pulling from library/nginx
  5. 07aded7c29c6: Already exists
  6. bbe0b7acc89c: Pull complete
  7. 44ac32b0bba8: Pull complete
  8. 91d6e3e593db: Pull complete
  9. 8700267f2376: Pull complete
  10. 4ce73aa6e9b0: Pull complete
  11. Digest: sha256:06e4235e95299b1d6d595c5ef4c41a9b12641f6683136c18394b858967cd1506
  12. Status: Downloaded newer image for nginx:1.21.3
  13. docker.io/library/nginx:1.21.3

如果不知道镜像的名字或版本号就可以去docker hub上搜索。
image.png

查看本地镜像

  1. [root@VM-8-9-centos ~]# docker images
  2. REPOSITORY TAG IMAGE ID CREATED SIZE
  3. nginx 1.21.3 f8f4ffc8092c 13 days ago 133MB
  4. mysql latest 2fe463762680 13 days ago 514MB
  5. hello-world latest feb5d9fea6a5 2 weeks ago 13.3kB
  6. centos latest 5d0da3dc9764 3 weeks ago 231MB

解释

  1. REPOSITORY 镜像名
  2. TAG 标签
  3. IMAGE ID 镜像ID
  4. CREATED 创建时间
  5. SIZE 镜像大小

可选项

  1. # -a Show all images (default hides intermediate images)
  2. # -q Only show image IDs
  3. [root@VM-8-9-centos ~]# docker images -a
  4. REPOSITORY TAG IMAGE ID CREATED SIZE
  5. nginx 1.21.3 f8f4ffc8092c 13 days ago 133MB
  6. mysql latest 2fe463762680 13 days ago 514MB
  7. hello-world latest feb5d9fea6a5 2 weeks ago 13.3kB
  8. centos latest 5d0da3dc9764 3 weeks ago 231MB
  9. [root@VM-8-9-centos ~]# docker images -q
  10. f8f4ffc8092c
  11. 2fe463762680
  12. feb5d9fea6a5
  13. 5d0da3dc9764

参数

  1. # docker images 镜像名
  2. [root@VM-8-9-centos ~]# docker images nginx
  3. REPOSITORY TAG IMAGE ID CREATED SIZE
  4. nginx 1.21.3 f8f4ffc8092c 13 days ago 133MB

删除镜像

  1. # docker rmi 镜像名/镜像ID
  2. [root@VM-8-9-centos ~]# docker rmi hello-world
  3. Untagged: hello-world:latest
  4. Untagged: hello-world@sha256:9ade9cc2e26189a19c2e8854b9c8f1e14829b51c55a630ee675a5a9540ef6ccf
  5. Deleted: sha256:feb5d9fea6a5e9606aa995e879d862b825965ba48de054caab5ef356dc6b3412
  6. Deleted: sha256:e07ee1baac5fae6a26f30cabfe54a36d3402f96afda318fe0a96cec4ca393359
  7. [root@VM-8-9-centos ~]# docker images -a
  8. REPOSITORY TAG IMAGE ID CREATED SIZE
  9. nginx 1.21.3 f8f4ffc8092c 13 days ago 133MB
  10. mysql latest 2fe463762680 13 days ago 514MB
  11. centos latest 5d0da3dc9764 3 weeks ago 231MB

删除全部镜像:

  1. docker rmi -f $(docker images -aq)
  2. docker images -q | xargs docker rmi

    容器命令

    运行容器

    1. # 基础用法:docker run -d -p 宿主机端口:容器端口 --name="容器名" 镜像名:版本
    2. # docker run -d -p 8080:80 --name="mynginx" nginx:1.21.3
    镜像类比为一个安装包,是一个静态的概念,容器类比为一个运行的应用,是一个动态的概念。每个容器都有一个唯一的容器名,可以通过—name指定容器名,如果不指定,会使用随机的名称。
    当我们运行容器之后,容器是一个单独的环境,理解为Linux中又启动了一个Linux。所以容器开启的端口不是宿主机的端口,上面的命令表示,容器本身开启了一个80端口,宿主机开启了一个8080端口映射到容器,所以如果需要从客户端访问到容器内的服务,需要使用8080端口。不能使用80端口。
    默认容器的启动是以前台方式进行的,-d可选项表示容器以后台方式运行。 ```bash [root@VM-8-9-centos ~]# docker run -d -p 8080:80 —name=”mynginx” nginx:1.21.3 d0de55896281ead7c0cab5596ff884d0bd849965147a4d02cbc6cb013692c459 [root@VM-8-9-centos ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES d0de55896281 nginx:1.21.3 “/docker-entrypoint.…” 2 seconds ago Up 2 seconds 0.0.0.0:8080->80/tcp, :::8080->80/tcp mynginx [root@VM-8-9-centos ~]# curl localhost:8080 <!DOCTYPE html>

    Welcome to nginx!

    If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.

Thank you for using nginx.

[root@VM-8-9-centos ~]# curl localhost:80 curl: (7) Failed to connect to localhost port 80: Connection refused [root@VM-8-9-centos ~]# <a name="i3ytJ"></a> ### 进入容器 上面讲到,容器本身就是一个linux,所以我们自然可以登录到容器中。bash # docker exec -i -t 容器名/容器ID shell # docker exec 是在容器中运行命令,通过我们会通过这个命令开一个shell终端,再在shell终端中执行业务命令 # -i : Keep STDIN open even if not attached # -t : Allocate a pseudo-TTY,分配一个伪终端。为啥是”伪”呢,暂不明白。 docker exec -i -t mynginx /bin/bash [root@VM-8-9-centos ~]# docker exec -i -t mynginx /bin/bash root@d0de55896281:/# ll bash: ll: command not found root@d0de55896281:/# ls bin boot dev docker-entrypoint.d docker-entrypoint.sh etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var root@d0de55896281:/# exit; exit [root@VM-8-9-centos ~]# docker exec -i -t mynginx /bin/bash root@d0de55896281:/# ls -a . .dockerenv boot docker-entrypoint.d etc lib media opt root sbin sys usr .. bin dev docker-entrypoint.sh home lib64 mnt proc run srv tmp var root@d0de55896281:/# curl localhost:80 <!DOCTYPE html>

Welcome to nginx!

If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.

Thank you for using nginx.

root@d0de55896281:/# exit exit 进入到正在运行的容器之后,我们就可以执行我们想要的指令。因为它本身就可以理解为一个Linux系统,但是这个Linux系统通常很简单,很多指令都不支持。<br />退出容器使用exit。<br />如果想运行容器时立刻打开一个此容器的shell终端,可以在docker run上增加可选项-it。bash [root@VM-8-9-centos ~]# docker run -it nginx:1.21.3 /bin/bash root@1e9fdc40a104:/# ls -a . .dockerenv boot docker-entrypoint.d etc lib media opt root sbin sys usr .. bin dev docker-entrypoint.sh home lib64 mnt proc run srv tmp var root@1e9fdc40a104:/# curl localhost curl: (7) Failed to connect to localhost port 80: Connection refused root@1e9fdc40a104:/# exit exit 但进入之后我们会发现nginx没有启动,这是因为docker run命令携带的参数/bin/bash覆盖了启动nginx的命令。前面提到过,docker容器可以理解为一个小型的linux,在制作docker镜像的时候,通过会写一些启动命令,这样在容器启动之后服务就会启动了。这些启动命令是可以被替换的,上面我们就是用/bin/bash替换了默认的命令,导致nginx无法启动。所以通过想进入容器,都会选择先启动容器,在docker exec执行/bin/bash开启一个能和docker交互的终端。 <a name="r30CB"></a> ### 查看容器bash 查看正在运行的容器 docker ps # 查看全部容器 -a docker pa -a [root@VM-8-9-centos ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1e9fdc40a104 nginx:1.21.3 “/docker-entrypoint.…” 37 minutes ago Exited (7) 36 minutes ago confident_hofstadter d0de55896281 nginx:1.21.3 “/docker-entrypoint.…” About an hour ago Up About an hour 0.0.0.0:8080->80/tcp, :::8080->80/tcp mynginx [root@VM-8-9-centos ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES d0de55896281 nginx:1.21.3 “/docker-entrypoint.…” About an hour ago Up About an hour 0.0.0.0:8080->80/tcp, :::8080->80/tcp mynginx 容器是有生命状态的,包含:Stop、Running和Pause三个状态。docker ps只能看见处于Running和Pause状态的容器,看不见处于Stop状态的容器。bash [root@VM-8-9-centos ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1fb0fd6f5272 nginx:1.21.3 “/docker-entrypoint.…” 2 minutes ago Up 2 minutes (Paused) 0.0.0.0:8082->80/tcp, :::8082->80/tcp mynginx3 d0de55896281 nginx:1.21.3 “/docker-entrypoint.…” 20 hours ago Up 20 hours 0.0.0.0:8080->80/tcp, :::8080->80/tcp mynginx [root@VM-8-9-centos ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1fb0fd6f5272 nginx:1.21.3 “/docker-entrypoint.…” 2 minutes ago Up 2 minutes (Paused) 0.0.0.0:8082->80/tcp, :::8082->80/tcp mynginx3 d6051e48450c nginx:1.21.3 “/docker-entrypoint.…” 3 minutes ago Exited (0) 43 seconds ago mynginx2 d0de55896281 nginx:1.21.3 “/docker-entrypoint.…” 20 hours ago Up 20 hours 0.0.0.0:8080->80/tcp, :::8080->80/tcp mynginx <a name="COJ6J"></a> ### 停止容器 & 暂停容器 ![image.png](https://cdn.nlark.com/yuque/0/2021/png/726149/1634121173289-552b8cd1-7f59-47f2-b599-e92d515f0826.png#clientId=u96bbaf5a-f6dd-4&from=paste&height=511&id=u4595e824&margin=%5Bobject%20Object%5D&name=image.png&originHeight=1022&originWidth=1444&originalType=binary&ratio=1&size=98670&status=done&style=none&taskId=ueffefd0a-317c-4004-a8ec-765ba8093fb&width=722)<br />暂停容器、启动容器、停止容器、重启容器的命令都很简单:docker pause、docker start、docker stop和docker restart。<br />Pause状态和Stop状态类似于电脑关机和电脑休眠,Pause时不消耗CPU,但是依然占据内存,Stop时CPU和内存都不再占据。所以对于被pause的容器,unpause之后,容器内部服务的Context不会改变。但是对于start或restart的容器,容器内部服务的Context会改变。<br />我们请求一下容器内部的服务看看。bash [root@VM-8-9-centos ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b38784365688 nginx:1.21.3 “/docker-entrypoint.…” 13 seconds ago Up 13 seconds 0.0.0.0:8080->80/tcp, :::8080->80/tcp mynginx1 1fb0fd6f5272 nginx:1.21.3 “/docker-entrypoint.…” 31 minutes ago Up 11 minutes (Paused) 0.0.0.0:8082->80/tcp, :::8082->80/tcp mynginx3 d6051e48450c nginx:1.21.3 “/docker-entrypoint.…” 31 minutes ago Exited (0) 29 minutes ago mynginx2 # 处于running状态的容器正常返回 [root@VM-8-9-centos ~]# curl localhost:8080 <!DOCTYPE html>

Welcome to nginx!

If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.

Thank you for using nginx.

处于Stop状态的容器拒绝服务

[root@VM-8-9-centos ~]# curl localhost:8081 curl: (7) Failed to connect to localhost port 8081: Connection refused

处于Pause状态的服务可以接受请求但是不返回结果,因为它申请不到CPU资源。

[root@VM-8-9-centos ~]# curl localhost:8082 ^C

  1. <a name="sC1mj"></a>
  2. ### 输出容器状态
  3. **查看容器内部进程**
  4. ```bash
  5. # docker top 容器ID
  6. [root@VM-8-9-centos ~]# docker top 1f
  7. UID PID PPID C STIME TTY TIME CMD
  8. root 3731429 3731409 0 18:33 ? 00:00:00 nginx: master process nginx -g daemon off;
  9. 101 3731473 3731429 0 18:33 ? 00:00:00 nginx: worker process
  10. 101 3731474 3731429 0 18:33 ? 00:00:00 nginx: worker process

从docker top的输出中看出一些容器的进程信息,比如进程的个数,进程的PID以及PPID,进程的创建时间以及创建命令。从上图可以看出nginx创建了一个主进程(用于接收请求),两个工作进程(用于处理请求)。
查看容器状态

  1. # docker stats 容器ID
  2. CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O BLOCK I/O PIDS
  3. b38784365688 mynginx1 0.00% 2.676MiB / 3.649GiB 0.07% 2.53kB / 2.55kB 0B / 8.19kB 3

docker stats可以查看容器的资源使用情况,并且可以动态刷新,类似于Linux的top命令。

删除容器

删除容器使用docker rm命令。与删除镜像容器类似。

  1. [root@VM-8-9-centos ~]# docker ps -a
  2. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  3. b38784365688 nginx:1.21.3 "/docker-entrypoint.…" 51 minutes ago Up 51 minutes 0.0.0.0:8080->80/tcp, :::8080->80/tcp mynginx1
  4. 1fb0fd6f5272 nginx:1.21.3 "/docker-entrypoint.…" About an hour ago Up About an hour (Paused) 0.0.0.0:8082->80/tcp, :::8082->80/tcp mynginx3
  5. d6051e48450c nginx:1.21.3 "/docker-entrypoint.…" About an hour ago Exited (0) About an hour ago mynginx2
  6. [root@VM-8-9-centos ~]# docker rm 1f
  7. Error response from daemon: You cannot remove a paused container 1fb0fd6f5272ef99249ce6effccf5be5a50ddcb887c388a045e4e812b015bf06. Unpause and then stop the container before attempting removal or force remove
  8. [root@VM-8-9-centos ~]# docker stop 1f
  9. d1f
  10. [root@VM-8-9-centos ~]# docker rm 1f
  11. 1f
  12. [root@VM-8-9-centos ~]# docker ps -a
  13. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  14. b38784365688 nginx:1.21.3 "/docker-entrypoint.…" 52 minutes ago Up 52 minutes 0.0.0.0:8080->80/tcp, :::8080->80/tcp mynginx1
  15. d6051e48450c nginx:1.21.3 "/docker-entrypoint.…" About an hour ago Exited (0) About an hour ago mynginx2

需要注意的是,当容器处于Running或Pause状态时,是不能删除容器的,需要先停止再删除或者加上可选项-f来强制删除。
删除全部容器:

  1. rm -f docker rm -f $(dockerps -aq)
  2. docker ps -a -q | xargs docker rm -f

容器是依托镜像存在的,所以当一个镜像被用于生成容器之后,这个镜像是不能删除的,得等到依托它创建的容器都被删除之后才能删除或者在docker rmi命令上增加-f可选项来强制删除。

后台运行

之前知道,使用docker run -d是后台运行。但是需要注意一点,如果你启动的镜像里没有一个守护进程(能持续对外提供服务),此镜像是会自动stop的。

  1. [root@VM-8-9-centos ~]# docker images -a
  2. REPOSITORY TAG IMAGE ID CREATED SIZE
  3. nginx 1.21.3 87a94228f133 37 hours ago 133MB
  4. centos centos8.4.2105 5d0da3dc9764 3 weeks ago 231MB
  5. [root@VM-8-9-centos ~]# docker run -d 5d
  6. 9b3783f131d33926ca0b1096951490f078c6427f709c092a19cebcffd01f14c8
  7. [root@VM-8-9-centos ~]# docker run -d 87
  8. 376f0e7db9c6dc7d22b92db3b1120835cd1c0b24fa9e1ba42a0c30e407e2719e
  9. [root@VM-8-9-centos ~]# docker ps -a
  10. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  11. 376f0e7db9c6 87 "/docker-entrypoint.…" 4 seconds ago Up 3 seconds 80/tcp amazing_yalow
  12. 9b3783f131d3 5d "/bin/bash" 14 seconds ago Exited (0) 13 seconds ago eloquent_feynman

上图我们启动了一个centos8和一个nginx,由于centos8里没有守护进程,所以它自动stop了。而nginx有处于用户态的进程,所以,处于Running的状态。
执行如下命令:

  1. docker run -d centos /bin/sh -c "for i in {1..50}; do echo $i; sleep 1; done"

启动之后,我们使用watch -n 1 ‘docker ps -a’。
就可以发现,在前50秒,此容器是处于Up状态的,50秒后容器就Exit了。
image.png
image.png
怎么解决这个问题呢?很简单,以交互模式开启一个shell进程就可以了。

  1. [root@VM-8-9-centos ~]# docker run -d -it 5d /bin/bash
  2. f976950a42b4e2fb699cde3076146e77dd2f68e0d10c486ae38722a2aedde084
  3. [root@VM-8-9-centos ~]# docker ps -a
  4. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  5. f976950a42b4 5d "/bin/bash" 4 seconds ago Up 3 seconds silly_nobel
  6. a025d52202d7 5d "/bin/bash" 23 seconds ago Exited (0) 22 seconds ago reverent_snyder
  7. 376f0e7db9c6 87 "/docker-entrypoint.…" 11 minutes ago Up 11 minutes 80/tcp amazing_yalow
  8. 9b3783f131d3 5d "/bin/bash" 11 minutes ago Exited (0) 11 minutes ago eloquent_feynman

此时就可以看见,f9这个容器是正常启动的。所以我的理解就是如果既没有用户态进程,又没有可交互的终端,docker会认为这个容器没有价值,就给它停了。
我们再看一个细节,就是docker ps的输出中COMMAND这一列。

  1. [root@VM-8-9-centos ~]# docker ps -a
  2. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  3. 89ad4394806c hello-world "/hello" 4 seconds ago Exited (0) 3 seconds ago loving_morse
  4. cc357ba877a3 5d "/bin/bash" 20 minutes ago Up 20 minutes eloquent_saha
  5. f976950a42b4 5d "/bin/bash" 35 minutes ago Up 35 minutes silly_nobel
  6. a025d52202d7 5d "/bin/bash" 35 minutes ago Exited (0) 35 minutes ago reverent_snyder
  7. 376f0e7db9c6 87 "/docker-entrypoint.…" 46 minutes ago Up 46 minutes 80/tcp amazing_yalow
  8. 9b3783f131d3 5d "/bin/bash" 46 minutes ago Exited (0) 46 minutes ago eloquent_feynman

可以发现每个容器启动之后都会执行一个命令,centos8容器是一个bash窗口,nginx容器是一个nginx的启动脚本。

  1. [root@VM-8-9-centos ~]# docker run -it 5d /bin/bash
  2. [root@ade15ea3850d /]#

如果我们使用前台命令运行,也需要以交互模式运行,否则容器会自动stop,因为即使是以前台方式运行,但是无法进行交互,容器也是无价值的。

查看控制台输出

上面提到,每个容器在启动之后都会执行一个命令,那么这个命名在执行的时候是可能在控制台上打印一些日志的。查看这些输出需要使用docker logs命令。

  1. [root@VM-8-9-centos ~]# docker ps -a
  2. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  3. 10d71843a63f nginx:1.21.3 "/docker-entrypoint.…" 7 minutes ago Up 4 minutes 80/tcp awesome_hopper
  4. 2f20c79ffaf2 centos "/bin/sh -c 'while t…" 11 minutes ago Up 11 minutes zealous_brattain
  5. # -t 行前输出日志打印的时间
  6. # -f 以follow模式运行
  7. # --tail n 显示末尾n行
  8. [root@VM-8-9-centos ~]# docker logs -t -f --tail 12 10
  9. 2021-10-14T10:40:03.745367518Z 10-listen-on-ipv6-by-default.sh: info: IPv6 listen already enabled
  10. 2021-10-14T10:40:03.745500346Z /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
  11. 2021-10-14T10:40:03.748270887Z /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
  12. 2021-10-14T10:40:03.749476173Z /docker-entrypoint.sh: Configuration complete; ready for start up
  13. 2021-10-14T10:40:03.753653093Z 2021/10/14 10:40:03 [notice] 1#1: using the "epoll" event method
  14. 2021-10-14T10:40:03.753666392Z 2021/10/14 10:40:03 [notice] 1#1: nginx/1.21.3
  15. 2021-10-14T10:40:03.753690583Z 2021/10/14 10:40:03 [notice] 1#1: built by gcc 8.3.0 (Debian 8.3.0-6)
  16. 2021-10-14T10:40:03.753695450Z 2021/10/14 10:40:03 [notice] 1#1: OS: Linux 4.18.0-305.3.1.el8.x86_64
  17. 2021-10-14T10:40:03.753699459Z 2021/10/14 10:40:03 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
  18. 2021-10-14T10:40:03.753752419Z 2021/10/14 10:40:03 [notice] 1#1: start worker processes
  19. 2021-10-14T10:40:03.754481107Z 2021/10/14 10:40:03 [notice] 1#1: start worker process 25
  20. 2021-10-14T10:40:03.754494278Z 2021/10/14 10:40:03 [notice] 1#1: start worker process 26

上面是我启动了一个nginx,nginx启动的时候会执行docker-entrypoint.sh脚本,脚本执行的时候会在控制台上打印一些日志。

文件拷贝

控制台的日志一般是一些基本的服务启动信息,完整的日志会写在日志文件中,所以一种看日志的方式就是进入容器看,因为容器就是一个Linux。但是容器本身的Linux功能很少,所以一般还是将日志拷贝出来比较方便。
从容器内部拷贝文件出来的命令是docker cp。

  1. [root@VM-8-9-centos ~]# docker run -d -it --name="mycentos" centos:centos8.4.2105 /bin/bash
  2. c57aaf10844578bb1165a675fcb81e399603c8c67f979ecc7ec852fd806be69a
  3. [root@VM-8-9-centos ~]# docker exec -i -t mycentos /bin/bash
  4. [root@c57aaf108445 /]# echo "1">test
  5. [root@c57aaf108445 /]# cat test
  6. 1
  7. [root@c57aaf108445 /]# exit
  8. exit
  9. [root@VM-8-9-centos ~]# docker ps -a
  10. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  11. c57aaf108445 centos:centos8.4.2105 "/bin/bash" About a minute ago Up About a minute mycentos
  12. 671a4427be55 nginx:1.21.3 "/docker-entrypoint.…" 4 minutes ago Up 4 minutes 0.0.0.0:8080->80/tcp, :::8080->80/tcp mynginx
  13. # docker cp 容器ID:绝对路径 绝对路径
  14. [root@VM-8-9-centos ~]# docker cp c57aaf10844:/test /test2
  15. [root@VM-8-9-centos ~]# cat /test2
  16. 1

容器启动的思考

docker镜像可以理解为一个安装了应用的Linux系统,docker容器可以理解为一个运行了服务的Linux系统。容器的启动应该是一个Linux的内核和一个Linux终端,然后在终端上执行一个命令或脚本来启动服务。从之前的测试中可以看出来,centos8容器启动之后会启动/bin/bash这个应用,nginx容器启动之后会执行docker-entrypoint.sh来启动nginx。
如果启动容器之后既没有用户态进程,又没有可交互的终端,docker会认为这个容器没有价值,就给它停了。
启动应用的终端除了使用之前提到过的docker logs查看输出以外,还可以使用docker attach连接到这个终端上。

  1. [root@VM-8-9-centos ~]# docker run -d centos /bin/sh -c "while true;do echo 6666;sleep 1;done"
  2. 17144183aec951ed19b825ba35e6bd28ae8e8bbfaa761ecf467fca7b73aee34e
  3. [root@VM-8-9-centos ~]# docker ps -a
  4. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  5. 17144183aec9 centos "/bin/sh -c 'while t…" 2 seconds ago Up 1 second busy_benz
  6. e042f05fae1f centos "/bin/sh 'while true…" 24 seconds ago Exited (127) 23 seconds ago modest_gould
  7. 671a4427be55 nginx:1.21.3 "/docker-entrypoint.…" 53 minutes ago Up 53 minutes 0.0.0.0:8080->80/tcp, :::8080->80/tcp mynginx
  8. [root@VM-8-9-centos ~]# docker attach 171
  9. 6666
  10. 6666
  11. 6666
  12. 6666

连接到这个终端上感觉也没什么价值。毕竟可以使用docker logs查看终端的输出。

镜像制作

容器提交为镜像

nginx有一个默认的启动页面,我们将其修改为Hello World。

  1. [root@VM-8-9-centos ~]# docker ps -a
  2. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  3. 671a4427be55 nginx:1.21.3 "/docker-entrypoint.…" About an hour ago Up About an hour 0.0.0.0:8080->80/tcp, :::8080->80/tcp mynginx
  4. [root@VM-8-9-centos ~]# docker exec -it 67 /bin/bash
  5. root@671a4427be55:/# cd /usr/share/nginx/html
  6. root@671a4427be55:/usr/share/nginx/html# ls -a -l
  7. total 16
  8. drwxr-xr-x 2 root root 4096 Oct 12 02:03 .
  9. drwxr-xr-x 3 root root 4096 Oct 12 02:03 ..
  10. -rw-r--r-- 1 root root 497 Sep 7 15:21 50x.html
  11. -rw-r--r-- 1 root root 615 Sep 7 15:21 index.html
  12. root@671a4427be55:/usr/share/nginx/html# echo "Hello World" > index.html
  13. root@671a4427be55:/usr/share/nginx/html# curl localhost
  14. Hello World

如果换了新的服务器,那么还需要修改这个文件,所以为了能方便ship,这个容器可以被提交为镜像。

  1. # -a author
  2. # -m commit message
  3. [root@VM-8-9-centos ~]# docker commit -a "addenda" -m "index page change" 671a4427be55 addenda_nginx:v1.0
  4. sha256:9e07a3f2860b1e904dc6c740c26fdf6fdb56ea5075d500025969b255df052d15
  5. [root@VM-8-9-centos ~]# docker images
  6. REPOSITORY TAG IMAGE ID CREATED SIZE
  7. addenda_nginx v1.0 9e07a3f2860b 5 seconds ago 133MB
  8. nginx 1.21.3 87a94228f133 2 days ago 133MB
  9. hello-world latest feb5d9fea6a5 2 weeks ago 13.3kB
  10. centos centos8.4.2105 5d0da3dc9764 4 weeks ago 231MB
  11. centos latest 5d0da3dc9764 4 weeks ago 231MB
  12. [root@VM-8-9-centos ~]# docker run -d -p 8081:80 addenda_nginx:v1.0
  13. b05ad7d463503f72e59da6efec812ceb20538d64589be8868923d1f292ed0a71
  14. [root@VM-8-9-centos ~]# docker ps -a
  15. CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
  16. b05ad7d46350 addenda_nginx:v1.0 "/docker-entrypoint.…" 2 seconds ago Up 2 seconds 0.0.0.0:8081->80/tcp, :::8081->80/tcp beautiful_greider
  17. 671a4427be55 nginx:1.21.3 "/docker-entrypoint.…" 2 hours ago Up 6 minutes 0.0.0.0:8080->80/tcp, :::8080->80/tcp mynginx
  18. [root@VM-8-9-centos ~]# curl localhost:8081
  19. Hello World

镜像打包 & 镜像加载

  1. [root@VM-8-9-centos ~]# docker images
  2. REPOSITORY TAG IMAGE ID CREATED SIZE
  3. addenda_nginx v1.0 9e07a3f2860b About an hour ago 133MB
  4. nginx 1.21.3 87a94228f133 2 days ago 133MB
  5. hello-world latest feb5d9fea6a5 2 weeks ago 13.3kB
  6. centos centos8.4.2105 5d0da3dc9764 4 weeks ago 231MB
  7. centos latest 5d0da3dc9764 4 weeks ago 231MB
  8. # docker save,默认的输出流是STDOUT,-o可选项是输出到文件
  9. [root@VM-8-9-centos ~]# docker save -o addenda_nginx.tar addenda_nginx:v1.0
  10. [root@VM-8-9-centos ~]# ll
  11. total 134344
  12. -rw------- 1 root root 137564672 Oct 14 21:47 addenda_nginx.tar
  13. [root@VM-8-9-centos ~]# docker rmi addenda_nginx:v1.0
  14. Untagged: addenda_nginx:v1.0
  15. Deleted: sha256:9e07a3f2860b1e904dc6c740c26fdf6fdb56ea5075d500025969b255df052d15
  16. Deleted: sha256:d758613c98a6b43089bf68ddd7d847604c5a37c800c3a3615f06d621bcf36a5e
  17. [root@VM-8-9-centos ~]# docker images
  18. REPOSITORY TAG IMAGE ID CREATED SIZE
  19. nginx 1.21.3 87a94228f133 2 days ago 133MB
  20. hello-world latest feb5d9fea6a5 2 weeks ago 13.3kB
  21. centos centos8.4.2105 5d0da3dc9764 4 weeks ago 231MB
  22. centos latest 5d0da3dc9764 4 weeks ago 231MB
  23. # docker load,默认的输入流是STDIN,-i可选项是从文件夹读取
  24. [root@VM-8-9-centos ~]# docker load -i addenda_nginx.tar
  25. 59d19ee14d22: Loading layer [==================================================>] 17.41kB/17.41kB
  26. Loaded image: addenda_nginx:v1.0
  27. [root@VM-8-9-centos ~]# docker images
  28. REPOSITORY TAG IMAGE ID CREATED SIZE
  29. addenda_nginx v1.0 9e07a3f2860b About an hour ago 133MB
  30. nginx 1.21.3 87a94228f133 2 days ago 133MB
  31. hello-world latest feb5d9fea6a5 2 weeks ago 13.3kB
  32. centos centos8.4.2105 5d0da3dc9764 4 weeks ago 231MB
  33. centos latest 5d0da3dc9764 4 weeks ago 231MB
  34. [root@VM-8-9-centos ~]# docker run -d -p 8081:80 addenda_nginx:v1.0
  35. ffbc8db810514d1f569535385c1cd827be7fc3b85db4bb159f77ee99f31ad288
  36. [root@VM-8-9-centos ~]# curl localhost:8081
  37. Hello World

镜像发布到docker hub

镜像发布需要先去docker hub上注册一个账户。
然后需要将本地的镜像打一个tag。

  1. # docker tag local-image:tagname new-repo:tagname
  2. [root@VM-8-9-centos ~]# docker tag addenda_nginx:v1.0 addenda1998/addenda_nginx:v1.0

登录

  1. [root@VM-8-9-centos ~]# docker login -u addenda1998 -p password
  2. WARNING! Using --password via the CLI is insecure. Use --password-stdin.
  3. WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
  4. Configure a credential helper to remove this warning. See
  5. https://docs.docker.com/engine/reference/commandline/login/#credentials-store
  6. Login Succeeded

推送

  1. [root@VM-8-9-centos ~]# docker push addenda1998/addenda_nginx:v1.0
  2. The push refers to repository [docker.io/addenda1998/addenda_nginx]
  3. 59d19ee14d22: Pushed
  4. 9959a332cf6e: Mounted from library/nginx
  5. f7e00b807643: Mounted from library/nginx
  6. f8e880dfc4ef: Mounted from library/nginx
  7. 788e89a4d186: Mounted from library/nginx
  8. 43f4e41372e4: Mounted from library/nginx
  9. e81bff2725db: Mounted from library/nginx
  10. v1.0: digest: sha256:84e8bffdd2f727db1d05421bc3d32ab743fd6b10acd8b8f926ff619416ae59eb size: 1778

退出

  1. [root@VM-8-9-centos ~]# docker logout
  2. Removing login credentials for https://index.docker.io/v1/

在docker hub上就能看见了。
image.png

镜像发布到腾讯云

由于在国内docker hub不好用,所以我们一般将镜像推送到阿里云或腾讯云的镜像仓库,由于写测试的服务器是腾讯云的,所以这里就以腾讯云仓库镜像来演示了。

  1. [root@VM-8-9-centos ~]# docker tag addenda_nginx:v1.0 ccr.ccs.tencentyun.com/addenda/addenda_learn:v1.0
  2. [root@VM-8-9-centos ~]# docker login --username=100003170363 ccr.ccs.tencentyun.com
  3. Password:
  4. WARNING! Your password will be stored unencrypted in /root/.docker/config.json.
  5. Configure a credential helper to remove this warning. See
  6. https://docs.docker.com/engine/reference/commandline/login/#credentials-store
  7. Login Succeeded
  8. [root@VM-8-9-centos ~]# docker push ccr.ccs.tencentyun.com/addenda/addenda_learn:v1.0
  9. The push refers to repository [ccr.ccs.tencentyun.com/addenda/addenda_learn]
  10. 59d19ee14d22: Pushed
  11. 9959a332cf6e: Pushed
  12. f7e00b807643: Pushed
  13. f8e880dfc4ef: Pushed
  14. 788e89a4d186: Pushed
  15. 43f4e41372e4: Pushed
  16. e81bff2725db: Pushed
  17. v1.0: digest: sha256:84e8bffdd2f727db1d05421bc3d32ab743fd6b10acd8b8f926ff619416ae59eb size: 1778
  18. [root@VM-8-9-centos ~]# docker logout
  19. Removing login credentials for https://index.docker.io/v1/

唯一的区别就是docker镜像的地址不同。其他步骤和docker hub一致。
image.png
推送成功之后就可以在腾讯云上看见了。
需要注意,如果设置为私有镜像,那么需要登录后才能docker pull。

Docker基础命令总结

image.png