建站不啰嗦,上手跟我做(二十二)Docker 使用

Docker 安装

[root@localhost ~]# yum install docker
[root@localhost ~]# docker version
Client:
 Version:         1.13.1
 API version:     1.26
 Package version: 
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?

安装指定版本 docker

[root@test ~]# yum list installed | grep docker
docker-ce.x86_64                      18.06.3.ce-3.el7                @docker-ce-stable
[root@test ~]# yum -y remove docker-ce.x86_64
[root@test ~]# yum list docker-ce --showduplicates|sort -r
Repository base is listed more than once in the configuration
Repository updates is listed more than once in the configuration
Repository extras is listed more than once in the configuration
Repository centosplus is listed more than once in the configuration
Repodata is over 2 weeks old. Install yum-cron? Or run: yum makecache fast
 * updates: mirrors.bfsu.edu.cn
This system is not registered with an entitlement server. You can use subscription-manager to register.
              : manager
Loaded plugins: fastestmirror, product-id, search-disabled-repos, subscription-
 * extras: mirrors.bfsu.edu.cn
docker-ce.x86_64            3:20.10.1-3.el7                     docker-ce-stable
docker-ce.x86_64            3:20.10.0-3.el7                     docker-ce-stable
docker-ce.x86_64            3:19.03.9-3.el7                     docker-ce-stable
docker-ce.x86_64            3:19.03.8-3.el7                     docker-ce-stable
docker-ce.x86_64            3:19.03.7-3.el7                     docker-ce-stable
docker-ce.x86_64            3:19.03.6-3.el7                     docker-ce-stable
docker-ce.x86_64            3:19.03.5-3.el7                     docker-ce-stable
docker-ce.x86_64            3:19.03.4-3.el7                     docker-ce-stable
docker-ce.x86_64            3:19.03.3-3.el7                     docker-ce-stable
docker-ce.x86_64            3:19.03.2-3.el7                     docker-ce-stable
docker-ce.x86_64            3:19.03.14-3.el7                    docker-ce-stable
docker-ce.x86_64            3:19.03.1-3.el7                     docker-ce-stable
docker-ce.x86_64            3:19.03.13-3.el7                    docker-ce-stable
docker-ce.x86_64            3:19.03.12-3.el7                    docker-ce-stable
docker-ce.x86_64            3:19.03.11-3.el7                    docker-ce-stable
docker-ce.x86_64            3:19.03.10-3.el7                    docker-ce-stable
docker-ce.x86_64            3:19.03.0-3.el7                     docker-ce-stable
docker-ce.x86_64            3:18.09.9-3.el7                     docker-ce-stable
docker-ce.x86_64            3:18.09.8-3.el7                     docker-ce-stable
docker-ce.x86_64            3:18.09.7-3.el7                     docker-ce-stable
docker-ce.x86_64            3:18.09.6-3.el7                     docker-ce-stable
docker-ce.x86_64            3:18.09.5-3.el7                     docker-ce-stable
docker-ce.x86_64            3:18.09.4-3.el7                     docker-ce-stable
docker-ce.x86_64            3:18.09.3-3.el7                     docker-ce-stable
docker-ce.x86_64            3:18.09.2-3.el7                     docker-ce-stable
docker-ce.x86_64            3:18.09.1-3.el7                     docker-ce-stable
docker-ce.x86_64            3:18.09.0-3.el7                     docker-ce-stable
docker-ce.x86_64            18.06.3.ce-3.el7                    docker-ce-stable
docker-ce.x86_64            18.06.2.ce-3.el7                    docker-ce-stable
docker-ce.x86_64            18.06.1.ce-3.el7                    docker-ce-stable
docker-ce.x86_64            18.06.0.ce-3.el7                    docker-ce-stable
docker-ce.x86_64            18.03.1.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            18.03.0.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            17.12.1.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            17.12.0.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            17.09.1.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            17.09.0.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            17.06.2.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            17.06.1.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            17.06.0.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            17.03.3.ce-1.el7                    docker-ce-stable
docker-ce.x86_64            17.03.2.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            17.03.1.ce-1.el7.centos             docker-ce-stable
docker-ce.x86_64            17.03.0.ce-1.el7.centos             docker-ce-stable
[root@test ~]# yum install docker-ce-17.03.3.ce-1.el7

docker 启动

[root@localhost ~]# service docker start
Redirecting to /bin/systemctl start docker.service
[root@localhost ~]# chkconfig docker on
Note: Forwarding request to 'systemctl enable docker.service'.
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
[root@localhost ~]# systemctl start docker.service
[root@localhost ~]# systemctl enable docker.service
[root@localhost ~]# docker version
Client:
 Version:         1.13.1
 API version:     1.26
 Package version: docker-1.13.1-84.git07f3374.el7.centos.x86_64
 Go version:      go1.10.2
 Git commit:      07f3374/1.13.1
 Built:           Fri Nov 30 02:48:45 2018
 OS/Arch:         linux/amd64

Server:
 Version:         1.13.1
 API version:     1.26 (minimum version 1.12)
 Package version: docker-1.13.1-84.git07f3374.el7.centos.x86_64
 Go version:      go1.10.2
 Git commit:      07f3374/1.13.1
 Built:           Fri Nov 30 02:48:45 2018
 OS/Arch:         linux/amd64
 Experimental:    false

配置 Docker 镜像地址

[root@localhost docker]# vim /etc/docker/daemon.json


{
        "registry-mirrors":["https://registry.docker-cn.com"],
        "live-restore":true
}

设置 docker 仓库

[root@localhost opt]# yum-config-manager     --add-repo     http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
-bash: yum-config-manager: command not found
[root@localhost opt]# yum -y install yum-utils
Loaded plugins: fastestmirror, product-id, search-disabled-repos, subscription-manager
[root@localhost opt]# yum-config-manager     --add-repo     http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
Loaded plugins: fastestmirror, product-id, subscription-manager

获取镜像到本地

[root@localhost docker]# docker pull library/hello-world
Using default tag: latest
Trying to pull repository docker.io/library/hello-world ... 
Get https://registry-1.docker.io/v2/: x509: certificate signed by unknown authority

library/hello-world 是 image 文件在仓库里面的位置,其中 library 是 image 文件所在的组,hello-world 是 image 文件的名字。

[root@localhost docker]# docker images
REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE
docker.io/hello-world   latest              4ab4c602aa5e        11 months ago       1.84 kB

运行 docker 镜像

[root@localhost docker]# docker run hello-world

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

输出这段提示以后,hello world 就会停止运行,容器自动终止。有些容器不会自动终止,因为提供的是服务,比如 Mysql 镜像等。

运行交互式容器

[root@localhost opt]# docker run -i -t ubuntu:15.10 /bin/bash
root@6783b17539b7:/# ls
bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
root@6783b17539b7:/# cat /proc/version
Linux version 3.10.0-957.el7.x86_64 (mockbuild@kbuilder.bsys.centos.org) (gcc version 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC) ) #1 SMP Thu Nov 8 23:39:32 UTC 2018
  • ubuntu:15.10** 指定要运行的镜像,Docker 首先从本地主机上查找镜像是否存在,如果不存在,Docker 就会从镜像仓库 Docker Hub 下载公共镜像。
  • -t: 在新容器内指定一个伪终端或终端。
  • -i: 允许你对容器内的标准输入 (STDIN) 进行交互。

通过运行 exit 命令或者使用 CTRL+D 来退出容器

root@6783b17539b7:/# exit
exit
[root@localhost opt]#

启动容器(后台模式)

[root@localhost opt]# docker run -d ubuntu:15.10 /bin/sh -c "while true; do echo hello world; sleep 1; done"
8eee47b8f771046a92710012057dd11c0f6abb321f1dc85a07df8a615ddd3634

使用 docker logs 命令,查看容器内的标准输出

[root@localhost opt]# docker logs 8eee47b8f771046a92710012057dd11c0f6abb321f1dc85a07df8a615ddd3634
hello world
hello world
hello world
hello world
hello world

查看当前有哪些容器正在运行

[root@localhost docker]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

CONTAINER ID: 容器 ID。

IMAGE: 使用的镜像。

COMMAND: 启动容器时运行的命令。

CREATED: 容器的创建时间。

STATUS: 容器状态。

状态有 7 种:

  • created(已创建)
  • restarting(重启中)
  • running 或 Up(运行中)
  • removing(迁移中)
  • paused(暂停)
  • exited(停止)
  • dead(死亡)

PORTS: 容器的端口信息和使用的连接类型(tcp\udp)。

NAMES: 自动分配的容器名称。

查看所有容器

[root@localhost docker]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                    PORTS               NAMES
eb736d3af742        hello-world         "/hello"            2 hours ago         Exited (0) 2 hours ago                        quizzical_sammet
c28a3133520f        hello-world         "/hello"            8 months ago        Exited (0) 8 months ago                       goofy_minsky
1c5681baa395        hello-world         "/hello"            8 months ago        Exited (0) 8 months ago                       admiring_boyd

启动、停止、重启容器命令

启动

docker start container_name/container_id

[root@localhost docker]# docker start 1c5681baa395
1c5681baa395

停止

docker stop container_name/container_id

[root@localhost docker]# docker stop 1c5681baa395
1c5681baa395

重启

docker restart container_name/container_id

[root@localhost docker]# docker restart 1c5681baa395
1c5681baa395
[root@localhost docker]# docker restart admiring_boyd
admiring_boyd

后台启动一个容器后,如果想进入到这个容器,可以使用 attach 命令

docker attach container_name/container_id

[root@localhost docker]# docker attach 1c5681baa395

进入运行中的容器

使用docker exec命令可以进入正在运行的容器。语法如下:

docker exec -it <container_name> bash

其中,<container_name>是需要进入的容器的名称或 ID。
这个命令的含义是以交互式的方式(-i-t参数)进入 Docker 容器,并在容器中打开 Bash shell。
例如,要进入名为mycontainer的容器,可以执行以下命令:

[root@localhost ~]# docker exec -it 3fa2ca80c7e5 bash
root@3fa2ca80c7e5:/#

进入容器后,就可以在其中执行命令和操作文件系统,就好像在一台独立的计算机中一样。要退出容器,只需要使用exit命令即可。

docker exec -u root -it 3fa2ca80c7e5 sh

删除容器的命令

docker rm container_name/container_id

[root@localhost docker]# docker rm 1c5681baa395
1c5681baa395

删除所有停止的容器

[root@localhost docker]# docker rm  $(docker ps -a  -q)
eb736d3af742
c28a3133520f

查看当前系统 Docker 信息

[root@localhost docker]# docker info
Containers: 0
 Running: 0
 Paused: 0
 Stopped: 0
Images: 1
Server Version: 1.13.1
Storage Driver: overlay2
 Backing Filesystem: xfs
 Supports d_type: true
 Native Overlay Diff: true
Logging Driver: journald
Cgroup Driver: systemd
Plugins: 
 Volume: local
 Network: bridge host macvlan null overlay
Swarm: inactive
Runtimes: docker-runc runc
Default Runtime: docker-runc
Init Binary: /usr/libexec/docker/docker-init-current
containerd version:  (expected: aa8187dbd3b7ad67d8e5e3a15115d3eef43a7ed1)
runc version: 5eda6f6fd0c2884c2c8e78a6e7119e8d0ecedb77 (expected: 9df8b306d01f59d3a8029be411de015b7304dd8f)
init version: fec3683b971d9c3ef73f284f176672c44b448662 (expected: 949e6facb77383876aeff8a6944dde66b3089574)
Security Options:
 seccomp
  WARNING: You're not using the default seccomp profile
  Profile: /etc/docker/seccomp.json
 selinux
Kernel Version: 3.10.0-957.el7.x86_64
Operating System: CentOS Linux 7 (Core)
OSType: linux
Architecture: x86_64
Number of Docker Hooks: 3
CPUs: 1
Total Memory: 1.795 GiB
Name: localhost.localdomain
ID: H3RD:3ODC:BTWP:HKP2:U64Z:VKJK:TN7W:AEGY:DA7H:QONN:5YR4:TE6S
Docker Root Dir: /var/lib/docker
Debug Mode (client): false
Debug Mode (server): false
Registry: https://index.docker.io/v1/
Experimental: false
Insecure Registries:
 127.0.0.0/8
Registry Mirrors:
 https://registry.docker-cn.com
Live Restore Enabled: true
Registries: docker.io (secure)

从 Docker hub 上下载某个镜像:

[root@localhost docker]# docker pull centos:latest 
Trying to pull repository docker.io/library/centos ...

查找 Docker Hub 上的 nginx 镜像

[root@localhost docker]# docker search nginx

删除镜像

如果镜像在被容器使用,只能强制删除或者先删除使用镜像的容器后再删除。

[root@localhost docker]# docker rmi docker.io/hello-world
Error response from daemon: conflict: unable to remove repository reference "docker.io/hello-world" (must force) - container 1c5681baa395 is using its referenced image 4ab4c602aa5e
root@localhost docker]# docker rmi docker.io/hello-world
Untagged: docker.io/hello-world:latest
Untagged: docker.io/hello-world@sha256:0add3ace90ecb4adbf7777e9aacf18357296e799f81cabc9fde470971e499788
Deleted: sha256:4ab4c602aa5eed5528a6620ff18a1dc4faef0e1ab3a5eddeddb410714478c67f
Deleted: sha256:428c97da766c4c13b19088a471de6b622b038f3ae8efa10ec5a37d6d31a2df0b

上一篇 建站不啰嗦,上手跟我做(二十一)FastDFS+FastDHT 安装
目录
下一篇 建站不啰嗦,上手跟我做(二十三)Go 环境配置