一、docker的安装

1.依赖包安装

yum install -y yum-utils device-mapper-persistent-data lvm2

2.添加yum源

yum-config-manager \
--add-repo \
https://mirrors.ustc.edu.cn/docker-ce/linux/centos/docker-ce.repo
yum-config-manager --enable docker-ce-edge
#配置为安装最新版Docker CE

3.安装docker

yum install docker-ce

*官方为了简化安装流程提供了便捷的安装脚本

curl -fsSL get.docker.com -o get-docker.sh
sh get-docker.sh --mirror Aliyun

4.启动docker

systemctl enable docker
systemctl start docker

5.建立docker用户和组

6.测试docker是否安装完成

[docker@dockertest ~]$ docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
ca4f61b1923c: Pull complete 
Digest: sha256:083de497cff944f969d8499ab94f07134c50bcf5e6b9559b27182d3fa80ce3f7
Status: Downloaded newer image for hello-world:latest

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://cloud.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/engine/userguide/

7.配置镜像加速器并重启服务

cat /etc/docker/daemon.json 
{
"registry-mirrors": [
"https://registry.docker-cn.com"
 ]
}
systemctl daemon-reload
systemctl restart docker

二、使用镜像

1.获取镜像

[root@dockertest ~]# docker pull ubuntu:16.04
16.04: Pulling from library/ubuntu
22dc81ace0ea: Pull complete 
1a8b3c87dba3: Pull complete 
91390a1c435a: Pull complete 
07844b14977e: Pull complete 
b78396653dae: Pull complete 
Digest: sha256:e348fbbea0e0a0e73ab0370de151e7800684445c509d46195aef73e090a49bd6
Status: Downloaded newer image for ubuntu:16.04

2.查看获取的镜像

[root@dockertest ~]# docker image ls 

#使用--help可以查看参数选项,比如自己定义显示的列

[root@dockertest ~]# docker image ls --format "table {{.ID}}\t{{.Repository}}\t{{.Tag}}"
IMAGE ID            REPOSITORY          TAG
f975c5035748        ubuntu              16.04
f2a91732366c        hello-world         latest

#查看空间的占用

[root@dockertest ~]# docker system df

3.删除镜像

#删除本地镜像可以使用镜像短ID、长ID、镜像名或镜像摘要来删除

[root@dockertest ~]# docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              16.04               f975c5035748        7 days ago          112MB
centos              latest              2d194b392dd1        8 days ago          195MB
mysql               latest              5d4d51c57ea8        2 weeks ago         374MB
nginx               latest              e548f1a579cf        3 weeks ago         109MB
hello-world         latest              f2a91732366c        3 months ago        1.85kB
[root@dockertest ~]# docker image rm 5d4
[root@dockertest ~]# docker image rm nginx
[root@dockertest ~]# docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              16.04               f975c5035748        7 days ago          112MB
centos              latest              2d194b392dd1        8 days ago          195MB
hello-world         latest              f2a91732366c        3 months ago        1.85kB

#还可以使用docker image ls命令来配合

docker image rm $(docker image ls -q ubuntu)
docker image rm $(docker image ls -q -f before=centos)

三、容器的操作

1.容器的启动

①新建并启动

[root@dockertest ~]# docker run ubuntu:16.04 /bin/echo "hello world"
hello world

②启动一个bash终端,允许用户进行交互。

[root@dockertest ~]# docker run -ti ubuntu:16.04 /bin/bash
root@edf8af896cc0:/# pwd
/
#选项 '-t' 开启一个伪终端,'-i' 让容器的标准输入保持打开。

③后台运行

[root@dockertest ~]# docker run -d ubuntu:16.04 /bin/sh -c "while true; do echo hello world; sleep 10; done"
cab4112e91fb0d6eae3762ec2a07b13798e908503a8f58799ac50fd9b4aa7e37

#此时容器会在后台运行并不会把输出的结果打印到宿主机上面,可以使用docker logs查看输出信息。

[root@dockertest ~]# docker logs cab41
hello world
hello world
hello world
hello world

2.容器的终止

[root@dockertest ~]# docker container ls
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
cab4112e91fb        ubuntu:16.04        "/bin/sh -c 'while t…"   5 minutes ago       Up 5 minutes                     
[root@dockertest ~]# docker container stop cab41
cab41

#查看终止的容器

[root@dockertest ~]# docker container ls -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                            PORTS               NAMES
cab4112e91fb        ubuntu:16.04        "/bin/sh -c 'while t…"   7 minutes ago       Exited (137) About a minute ago                       angry_wescoff

#可以使用docker container start来启动处于终止状态的容器

[root@dockertest ~]# docker container start cab41
cab41
[root@dockertest ~]# docker container ls 
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
cab4112e91fb        ubuntu:16.04        "/bin/sh -c 'while t…"   10 minutes ago      Up 5 seconds                            angry_wescoff
[root@dockertest ~]# docker attach 4417
root@4417b64c1c7f:/# pwd
/
root@4417b64c1c7f:/# exit
exit

3.进入容器

①attack(输入exit会导致容器的终止)

[root@dockertest ~]# docker run -itd ubuntu
4417b64c1c7f5c59bc446467e6d3c033ebbe6412f6e51fa722e463ff2b41a3a9
[root@dockertest ~]# docker container ls 
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
4417b64c1c7f        ubuntu              "/bin/bash"         8 seconds ago       Up 7 seconds                            hungry_easley

②exec(输入exit不会导致容器的终止)

[root@dockertest ~]# docker run -itd ubuntu
5276352cf8d11ec07c12d07adfa209453299822aae779ac9ff2f401e7ff1fcb1
[root@dockertest ~]# docker container ls 
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
5276352cf8d1        ubuntu              "/bin/bash"         8 seconds ago       Up 8 seconds                            blissful_edison
[root@dockertest ~]# docker exec -it 5276 bash
root@5276352cf8d1:/# whoami
root
root@5276352cf8d1:/# exit
exit

#也可以使用组合键退出,仍然保持容器运行,我们可以随时回来到这个bash中来,组合键是 Ctrl-p Ctrl-q,先同时按下Ctrl和p,再按Ctrl和q。就可以退出到我们的宿主机了。

4.容器的导出和导入

①导出

[root@dockertest ~]# docker export 4417 > test.tar
[root@dockertest ~]# ls
test.tar

②导入

[root@dockertest ~]# cat test.tar | docker import - test/ubuntu:v1.1
sha256:fb0e8d9dcaba2d018ec1fe26394f3b4989db55ace695d1c685c5e753fbe8ed9e
[root@dockertest ~]# docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
test/ubuntu         v1.1                fb0e8d9dcaba        3 seconds ago       85.8MB

#也可以通过指定URL或者某个目录来导入

[root@dockertest ~]# docker import /root/test.tar test/ubuntu:v1.2
sha256:e33803dd759481e27cd807536f28fe8ab0cfddd9c3bae2a15b95ea8b7645172b
[root@dockertest ~]# docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
test/ubuntu         v1.2                e33803dd7594        3 seconds ago       85.8MB
test/ubuntu         v1.1                fb0e8d9dcaba        3 minutes ago       85.8MB

5.删除容器

①删除一个处于终止状态的容器

[root@dockertest ~]# docker container rm edf8af
edf8af
#如果要删除一个运行中的容器,可以添加 -f 参数来删除。

②清除所有处于终止状态的容器

[root@dockertest ~]# docker container prune
WARNING! This will remove all stopped containers.
Are you sure you want to continue? [y/N] y
Deleted Containers:
4417b64c1c7f5c59bc446467e6d3c033ebbe6412f6e51fa722e463ff2b41a3a9
5276352cf8d11ec07c12d07adfa209453299822aae779ac9ff2f401e7ff1fcb1
99aee81f7684d59c72d2c1bbc933fd052f2158e833fb08988803108de613de24
cab4112e91fb0d6eae3762ec2a07b13798e908503a8f58799ac50fd9b4aa7e37

#学习文档地址:https://github.com/yeasy/docker_practice/blob/master/SUMMARY.md