一、安装卸载虚拟机
1 下载Docker yum仓库
yum install -y wget #如果没有安装wget的需要进行这一步操作
wget https://download.docker.com/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo
2 安装Docker引擎
yum install -y docker-ce docker-ce-cli containerd.io
3 启动Docker服务
systemctl start docker && systemctl enable docker
4 通过运行hello-world 映像来验证是否正确安装了Docker
4.1 因为docker是国外的,所以这里需要修改国内镜像源(我是使用的阿里的)
- 访问阿里云官网 https://www.aliyun.com/?utm_content=se_1008364713
- 注册登录账号
- 点击控制台
4.2 验证
[root@bogon ~]# 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/
这时,Docker 会主动去下载这个镜像,并用这个镜像启动一个容器,那么一个容器就做好了
5 卸载
5.1 卸载Docker引擎
yum remove docker-ce docker-ce-cli containerd.io
5.2 删除所有的镜像、容器和卷
rm -rf /var/lib/docker
二、命令行管理Docker容器
1 运行Centos7容器
docker run -it centos:7
#run : 子命令
#-it : 选项
#centos:7 : 镜像名称:标签
如果运行的镜像不在本地,会自动从 Docker hub 下载到本地,之后再运行。
[root@bogon ~]#docker run -it centos:7
Unable to find image 'centos:7' locally
7: Pulling from library/centos
75f829a71a1c: Pull complete
Digest: sha256:19a79828ca2e505eaee0ff38c2f3fd9901f4826737295157cc5212b7a372cd2b
Status: Downloaded newer image for centos:7
[root@f940d5811e59 /]# ls
anaconda-post.log bin dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
[root@f940d5811e59 /]# exit #退出容器
exit
[root@bogon ~]#
-t
分配一个伪TTY,以便和容器进行命令的交互
-i
表示持续和 容器交互,防止断开
2 查看本地镜像源
使用命令 docker images
或者 docker image ls
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest f6d0b4767a6c 2 months ago 133MB
centos 7 8652b9f0cb4c 4 months ago 204MB
hello-world latest bf756fb1ae65 15 months ago 13.3kB
3 容器进阶操作
3.1 查看容器
docker ps
查看正在运行的容器
docker ps -a
查看所有容器
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bd29e59fa4e0 nginx "/docker-entrypoint.…" 11 minutes ago Up 11 minutes 0.0.0.0:8000->80/tcp exciting_lichterman
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bd29e59fa4e0 nginx "/docker-entrypoint.…" 11 minutes ago Up 11 minutes 0.0.0.0:8000->80/tcp exciting_lichterman
dab16a613e7b nginx "/docker-entrypoint.…" 12 minutes ago Exited (0) 12 minutes ago pedantic_chatterjee
f940d5811e59 centos:7 "/bin/bash" 4 hours ago Exited (0) 4 hours ago dreamy_gates
4b7c55731990 hello-world "/hello" 4 hours ago Exited (0) 4 hours ago affectionate_snyder
各列的含义
字段名 | 含义 |
---|---|
CONTAINER ID | 容器 ID, 具有唯一性 |
IMAGE | 镜像名称, 就是说这个容器是用这个镜像创建的 |
COMMAND | 运行这个容器时,在容器内执行的命令,一般都有一些默认的命令 |
CREATED | 此容器何时创建的 |
STATUS | 此容器的状态 |
PORTS | 宿主机和容器之间的端口映射 |
NAMES | 此容器的名称 |
3.2 启动一个已经停止的容器
docker start bcbf2db9ae6a # 容器 ID或者name
3.3 停止或重启容器
docker stop bcbf2db9ae6a # 容器 ID或者name
3.4 进入/退出容器
docker exec -it bcbf2db9ae6a bash
[root@bcbf2db9ae6a /]# exit
exit
3.5 本地文件和容器文件快速交换(拷贝文件)
docker cp /root/kai.sh bcbf2db9ae6a:/tmp #和scp差不多
源文件路径 目标文件路径
3.6 挂载本地文件/目录到容器
[root@bogon tmp]# docker run -it -v /tmp/1.txt:/tmp/1.txt centos:7
[root@7d83c2094ad3 /]# cd tmp/
[root@7d83c2094ad3 tmp]# ls
1.txt
-v
本地文件路径,这个路径必须是绝对路径
映射目录是一样的操作
3.7 映射容器端口到本地端口
-p
指定端口映射
-d
让容器在后台运行
[root@bogon ~]# docker run -itd -p 8000:80 nginx:latest
e0523e691a7e38342d2d4308447f17b3430d02b39ec91f2381c95ec4d9ea1950
[root@bogon ~]# curl -I 127.0.0.1:8000
HTTP/1.1 200 OK
Server: nginx/1.19.6
Date: Wed, 31 Mar 2021 13:12:52 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 15 Dec 2020 13:59:38 GMT
Connection: keep-alive
ETag: "5fd8c14a-264"
Accept-Ranges: bytes
3.8 给容器名定义名字
--name
给容器定义名字
[root@bogon ~]# docker run -itd --name centps7-1 centos:7
32ddb0f4cef0df82f5e8e8b15c1de50ee4844b211fabc56f22bcdcfb6adef065
[root@bogon ~]# docker exec -it centps7-1 bash
[root@32ddb0f4cef0 /]#
3.9 删除已经停止的容器
[root@bogon ~]# docker rm -f centos7-1 #强制删除 不加-f只能删除不在运行的容器
centos7-1
3.10 查看元数据
docker inspect 容器名称/逻辑卷名称/镜像名称
3.11 查看日志
docker logs 容器名
三、镜像操作
1 下载镜像到本地
[root@bogon ~]$ docker pull redis
Using default tag: latest
latest: Pulling from library/redis
d121f8d1c412: Already exists
2f9874741855: Pull complete
d92da09ebfd4: Pull complete
bdfa64b72752: Pull complete
e748e6f663b9: Pull complete
eb1c8b66e2a1: Pull complete
Digest: sha256:1cfb205a988a9dae5f025c57b92e9643ec0e7ccff6e66bc639d8a5f95bba928c
Status: Downloaded newer image for redis:latest
docker.io/library/redis:latest
2 删除本地镜像源
#语法: docker rmi 镜像名称
[root@bogon ~]# docker rmi nginx:22
Untagged: nginx:22
Deleted: sha256:1b495ac58a35e016ae329c839ec9de49e5ad6efe7e3f1efb31b3ae8754fdde01
Deleted: sha256:3be80929735b9db2154a23458766a67e86d28f0604ed0d6f334f843b85cefd64
[root@bogon ~]#
3 Dockerfile 制作镜像
这里只是粗略写一下,具体dockerfile
操作请去我主页找
定义 Dockerfile
[root@bogon centos-vim]# vim Dockerfile
[root@bogon centos-vim]# cat Dockerfile
FROM centos:7
RUN yum install -y vim
[root@bogon centos-vim]# docker build -t centos:vim .
指令介绍:
FORM:定义一个基础镜像
RUN:执行命令行的命令
四、Docker 镜像管理
1 保存本地镜像到本地文件
1.1 保存镜像
#语法: docker save -o 压缩包名 镜像名
docker save -o centos7.tar centos:7
docker save -o centos7.tar nginx:latest centos-nginx.tar #保存多个镜像
1.2 使用镜像
#语法: docker load -i 压缩包名
docker load -i centos7.tar
2 给目前的容器制作快照
2.1 创建
#语法: docker export -o 压缩包名 容器ID/名称
docker export -o centos7.tar df7fe59a73a7 #创建快照
2.2 恢复
#语法: docker import 压缩包名 定义容器名称
docker import centos7.tar centos7 #恢复快照