欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Docker镜像与容器的常用操作

程序员文章站 2022-06-13 10:45:34
...

Docker镜像加速配置;Docker镜像常用操作;Dcoker容器常用操作。

镜像加速器

国内从 Docker Hub 拉取镜像有时会遇到困难,此时可以配置镜像加速器。国内很多云服务商都提供了国内加速器服务,例如:

网易云加速器 https://hub-mirror.c.163.com
阿里云加速器(需登录账号获取): https://cr.console.aliyun.com/cn-hangzhou/mirrors

国内各大云服务商均提供了 Docker 镜像加速服务,建议根据运行 Docker 的云平台选择对应的镜像加速服务,具体请参考官方文档。

在CentOS7系统,请在 /etc/docker/daemon.json 中写入如下内容(如果文件不存在请新建该文件)

[aaa@qq.com ~]# vim /etc/docker/daemon.json
{
  "registry-mirrors": [
    "https://hub-mirror.c.163.com"
  ]
}

注意,一定要保证该文件符合 json 规范,否则 Docker 将不能启动。

之后重新启动服务。

systemctl daemon-reload
systemctl restart docker

检查加速器是否生效

执行如下命令,如果从结果中看到了如下内容,说明配置成功。

[aaa@qq.com ~]# 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

Docker镜像操作

说明:Docker 运行容器前需要本地存在对应的镜像,如果本地不存在该镜像,Docker 会从镜像仓库下载该镜像。

search搜索镜像

[aaa@qq.com ~]# 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                
…………

Docker镜像与容器的常用操作

pull从镜像中心下载镜像

# 格式:docker pull <image_name>:<tag> ,如果没有tag,默认为 latest
[aaa@qq.com ~]# docker pull centos:latest
latest: Pulling from library/centos
8a29a15cefae: Pull complete 
Digest: sha256:fe8d824220415eed5477b63addf40fb06c3b049404242b31982106ac204f6700
Status: Downloaded newer image for centos:latest

push推送镜像到镜像中心

格式:docker push <image_name>:<tag>
[aaa@qq.com ~]# docker push registry.cn-beijing.aliyuncs.com/google_registry/centos:latest

说明:如果有疑问可先忽略,后面搭建私有仓库文章会再次说明的。

images列出镜像

[aaa@qq.com ~]# docker images  # 或者 docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              latest              470671670cac        2 months ago        237MB

save镜像保存到本地

# 格式:docker save -o <保存的文件名>  <image_name:tag>|<image_id>
[aaa@qq.com docker_test]# docker save -o centos_docker_20200413.tar centos:latest 
[aaa@qq.com docker_test]# ll -h
total 234M
-rw------- 1 root root 234M Apr 13 16:21 centos_docker_20200413.tar

rmi删除镜像

# 格式:docker rmi <image_name:tag>|<image_id>
[aaa@qq.com docker_test]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              latest              470671670cac        2 months ago        237MB
[aaa@qq.com docker_test]# docker rmi 470671670cac  # 删除镜像 
[aaa@qq.com docker_test]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE

load导入镜像

# 格式:docker load -i <image_file>
[aaa@qq.com docker_test]# docker load -i centos_docker_20200413.tar
[aaa@qq.com docker_test]# docker images 
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              latest              470671670cac        2 months ago        237MB

tag标签

# 格式:docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]
[aaa@qq.com docker_test]# docker images 
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
centos              latest              470671670cac        2 months ago        237MB 
[aaa@qq.com docker_test]# docker tag centos:latest centos:20200413
[aaa@qq.com 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显示整个系统的信息

[aaa@qq.com ~]# 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,相同的层只需要保存一份即可,因此实际镜像硬盘占用空间很可能要比这个列表镜像大小的总和要小的多。

可以通过以下命令来便捷的查看镜像、容器、数据卷所占用的空间。

[aaa@qq.com 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显示镜像或容器的详情

# 格式:docker inspect <镜像ID>|<镜像名>|<容器ID>|<容器名>
[aaa@qq.com ~]# docker inspect centos:latest  # 显示镜像详情
[aaa@qq.com ~]# docker inspect 67ba647b0151   # 显示容器详情

Docker容器操作

run创建容器

# 格式:docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
[aaa@qq.com ~]# docker run -i -t --name centos01 centos:latest /bin/bash
[aaa@qq.com /]#       # 此时已进入docker容器
[aaa@qq.com /]# exit  # 退出容器,此时容器会停止【正常情况】
[aaa@qq.com ~]#

参数说明:

-i  交互式操作
-t  分配一个终端
--name  运行的容器名称
最后的/bin/bash  要执行的命令

ps查看容器

[aaa@qq.com ~]# docker ps  # 查看正在运行的容器
CONTAINER ID   IMAGE           COMMAND        CREATED        STATUS                    PORTS    NAMES
[aaa@qq.com ~]# 
[aaa@qq.com ~]# 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启动容器

# 格式:docker start <容器名>|<容器ID>
[aaa@qq.com ~]# 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
[aaa@qq.com ~]# docker start f7c4da3cecad  # 启动容器
f7c4da3cecad
[aaa@qq.com ~]# docker ps 
CONTAINER ID        IMAGE               COMMAND      CREATED         STATUS        PORTS   NAMES
f7c4da3cecad        centos:latest       "/bin/bash"  10 minutes ago  Up 3 seconds          centos01

restart重启容器

# 格式:docker restart <容器名>|<容器ID>
[aaa@qq.com ~]# docker ps 
CONTAINER ID        IMAGE               COMMAND       CREATED          STATUS         PORTS    NAMES
f7c4da3cecad        centos:latest       "/bin/bash"   15 minutes ago   Up 4 minutes            centos01
[aaa@qq.com ~]# docker restart f7c4da3cecad  # 重启容器
f7c4da3cecad
[aaa@qq.com ~]# docker ps 
CONTAINER ID        IMAGE               COMMAND       CREATED          STATUS         PORTS    NAMES
f7c4da3cecad        centos:latest       "/bin/bash"   15 minutes ago   Up 1 second             centos01

stop停止容器

# 格式:docker stop <容器名>|<容器ID>
[aaa@qq.com ~]# docker ps 
CONTAINER ID        IMAGE           COMMAND       CREATED       STATUS         PORTS     NAMES
f7c4da3cecad        centos:latest   "/bin/bash"   3 hours ago   Up 4 minutes             centos01
[aaa@qq.com ~]# docker stop f7c4da3cecad  # 停止容器
f7c4da3cecad
[aaa@qq.com ~]# 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删除容器

# 格式:docker rm <容器名>|<容器ID>
[aaa@qq.com ~]# 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
[aaa@qq.com ~]# docker rm f7c4da3cecad   # 删除已停止的容器
f7c4da3cecad

说明:如果要强制删除正在运行的容器,使用 docker rm -f <容器ID>。不过生产环境不建议强制删除容器,防止误删除。

rename容器重命名

# 格式:docker rename CONTAINER NEW_NAME
[aaa@qq.com ~]# docker ps 
CONTAINER ID        IMAGE          COMMAND       CREATED              STATUS              PORTS    NAMES
67ba647b0151        centos:latest  "/bin/bash"   About a minute ago   Up About a minute            centos01
[aaa@qq.com ~]# docker rename 67ba647b0151 centos001   # 容器重命名
[aaa@qq.com ~]# docker ps 
CONTAINER ID        IMAGE          COMMAND       CREATED             STATUS              PORTS    NAMES
67ba647b0151        centos:latest  "/bin/bash"   2 minutes ago       Up About a minute            centos001

exec进入容器或在运行容器中执行命令

进入容器
[aaa@qq.com ~]# docker exec -it f7c4da3cecad bash
[aaa@qq.com /]#

说明:不建议通过 docker attach 方式进入容器。

在容器外让指定容器执行命令
# 不要有 -t 选项,因为不需要分配 tty 终端
[aaa@qq.com ~]# 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容器中
[aaa@qq.com ~]# 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
[aaa@qq.com ~]# 
[aaa@qq.com ~]# docker cp /usr/bin/telnet 67ba647b0151:/root/     # 拷贝文件
[aaa@qq.com ~]# docker cp /root/basedOptimi 67ba647b0151:/root/   # 拷贝目录
[aaa@qq.com ~]# 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容器中的文件或目录,拷贝到宿主机中
[aaa@qq.com ~]# docker cp 67ba647b0151:/root/original-ks.cfg /root/   # 拷贝文件
[aaa@qq.com ~]# docker cp 67ba647b0151:/etc /root/                    # 拷贝目录

Docker镜像与容器的常用操作

logs查看容器日志

[aaa@qq.com ~]# docker ps 
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
f7c4da3cecad        centos:latest       "/bin/bash"         3 hours ago         Up 1 second                             centos01
# 获取容器日志
[aaa@qq.com ~]# docker logs -f --tail 500 f7c4da3cecad
…………

参数说明:

-f  持续打印输出
--tail 500  打印日志最后的500行

stats容器使用资源统计

可用于监控

[aaa@qq.com ~]# docker stats <容器ID>|<容器名称>   # 持续监控
[aaa@qq.com ~]# docker stats --no-stream <容器ID>|<容器名称>  # 不是持续监控,只显示第一次返回的结果

Docker镜像与容器的常用操作

top容器中运行的进程

[aaa@qq.com ~]# docker ps 
CONTAINER ID   IMAGE           COMMAND       CREATED       STATUS       PORTS   NAMES
67ba647b0151   centos:latest   "/bin/bash"   2 hours ago   Up 2 hours           centos001 
# 查看容器中运行的进程信息
[aaa@qq.com ~]# 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容器映射特定端口

容器映射端口有:随机端口映射、指定单个端口映射、指定多个端口映射

获取镜像
[aaa@qq.com ~]# docker pull registry.cn-beijing.aliyuncs.com/google_registry/nginx:1.17
[aaa@qq.com ~]# docker tag ed21b7a8aee9 nginx:1.17
[aaa@qq.com ~]# 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
映射随机端口
[aaa@qq.com ~]# docker run -d -P --name nginx01 nginx:1.17
e90c9faaf8e3387920dd9763bf5c7df9dd17856673868bb512cec78741ff71dc
[aaa@qq.com ~]# 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  前面为宿主机端口,后面为容器端口

容器日志:

[aaa@qq.com ~]# docker logs -f --tail 500 nginx01

Docker镜像与容器的常用操作

浏览器访问:

Docker镜像与容器的常用操作

映射单个指定端口
[aaa@qq.com ~]# docker run -d -p 81:80 --name nginx02 nginx:1.17
04478222f0dc981883f25504164be3af7da49248886cee7386ccc89b80cc57a1
[aaa@qq.com ~]# 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

浏览器访问:

Docker镜像与容器的常用操作

映射多个指定端口
[aaa@qq.com ~]# docker run -d -p 85:80 -p 445:443 --name nginx03 nginx:1.17
5886e52ff8e934bc827c8d7753a532b9062bd045799d0658a008e371e6ecd09c
[aaa@qq.com ~]# 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简介与安装

完毕!


———END———
如果觉得不错就关注下呗 (-^O^-) !

Docker镜像与容器的常用操作