建站不啰嗦,上手跟我做(二十二)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@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
}

获取镜像到本地

[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 docker]# docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               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 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 环境配置