镜像加速器
国内从 docker hub 拉取镜像有时会遇到困难,此时可以配置镜像加速器。国内很多云服务商都提供了国内加速器服务,例如:
网易云加速器 https://hub-mirror.c.163.com
阿里云加速器(需登录账号获取): https://cr.console.aliyun.com/cn-hangzhou/mirrors
国内各大云服务商均提供了 docker 镜像加速服务,建议根据运行 docker 的云平台选择对应的镜像加速服务,具体请参考官方文档。
在centos7系统,请在 /etc/docker/daemon.json 中写入如下内容(如果文件不存在请新建该文件)
1
2
3
4
5
6
|
[root@docker01 ~] # vim /etc/docker/daemon.json { "registry-mirrors" : [ "https://hub-mirror.c.163.com" ] } |
注意,一定要保证该文件符合 json 规范,否则 docker 将不能启动。
之后重新启动服务。
1
2
|
systemctl daemon-reload systemctl restart docker |
检查加速器是否生效
执行如下命令,如果从结果中看到了如下内容,说明配置成功。
1
2
3
4
5
6
7
8
|
[root@docker01 ~] # docker info # 显示整个系统的信息 ……………… registry mirrors: https: //hub-mirror .c.163.com/ live restore enabled: false warning: bridge-nf-call-iptables is disabled warning: bridge-nf-call-ip6tables is disabled |
dcoker镜像操作
说明:docker 运行容器前需要本地存在对应的镜像,如果本地不存在该镜像,docker 会从镜像仓库下载该镜像。
search搜索镜像
1
2
3
4
5
6
7
8
|
[root@docker01 ~] # docker search centos name description stars official automated centos the official build of centos. 5934 [ok] ansible /centos7-ansible ansible on centos7 128 [ok] jdeathe /centos-ssh openssh / supervisor / epel /ius/scl repos - … 114 [ok] consol /centos-xfce-vnc centos container with "headless" vnc session… 114 [ok] centos /mysql-57-centos7 mysql 5.7 sql database server 74 ………… |
pull从镜像中心下载镜像
1
2
3
4
5
6
|
# 格式:docker pull <image_name>:<tag> ,如果没有tag,默认为 latest [root@docker01 ~] # docker pull centos:latest latest: pulling from library /centos 8a29a15cefae: pull complete digest: sha256:fe8d824220415eed5477b63addf40fb06c3b049404242b31982106ac204f6700 status: downloaded newer image for centos:latest |
push推送镜像到镜像中心
1
2
|
格式:docker push <image_name>:<tag> [root@docker01 ~] # docker push registry.cn-beijing.aliyuncs.com/google_registry/centos:latest |
说明:如果有疑问可先忽略,后面搭建私有仓库文章会再次说明的。
images列出镜像
1
2
3
|
[root@docker01 ~] # docker images # 或者 docker image ls repository tag image id created size centos latest 470671670cac 2 months ago 237mb |
save镜像保存到本地
1
2
3
4
5
|
# 格式:docker save -o <保存的文件名> <image_name:tag>|<image_id> [root@docker01 docker_test] # docker save -o centos_docker_20200413.tar centos:latest [root@docker01 docker_test] # ll -h total 234m -rw------- 1 root root 234m apr 13 16:21 centos_docker_20200413. tar |
rmi删除镜像
1
2
3
4
5
6
7
|
# 格式:docker rmi <image_name:tag>|<image_id> [root@docker01 docker_test] # docker images repository tag image id created size centos latest 470671670cac 2 months ago 237mb [root@docker01 docker_test] # docker rmi 470671670cac # 删除镜像 [root@docker01 docker_test] # docker images repository tag image id created size |
load导入镜像
1
2
3
4
5
|
# 格式:docker load -i <image_file> [root@docker01 docker_test] # docker load -i centos_docker_20200413.tar [root@docker01 docker_test] # docker images repository tag image id created size centos latest 470671670cac 2 months ago 237mb |
tag标签
1
2
3
4
5
6
7
8
9
|
# 格式:docker tag source_image[:tag] target_image[:tag] [root@docker01 docker_test] # docker images repository tag image id created size centos latest 470671670cac 2 months ago 237mb [root@docker01 docker_test] # docker tag centos:latest centos:20200413 [root@docker01 docker_test] # docker images repository tag image id created size centos 20200413 470671670cac 2 months ago 237mb centos latest 470671670cac 2 months ago 237mb |
使用:根据需要给docker镜像打一个新标签。
info显示整个系统的信息
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
[root@docker01 ~] # docker info containers: 0 running: 0 paused: 0 stopped: 0 images: 1 server version: 18.06.3-ce storage driver: overlay2 backing filesystem: xfs supports d_type: true native overlay diff : true logging driver: json- file cgroup driver: cgroupfs plugins: volume: local network: bridge host macvlan null overlay log: awslogs fluentd gcplogs gelf journald json- file logentries splunk syslog swarm: inactive runtimes: runc default runtime: runc init binary: docker-init containerd version: 468a545b9edcd5932818eb9de8e72413e616e86e runc version: a592beb5bc4c4092b1b1bac971afed27687340c5 init version: fec3683 security options: seccomp profile: default kernel version: 3.10.0-1062.el7.x86_64 operating system: centos linux 7 (core) ostype: linux architecture: x86_64 cpus: 2 total memory: 1.777gib name: docker01 id : xihu:xnwu:ii7a:yxuh:boz3:jsgg:j3p2:cu2z:5qha:5y64:pz4v:62di docker root dir : /var/lib/docker debug mode (client): false debug mode (server): false registry: https: //index .docker.io /v1/ labels: experimental: false insecure registries: 127.0.0.0 /8 registry mirrors: https: //hub-mirror .c.163.com/ live restore enabled: false warning: bridge-nf-call-iptables is disabled warning: bridge-nf-call-ip6tables is disabled |
system镜像体积查看
另外一个需要注意的问题是,docker image ls 列表中的镜像体积总和并非是所有镜像实际硬盘消耗。由于 docker 镜像是多层存储结构,并且可以继承、复用,因此不同镜像可能会因为使用相同的基础镜像,从而拥有共同的层。由于 docker 使用 union fs,相同的层只需要保存一份即可,因此实际镜像硬盘占用空间很可能要比这个列表镜像大小的总和要小的多。
可以通过以下命令来便捷的查看镜像、容器、数据卷所占用的空间。
1
2
3
4
5
6
|
[root@docker01 docker_test] # docker system df type total active size reclaimable images 2 0 440.1mb 440.1mb (100%) containers 0 0 0b 0b local volumes 0 0 0b 0b build cache 0 0 0b 0b |
inspect显示镜像或容器的详情
1
2
3
|
# 格式:docker inspect <镜像id>|<镜像名>|<容器id>|<容器名> [root@docker01 ~] # docker inspect centos:latest # 显示镜像详情 [root@docker01 ~] # docker inspect 67ba647b0151 # 显示容器详情 |
dcoker容器操作
run创建容器
1
2
3
4
5
|
# 格式:docker run [options] image [command] [arg...] [root@docker01 ~] # docker run -i -t --name centos01 centos:latest /bin/bash [root@f7c4da3cecad /] # # 此时已进入docker容器 [root@f7c4da3cecad /] # exit # 退出容器,此时容器会停止【正常情况】 [root@docker01 ~] # |
参数说明:
-i 交互式操作
-t 分配一个终端
--name 运行的容器名称
最后的/bin/bash 要执行的命令
ps查看容器
1
2
3
4
5
6
|
[root@docker01 ~] # docker ps # 查看正在运行的容器 container id image command created status ports names [root@docker01 ~] # [root@docker01 ~] # docker ps -a # 查看所有容器,包括运行和停止的 container id image command created status ports names f7c4da3cecad centos:latest "/bin/bash" 4 minutes ago exited (0) 36 seconds ago centos01 |
start启动容器
1
2
3
4
5
6
7
8
9
|
# 格式:docker start <容器名>|<容器id> [root@docker01 ~] # docker ps -a container id image command created status ports names f7c4da3cecad centos:latest "/bin/bash" 10 minutes ago exited (0) 3 seconds ago centos01 [root@docker01 ~] # docker start f7c4da3cecad # 启动容器 f7c4da3cecad [root@docker01 ~] # docker ps container id image command created status ports names f7c4da3cecad centos:latest "/bin/bash" 10 minutes ago up 3 seconds centos01 |
restart重启容器
1
2
3
4
5
6
7
8
9
|
# 格式:docker restart <容器名>|<容器id> [root@docker01 ~] # docker ps container id image command created status ports names f7c4da3cecad centos:latest "/bin/bash" 15 minutes ago up 4 minutes centos01 [root@docker01 ~] # docker restart f7c4da3cecad # 重启容器 f7c4da3cecad [root@docker01 ~] # docker ps container id image command created status ports names f7c4da3cecad centos:latest "/bin/bash" 15 minutes ago up 1 second centos01 |
stop停止容器
1
2
3
4
5
6
7
8
9
|
# 格式:docker stop <容器名>|<容器id> [root@docker01 ~] # docker ps container id image command created status ports names f7c4da3cecad centos:latest "/bin/bash" 3 hours ago up 4 minutes centos01 [root@docker01 ~] # docker stop f7c4da3cecad # 停止容器 f7c4da3cecad [root@docker01 ~] # docker ps -a container id image command created status ports names f7c4da3cecad centos:latest "/bin/bash" 3 hours ago exited (0) 10 seconds ago centos01 |
rm删除容器
1
2
3
4
5
6
|
# 格式:docker rm <容器名>|<容器id> [root@docker01 ~] # docker ps -a container id image command created status ports names f7c4da3cecad centos:latest "/bin/bash" 3 hours ago exited (0) 10 seconds ago centos01 [root@docker01 ~] # docker rm f7c4da3cecad # 删除已停止的容器 f7c4da3cecad |
说明:如果要强制删除正在运行的容器,使用 docker rm -f <容器id>。不过生产环境不建议强制删除容器,防止误删除。
rename容器重命名
1
2
3
4
5
6
7
8
|
# 格式:docker rename container new_name [root@docker01 ~] # docker ps container id image command created status ports names 67ba647b0151 centos:latest "/bin/bash" about a minute ago up about a minute centos01 [root@docker01 ~] # docker rename 67ba647b0151 centos001 # 容器重命名 [root@docker01 ~] # docker ps container id image command created status ports names 67ba647b0151 centos:latest "/bin/bash" 2 minutes ago up about a minute centos001 |
exec进入容器或在运行容器中执行命令
进入容器
1
2
|
[root@docker01 ~] # docker exec -it f7c4da3cecad bash [root@f7c4da3cecad /] # |
说明:不建议通过 docker attach 方式进入容器。
在容器外让指定容器执行命令
1
2
3
4
5
|
# 不要有 -t 选项,因为不需要分配 tty 终端 [root@docker01 ~] # docker exec -i f7c4da3cecad bash -c "ps -ef" uid pid ppid c stime tty time cmd root 1 0 0 10:12 pts /0 00:00:00 /bin/bash root 78 0 0 10:19 ? 00:00:00 ps -ef |
cp复制文件或目录
将宿主机文件或目录,拷贝到docker容器中
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
[root@docker01 ~] # docker exec -i 67ba647b0151 bash -c "ls -l /root" total 12 -rw------- 1 root root 2366 jan 13 21:49 anaconda-ks.cfg -rw-r--r-- 1 root root 435 jan 13 21:49 anaconda-post.log -rw------- 1 root root 2026 jan 13 21:49 original-ks.cfg [root@docker01 ~] # [root@docker01 ~] # docker cp /usr/bin/telnet 67ba647b0151:/root/ # 拷贝文件 [root@docker01 ~] # docker cp /root/basedoptimi 67ba647b0151:/root/ # 拷贝目录 [root@docker01 ~] # docker exec -i 67ba647b0151 bash -c "ls -l /root" total 112 -rw------- 1 root root 2366 jan 13 21:49 anaconda-ks.cfg -rw-r--r-- 1 root root 435 jan 13 21:49 anaconda-post.log drwxr-xr-x 2 root root 30 mar 8 19:59 basedoptimi -rw------- 1 root root 2026 jan 13 21:49 original-ks.cfg -rwxr-xr-x 1 root root 101776 aug 3 2017 telnet |
将docker容器中的文件或目录,拷贝到宿主机中
1
2
|
[root@docker01 ~] # docker cp 67ba647b0151:/root/original-ks.cfg /root/ # 拷贝文件 [root@docker01 ~] # docker cp 67ba647b0151:/etc /root/ # 拷贝目录 |
logs查看容器日志
1
2
3
4
5
6
|
[root@docker01 ~] # docker ps container id image command created status ports names f7c4da3cecad centos:latest "/bin/bash" 3 hours ago up 1 second centos01 # 获取容器日志 [root@docker01 ~] # docker logs -f --tail 500 f7c4da3cecad ………… |
参数说明:
-f 持续打印输出
--tail 500 打印日志最后的500行
stats容器使用资源统计
可用于监控
1
2
|
[root@docker01 ~] # docker stats <容器id>|<容器名称> # 持续监控 [root@docker01 ~] # docker stats --no-stream <容器id>|<容器名称> # 不是持续监控,只显示第一次返回的结果 |
top容器中运行的进程
1
2
3
4
5
6
7
|
[root@docker01 ~] # docker ps container id image command created status ports names 67ba647b0151 centos:latest "/bin/bash" 2 hours ago up 2 hours centos001 # 查看容器中运行的进程信息 [root@docker01 ~] # docker top 67ba647b0151 uid pid ppid c stime tty time cmd root 3302 3285 0 21:13 pts /0 00:00:00 /bin/bash |
port容器映射特定端口
容器映射端口有:随机端口映射、指定单个端口映射、指定多个端口映射
获取镜像
1
2
3
4
5
|
[root@docker01 ~] # docker pull registry.cn-beijing.aliyuncs.com/google_registry/nginx:1.17 [root@docker01 ~] # docker tag ed21b7a8aee9 nginx:1.17 [root@docker01 ~] # docker images | grep 'nginx' nginx 1.17 ed21b7a8aee9 2 weeks ago 127mb registry.cn-beijing.aliyuncs.com /google_registry/nginx 1.17 ed21b7a8aee9 2 weeks ago 127mb |
映射随机端口
1
2
3
4
5
|
[root@docker01 ~] # docker run -d -p --name nginx01 nginx:1.17 e90c9faaf8e3387920dd9763bf5c7df9dd17856673868bb512cec78741ff71dc [root@docker01 ~] # docker ps container id image command created status ports names e90c9faaf8e3 nginx:1.17 "nginx -g 'daemon of…" 3 seconds ago up 2 seconds 0.0.0.0:1025->80 /tcp nginx01 |
说明:
0.0.0.0:1025->80/tcp 前面为宿主机端口,后面为容器端口
容器日志:
1
|
[root@docker01 ~] # docker logs -f --tail 500 nginx01 |
浏览器访问:
映射单个指定端口
1
2
3
4
|
[root@docker01 ~] # docker run -d -p 81:80 --name nginx02 nginx:1.17 04478222f0dc981883f25504164be3af7da49248886cee7386ccc89b80cc57a1 [root@docker01 ~] # docker ps | grep 'nginx02' 04478222f0dc nginx:1.17 "nginx -g 'daemon of…" 29 seconds ago up 28 seconds 0.0.0.0:81->80 /tcp nginx02 |
浏览器访问:
映射多个指定端口
1
2
3
4
|
[root@docker01 ~] # docker run -d -p 85:80 -p 445:443 --name nginx03 nginx:1.17 5886e52ff8e934bc827c8d7753a532b9062bd045799d0658a008e371e6ecd09c [root@docker01 ~] # docker ps | grep 'nginx03' 5886e52ff8e9 nginx:1.17 "nginx -g 'daemon of…" 12 seconds ago up 11 seconds 0.0.0.0:85->80 /tcp , 0.0.0.0:445->443 /tcp nginx03 |
推荐阅读
1、 docker简介与安装
到此这篇关于详解docker镜像与容器的常见操作的文章就介绍到这了,更多相关docker镜像与容器操作内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:http://www.zhangblog.com/2020/04/25/docker02/