文章目录
- 一、Docker 安装
- 二、Docker基本命令
- 2.1 查询docker版本信息
- 2.2 docker镜像加速与网络优化
- 2.3 镜像操作
- 2.3.1 镜像搜索命令
- 2.3.2 下载nginx镜像
- 2.3.3 查看镜像
- 2.3.4 添加镜像标签
- 2.3.5 镜像输出与导入
- 2.3.6 常用镜像命令小结
- 2.4 容器操作基本命令
- 2.4.1 容器列表查询
- 2.4.2 创建容器
- 2.4.3 启动容器
- 2.4.4 一次性执行容器操作
- 2.4.5 进入容器
- 2.4.6 容器删除
- 2.4.7 Docker容器命令小结
一、Docker 安装
- 准备实验环境
[root@docker ~]# systemctl stop firewalld && systemctl disable firewalld
[root@docker ~]# setenforce 0
[root@docker ~]# sed -i "7s/enforcing/disabled/" /etc/selinux/config //关闭核心防护
- 安装docker引擎依赖包
[root@docker ~]# yum install -y yum-utils device-mapper-persistent-data lvm2 libsemanage-python libselinux-utils
- 添加Docker软件包源
[root@docker ~]# cd /etc/yum.repos.d/
[root@docker yum.repos.d]# yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
//这里我们设置阿里云镜像源,只有在下载完docker-ce.repo配置文件后,才可以正常安装Docker-CE(社区版Docker引擎)
- 安装Docker-CE引擎,社区版
[root@docker ~]# yum install -y docker-ce
- 开启Docker,并设置为开启自启
[root@docker ~]# systemctl start docker.service && systemctl enable docker.service
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
二、Docker基本命令
2.1 查询docker版本信息
[root@docker ~]# docker version
Client: Docker Engine - Community
Version: 20.10.5
API version: 1.41
Go version: go1.13.15
Git commit: 55c4c88
Built: Tue Mar 2 20:33:55 2021
OS/Arch: linux/amd64
Context: default
Experimental: true
Server: Docker Engine - Community
Engine:
Version: 20.10.5
API version: 1.41 (minimum version 1.12)
Go version: go1.13.15
Git commit: 363e9a8
Built: Tue Mar 2 20:32:17 2021
OS/Arch: linux/amd64
Experimental: false
containerd:
Version: 1.4.4
GitCommit: 05f951a3781f4f2c1911b05e61c160e9c30eaa8e
runc:
Version: 1.0.0-rc93
GitCommit: 12644e614e25b05da6fd08a38ffa0cfe1903fdec
docker-init:
Version: 0.19.0
GitCommit: de40ad0
2.2 docker镜像加速与网络优化
- 镜像加速注册地址:https://help.aliyun.com/document_detail/60750.html
- 镜像加速加载完成后需要重启进程和docker,因为docker是以守护进程的方式在后台进行工作的
[root@docker ~]# systemctl daemon-reload && systemctl restart docker
- 网络优化
[root@docker ~]# echo "net.ipv4.ip_forward=1" >> /etc/sysctl.conf
[root@docker ~]# sysctl -p
net.ipv4.ip_forward = 1
[root@docker ~]# service network restart
Restarting network (via systemctl): [ 确定 ]
[root@docker ~]# systemctl restart docker
2.3 镜像操作
2.3.1 镜像搜索命令
- 格式:
dcoker search 关键字
- 示例:
[root@docker ~]# docker search nginx
2.3.2 下载nginx镜像
- 格式 :
docker pull 关键字
- 示例:
[root@docker ~]# docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
a076a628af6f: Pull complete
0732ab25fa22: Pull complete
d7f36f6fe38f: Pull complete
f72584a26f32: Pull complete
7125e4df9063: Pull complete
Digest: sha256:10b8cc432d56da8b61b070f4c7d2543a9ed17c2b23010b43af434fd40e2ca4aa
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
- 注意:
下载后存放在以下路径:/var/lib/docker/image/overlay2/repositories.json
[root@docker ~]# cat /var/lib/docker/image/overlay2/repositories.json
2.3.3 查看镜像
- 格式:
docker images
- 示例:
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest f6d0b4767a6c 8 weeks ago 133MB
- 查看镜像信息
格式:docker inspect 镜像ID
[root@docker ~]# docker inspect f6d0b4767a6c //借助IMAGE ID标签 详细查看镜像信息
[
{
"Id": "sha256:f6d0b4767a6c466c178bf718f99bea0d3742b26679081e52dbf8e0c7c4c42d74",
"RepoTags": [
"nginx:latest"
],
"RepoDigests": [
"nginx@sha256:10b8cc432d56da8b61b070f4c7d2543a9ed17c2b23010b43af434fd40e2ca4aa"
],
'//省略部分信息'
2.3.4 添加镜像标签
- 格式:
docker tag 镜像标签 新标签
- 示例:
[root@docker ~]# docker tag nginx:latest nginx:web //添加标签web
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest f6d0b4767a6c 8 weeks ago 133MB
nginx web f6d0b4767a6c 8 weeks ago 133MB
- 删除镜像
格式:docker rmi 镜像标签
[root@docker ~]# docker rmi nginx:web
Untagged: nginx:web
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest f6d0b4767a6c 8 weeks ago 133MB
//注意此处有两个相同的镜像,所以删除的只是别名;如果把最后一个也删除,那镜像也会被删除
2.3.5 镜像输出与导入
- 镜像输出
格式:docker save -o 导出的文件名 镜像标签
[root@docker ~]# cd /opt/
[root@docker opt]# docker save -o nginx nginx:latest //镜像输出
[root@docker opt]# ls
containerd nginx rh
- 镜像导入
格式:docker load < 镜像文件
示例:
[root@docker opt]# docker rmi f6d0b4767a6c //先删除镜像
Untagged: nginx:latest
Untagged: nginx@sha256:10b8cc432d56da8b61b070f4c7d2543a9ed17c2b23010b43af434fd40e2ca4aa
Deleted: sha256:f6d0b4767a6c466c178bf718f99bea0d3742b26679081e52dbf8e0c7c4c42d74
Deleted: sha256:4dfe71c4470c5920135f00af483556b09911b72547113512d36dc29bfc5f7445
Deleted: sha256:3c90a0917c79b758d74b7040f62d17a7680cd14077f734330b1994a2985283b8
Deleted: sha256:a1c538085c6f891424160d8db120ea093d4dda393e94cd4713e3fff3c82299b5
Deleted: sha256:a3ee2510dcf02c980d7aff635909612006fd1662084d6225e52e769b984abeb5
Deleted: sha256:cb42413394c4059335228c137fe884ff3ab8946a014014309676c25e3ac86864
[root@docker opt]# docker images //查看镜像是否删除
REPOSITORY TAG IMAGE ID CREATED SIZE
[root@docker opt]# docker load < nginx //再导入镜像
cb42413394c4: Loading layer [==================================================>] 72.51MB/72.51MB
1c91bf69a08b: Loading layer [==================================================>] 64.6MB/64.6MB
56bc37de0858: Loading layer [==================================================>] 3.072kB/3.072kB
3e5288f7a70f: Loading layer [==================================================>] 4.096kB/4.096kB
85fcec7ef3ef: Loading layer [==================================================>] 3.584kB/3.584kB
Loaded image: nginx:latest
[root@docker opt]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest f6d0b4767a6c 8 weeks ago 133MB
//导入成功
2.3.6 常用镜像命令小结
镜像搜索:
docker search 关键字(nginx)
镜像下载:
docker pull 关键字(nginx)
镜像查询:
docker images
查看镜像详细信息:
docker inspect 镜像ID
添加镜像标签:
docker tag 镜像标签 新标签
docker tag nginx:latest nginx:web
删除镜像:
docker rmi 镜像标签(nginx:latest)
镜像输出:
docker save -o nginx_img nginx:latest
镜像导入:
docker load <镜像文件(nginx_img)
2.4 容器操作基本命令
2.4.1 容器列表查询
- 格式:
docker ps -a (-a 列出最近一次启动的容器)
- 示例:
[root@docker ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2.4.2 创建容器
- 格式:
docker create -it 选项 /bin/bash
-i 让容器的标准输入保持打开
-t 让Docker分配一个伪终端
- 示例:
[root@docker ~]# docker create nginx:latest /bin/bash //创建容器
bf659453c870227ef2763c6f74284396f89097ce067f26db1d838241b88eb0b1
[root@docker ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bf659453c870 nginx:latest "/docker-entrypoint.…" 8 seconds ago Created elegant_galois
参数解释:
/bin/bash 指定终端
statys :created 创建状态,暂时还未开启
container id 容器ID
2.4.3 启动容器
- 格式:
docker start 容器ID
- 示例:
[root@docker ~]# docker start bf659453c870
bf659453c870
[root@docker ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bf659453c870 nginx:latest "/docker-entrypoint.…" 3 minutes ago Exited (0) 7 seconds ago elegant_galois
2.4.4 一次性执行容器操作
- 格式:
docker run 镜像名称 操作指令
- 示例:
[root@docker ~]# docker run centos:7 /usr/bin/bash -c ls /
#docker run 一次性运行centos:7镜像(如在本地未发现镜像会自动在容器仓库中下载该镜像)
-c (command指令,进入到centos:7容器) ls / (执行ls /命令)
- 如果不希望执行完命令后结束进程,可以给一条循环语句命令,如下:
docker run -d centos:7 /bin/bash -c "while true;do echo hello;done"
2.4.5 进入容器
- 格式:
docker run -it 容器名 容器环境
在容器为运行状态下:docker exec -it 容器ID 容器环境
[root@docker ~]# docker exec --help //列出 docker exec 相关参数
Usage: docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
Run a command in a running container
Options:
-d, --detach Detached mode: run command in the background
--detach-keys string Override the key sequence for detaching a container
-e, --env list Set environment variables
--env-file list Read in a file of environment variables
-i, --interactive Keep STDIN open even if not attached
--privileged Give extended privileges to the command
-t, --tty Allocate a pseudo-TTY
-u, --user string Username or UID (format: <name|uid>[:<group|gid>])
-w, --workdir string Working directory inside the container
- 示例:
[root@docker ~]# docker run -it nginx:latest /bin/bash
root@ae3b094a198d:/# exit
exit
[root@docker ~]#
- 容器导出、导入
容器导出:
docker export 容器ID > 导出的容器名
容器导入:
cat 容器名 | docker import - 镜像名 (生成镜像,不会生成容器)
示例:
[root@docker ~]# docker export bf659453c870 > centos_c
[root@docker ~]# ls
anaconda-ks.cfg container-selinux-2.119.2-1.911c772.el7_8.noarch.rpm 公共 视频 文档 音乐
centos_c initial-setup-ks.cfg 模板 图片 下载 桌面
2.4.6 容器删除
- 格式:
docker rm 容器ID (必须先停止再删除,删除正在运行的容器添加 -f 参数)
停止容器有2种方式:
docker stop 容器名或容器id
docker kill 容器名或容器id
- 查看 docker rm 相关参数
[root@docker ~]# docker rm --help
Usage: docker rm [OPTIONS] CONTAINER [CONTAINER...]
Remove one or more containers
Options:
-f, --force Force the removal of a running container (uses SIGKILL)
-l, --link Remove the specified link
-v, --volumes Remove anonymous volumes associated with the container
- 示例:
[root@docker ~]# docker ps -a //查看容器列表
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ae3b094a198d nginx:latest "/docker-entrypoint.…" 6 minutes ago Exited (0) 6 minutes ago kind_robinson
eac2f146ef01 centos:7 "/usr/bin/bash -c ls…" 38 minutes ago Exited (0) 38 minutes ago sleepy_liskov
bf659453c870 nginx:latest "/docker-entrypoint.…" 48 minutes ago Exited (0) 44 minutes ago elegant_galois
[root@docker ~]# docker rm bf659453c870 //删除容器
bf659453c870
[root@docker ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ae3b094a198d nginx:latest "/docker-entrypoint.…" 7 minutes ago Exited (0) 7 minutes ago kind_robinson
eac2f146ef01 centos:7 "/usr/bin/bash -c ls…" 39 minutes ago Exited (0) 39 minutes ago sleepy_liskov
注意:
- 如果想批量删除容器,可借助正则表达式
docker ps -a | awk '{print "docker rm " $1}' | bash
#遍历容器ID,依次删除,但如果有UP状态的容器则会报错
补充命令:
退出容器
control + d
查看日志
docker logs -f 容器名或id
2.4.7 Docker容器命令小结
- 查看、停止、删除容器命令小结
1.列出所有容器 ID
docker ps -aq
2.停止所有容器
docker stop $(docker ps -aq)
3.开启所有容器
docker start $(docker ps -aq)
4.开启/停止单个容器
docker start/stop 要停止的容器名
5.删除所有容器
docker rm $(docker ps -aq)
6.删除所有 Exited 状态的容器
docker rm $(docker ps -a | grep "Exited" | awk '{print $1 }')
7.删除单个容器
docker rm 要删除的容器名
8.删除所有的镜像
docker rmi $(docker images -q)
9.删除所有 none 状态的镜像
docker rmi $(docker images | grep "none" | awk '{print $3}')
- 其他常用命令
容器列表状态查询
docker ps -a
创建容器
docker create -it 容器名 环境
docker create -it nginx:latest /bin/bash
一次性容器操作
docker run 镜像名称 操作指令
docker run centos:7 /usr/bin/bash -c ls /
进入容器
docker run -it 容器名 环境
docker run -it nginx:latest /bin/bash
容器导入
cat 容器ID | docker import -镜像名 (会生成镜像,不会生成容器)
容器导出
docker export 容器ID > 导出的容器名
容器删除
docker rm 容器ID (必须先停止再删除)