标签:mysql mon oca stop version alt ade 结构 指定端口
原文:Docker镜像与容器的常用操作
Docker镜像加速配置;Docker镜像常用操作;Dcoker容器常用操作。
国内从 Docker Hub 拉取镜像有时会遇到困难,此时可以配置镜像加速器。国内很多云服务商都提供了国内加速器服务,例如:
1 网易云加速器 https://hub-mirror.c.163.com 2 阿里云加速器(需登录账号获取): https://cr.console.aliyun.com/cn-hangzhou/mirrors
国内各大云服务商均提供了 Docker 镜像加速服务,建议根据运行 Docker 的云平台选择对应的镜像加速服务,具体请参考官方文档。
在CentOS7系统,请在 /etc/docker/daemon.json 中写入如下内容(如果文件不存在请新建该文件)
1 [root@docker01 ~]# vim /etc/docker/daemon.json 2 { 3 "registry-mirrors": [ 4 "https://hub-mirror.c.163.com" 5 ] 6 }
注意,一定要保证该文件符合 json 规范,否则 Docker 将不能启动。
之后重新启动服务。
1 systemctl daemon-reload 2 systemctl restart docker
执行如下命令,如果从结果中看到了如下内容,说明配置成功。
1 [root@docker01 ~]# docker info # 显示整个系统的信息 2 ……………… 3 Registry Mirrors: 4 https://hub-mirror.c.163.com/ 5 Live Restore Enabled: false 6 7 WARNING: bridge-nf-call-iptables is disabled 8 WARNING: bridge-nf-call-ip6tables is disabled
说明:Docker 运行容器前需要本地存在对应的镜像,如果本地不存在该镜像,Docker 会从镜像仓库下载该镜像。
1 [root@docker01 ~]# docker search centos 2 NAME DESCRIPTION STARS OFFICIAL AUTOMATED 3 centos The official build of CentOS. 5934 [OK] 4 ansible/centos7-ansible Ansible on Centos7 128 [OK] 5 jdeathe/centos-ssh OpenSSH / Supervisor / EPEL/IUS/SCL Repos - … 114 [OK] 6 consol/centos-xfce-vnc Centos container with "headless" VNC session… 114 [OK] 7 centos/mysql-57-centos7 MySQL 5.7 SQL database server 74 8 …………
1 # 格式:docker pull <image_name>:<tag> ,如果没有tag,默认为 latest 2 [root@docker01 ~]# docker pull centos:latest 3 latest: Pulling from library/centos 4 8a29a15cefae: Pull complete 5 Digest: sha256:fe8d824220415eed5477b63addf40fb06c3b049404242b31982106ac204f6700 6 Status: Downloaded newer image for centos:latest
1 格式:docker push <image_name>:<tag> 2 [root@docker01 ~]# docker push registry.cn-beijing.aliyuncs.com/google_registry/centos:latest
说明:如果有疑问可先忽略,后面搭建私有仓库文章会再次说明的。
1 [root@docker01 ~]# docker images # 或者 docker image ls 2 REPOSITORY TAG IMAGE ID CREATED SIZE 3 centos latest 470671670cac 2 months ago 237MB
1 # 格式:docker save -o <保存的文件名> <image_name:tag>|<image_id> 2 [root@docker01 docker_test]# docker save -o centos_docker_20200413.tar centos:latest 3 [root@docker01 docker_test]# ll -h 4 total 234M 5 -rw------- 1 root root 234M Apr 13 16:21 centos_docker_20200413.tar
1 # 格式:docker rmi <image_name:tag>|<image_id> 2 [root@docker01 docker_test]# docker images 3 REPOSITORY TAG IMAGE ID CREATED SIZE 4 centos latest 470671670cac 2 months ago 237MB 5 [root@docker01 docker_test]# docker rmi 470671670cac # 删除镜像 6 [root@docker01 docker_test]# docker images 7 REPOSITORY TAG IMAGE ID CREATED SIZE
1 # 格式:docker load -i <image_file> 2 [root@docker01 docker_test]# docker load -i centos_docker_20200413.tar 3 [root@docker01 docker_test]# docker images 4 REPOSITORY TAG IMAGE ID CREATED SIZE 5 centos latest 470671670cac 2 months ago 237MB
1 # 格式:docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG] 2 [root@docker01 docker_test]# docker images 3 REPOSITORY TAG IMAGE ID CREATED SIZE 4 centos latest 470671670cac 2 months ago 237MB 5 [root@docker01 docker_test]# docker tag centos:latest centos:20200413 6 [root@docker01 docker_test]# docker images 7 REPOSITORY TAG IMAGE ID CREATED SIZE 8 centos 20200413 470671670cac 2 months ago 237MB 9 centos latest 470671670cac 2 months ago 237MB
使用:根据需要给docker镜像打一个新标签。
1 [root@docker01 ~]# docker info 2 Containers: 0 3 Running: 0 4 Paused: 0 5 Stopped: 0 6 Images: 1 7 Server Version: 18.06.3-ce 8 Storage Driver: overlay2 9 Backing Filesystem: xfs 10 Supports d_type: true 11 Native Overlay Diff: true 12 Logging Driver: json-file 13 Cgroup Driver: cgroupfs 14 Plugins: 15 Volume: local 16 Network: bridge host macvlan null overlay 17 Log: awslogs fluentd gcplogs gelf journald json-file logentries splunk syslog 18 Swarm: inactive 19 Runtimes: runc 20 Default Runtime: runc 21 Init Binary: docker-init 22 containerd version: 468a545b9edcd5932818eb9de8e72413e616e86e 23 runc version: a592beb5bc4c4092b1b1bac971afed27687340c5 24 init version: fec3683 25 Security Options: 26 seccomp 27 Profile: default 28 Kernel Version: 3.10.0-1062.el7.x86_64 29 Operating System: CentOS Linux 7 (Core) 30 OSType: linux 31 Architecture: x86_64 32 CPUs: 2 33 Total Memory: 1.777GiB 34 Name: docker01 35 ID: XIHU:XNWU:II7A:YXUH:BOZ3:JSGG:J3P2:CU2Z:5QHA:5Y64:PZ4V:62DI 36 Docker Root Dir: /var/lib/docker 37 Debug Mode (client): false 38 Debug Mode (server): false 39 Registry: https://index.docker.io/v1/ 40 Labels: 41 Experimental: false 42 Insecure Registries: 43 127.0.0.0/8 44 Registry Mirrors: 45 https://hub-mirror.c.163.com/ 46 Live Restore Enabled: false 47 48 WARNING: bridge-nf-call-iptables is disabled 49 WARNING: bridge-nf-call-ip6tables is disabled
另外一个需要注意的问题是,docker image ls 列表中的镜像体积总和并非是所有镜像实际硬盘消耗。由于 Docker 镜像是多层存储结构,并且可以继承、复用,因此不同镜像可能会因为使用相同的基础镜像,从而拥有共同的层。由于 Docker 使用 Union FS,相同的层只需要保存一份即可,因此实际镜像硬盘占用空间很可能要比这个列表镜像大小的总和要小的多。
可以通过以下命令来便捷的查看镜像、容器、数据卷所占用的空间。
1 [root@docker01 docker_test]# docker system df 2 TYPE TOTAL ACTIVE SIZE RECLAIMABLE 3 Images 2 0 440.1MB 440.1MB (100%) 4 Containers 0 0 0B 0B 5 Local Volumes 0 0 0B 0B 6 Build Cache 0 0 0B 0B
1 # 格式:docker inspect <镜像ID>|<镜像名>|<容器ID>|<容器名> 2 [root@docker01 ~]# docker inspect centos:latest # 显示镜像详情 3 [root@docker01 ~]# docker inspect 67ba647b0151 # 显示容器详情
1 # 格式:docker run [OPTIONS] IMAGE [COMMAND] [ARG...] 2 [root@docker01 ~]# docker run -i -t --name centos01 centos:latest /bin/bash 3 [root@f7c4da3cecad /]# # 此时已进入docker容器 4 [root@f7c4da3cecad /]# exit # 退出容器,此时容器会停止【正常情况】 5 [root@docker01 ~]#
参数说明:
1 -i 交互式操作 2 -t 分配一个终端 3 --name 运行的容器名称 4 最后的/bin/bash 要执行的命令
1 [root@docker01 ~]# docker ps # 查看正在运行的容器 2 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 3 [root@docker01 ~]# 4 [root@docker01 ~]# docker ps -a # 查看所有容器,包括运行和停止的 5 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 6 f7c4da3cecad centos:latest "/bin/bash" 4 minutes ago Exited (0) 36 seconds ago centos01
1 # 格式:docker start <容器名>|<容器ID> 2 [root@docker01 ~]# docker ps -a 3 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 4 f7c4da3cecad centos:latest "/bin/bash" 10 minutes ago Exited (0) 3 seconds ago centos01 5 [root@docker01 ~]# docker start f7c4da3cecad # 启动容器 6 f7c4da3cecad 7 [root@docker01 ~]# docker ps 8 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 9 f7c4da3cecad centos:latest "/bin/bash" 10 minutes ago Up 3 seconds centos01
1 # 格式:docker restart <容器名>|<容器ID> 2 [root@docker01 ~]# docker ps 3 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 4 f7c4da3cecad centos:latest "/bin/bash" 15 minutes ago Up 4 minutes centos01 5 [root@docker01 ~]# docker restart f7c4da3cecad # 重启容器 6 f7c4da3cecad 7 [root@docker01 ~]# docker ps 8 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 9 f7c4da3cecad centos:latest "/bin/bash" 15 minutes ago Up 1 second centos01
1 # 格式:docker stop <容器名>|<容器ID> 2 [root@docker01 ~]# docker ps 3 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 4 f7c4da3cecad centos:latest "/bin/bash" 3 hours ago Up 4 minutes centos01 5 [root@docker01 ~]# docker stop f7c4da3cecad # 停止容器 6 f7c4da3cecad 7 [root@docker01 ~]# docker ps -a 8 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 9 f7c4da3cecad centos:latest "/bin/bash" 3 hours ago Exited (0) 10 seconds ago centos01
1 # 格式:docker rm <容器名>|<容器ID> 2 [root@docker01 ~]# docker ps -a 3 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 4 f7c4da3cecad centos:latest "/bin/bash" 3 hours ago Exited (0) 10 seconds ago centos01 5 [root@docker01 ~]# docker rm f7c4da3cecad # 删除已停止的容器 6 f7c4da3cecad
说明:如果要强制删除正在运行的容器,使用 docker rm -f <容器ID>。不过生产环境不建议强制删除容器,防止误删除。
1 # 格式:docker rename CONTAINER NEW_NAME 2 [root@docker01 ~]# docker ps 3 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 4 67ba647b0151 centos:latest "/bin/bash" About a minute ago Up About a minute centos01 5 [root@docker01 ~]# docker rename 67ba647b0151 centos001 # 容器重命名 6 [root@docker01 ~]# docker ps 7 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 8 67ba647b0151 centos:latest "/bin/bash" 2 minutes ago Up About a minute centos001
1 [root@docker01 ~]# docker exec -it f7c4da3cecad bash 2 [root@f7c4da3cecad /]#
说明:不建议通过 docker attach 方式进入容器。
1 # 不要有 -t 选项,因为不需要分配 tty 终端 2 [root@docker01 ~]# docker exec -i f7c4da3cecad bash -c "ps -ef" 3 UID PID PPID C STIME TTY TIME CMD 4 root 1 0 0 10:12 pts/0 00:00:00 /bin/bash 5 root 78 0 0 10:19 ? 00:00:00 ps -ef
1 [root@docker01 ~]# docker exec -i 67ba647b0151 bash -c "ls -l /root" 2 total 12 3 -rw------- 1 root root 2366 Jan 13 21:49 anaconda-ks.cfg 4 -rw-r--r-- 1 root root 435 Jan 13 21:49 anaconda-post.log 5 -rw------- 1 root root 2026 Jan 13 21:49 original-ks.cfg 6 [root@docker01 ~]# 7 [root@docker01 ~]# docker cp /usr/bin/telnet 67ba647b0151:/root/ # 拷贝文件 8 [root@docker01 ~]# docker cp /root/basedOptimi 67ba647b0151:/root/ # 拷贝目录 9 [root@docker01 ~]# docker exec -i 67ba647b0151 bash -c "ls -l /root" 10 total 112 11 -rw------- 1 root root 2366 Jan 13 21:49 anaconda-ks.cfg 12 -rw-r--r-- 1 root root 435 Jan 13 21:49 anaconda-post.log 13 drwxr-xr-x 2 root root 30 Mar 8 19:59 basedOptimi 14 -rw------- 1 root root 2026 Jan 13 21:49 original-ks.cfg 15 -rwxr-xr-x 1 root root 101776 Aug 3 2017 telnet
将docker容器中的文件或目录,拷贝到宿主机中
1 [root@docker01 ~]# docker cp 67ba647b0151:/root/original-ks.cfg /root/ # 拷贝文件 2 [root@docker01 ~]# docker cp 67ba647b0151:/etc /root/ # 拷贝目录
1 [root@docker01 ~]# docker ps 2 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 3 f7c4da3cecad centos:latest "/bin/bash" 3 hours ago Up 1 second centos01 4 # 获取容器日志 5 [root@docker01 ~]# docker logs -f --tail 500 f7c4da3cecad 6 …………
参数说明:
1 -f 持续打印输出 2 --tail 500 打印日志最后的500行
可用于监控
1 [root@docker01 ~]# docker stats <容器ID>|<容器名称> # 持续监控 2 [root@docker01 ~]# docker stats --no-stream <容器ID>|<容器名称> # 不是持续监控,只显示第一次返回的结果
1 [root@docker01 ~]# docker ps 2 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 3 67ba647b0151 centos:latest "/bin/bash" 2 hours ago Up 2 hours centos001 4 # 查看容器中运行的进程信息 5 [root@docker01 ~]# docker top 67ba647b0151 6 UID PID PPID C STIME TTY TIME CMD 7 root 3302 3285 0 21:13 pts/0 00:00:00 /bin/bash
容器映射端口有:随机端口映射、指定单个端口映射、指定多个端口映射
1 [root@docker01 ~]# docker pull registry.cn-beijing.aliyuncs.com/google_registry/nginx:1.17 2 [root@docker01 ~]# docker tag ed21b7a8aee9 nginx:1.17 3 [root@docker01 ~]# docker images | grep ‘nginx‘ 4 nginx 1.17 ed21b7a8aee9 2 weeks ago 127MB 5 registry.cn-beijing.aliyuncs.com/google_registry/nginx 1.17 ed21b7a8aee9 2 weeks ago 127MB
映射随机端口
1 [root@docker01 ~]# docker run -d -P --name nginx01 nginx:1.17 2 e90c9faaf8e3387920dd9763bf5c7df9dd17856673868bb512cec78741ff71dc 3 [root@docker01 ~]# docker ps 4 CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 5 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 前面为宿主机端口,后面为容器端口
容器日志:
[root@docker01 ~]# docker logs -f --tail 500 nginx01
浏览器访问:
映射单个指定端口
1 [root@docker01 ~]# docker run -d -p 81:80 --name nginx02 nginx:1.17 2 04478222f0dc981883f25504164be3af7da49248886cee7386ccc89b80cc57a1 3 [root@docker01 ~]# docker ps | grep ‘nginx02‘ 4 04478222f0dc nginx:1.17 "nginx -g ‘daemon of…" 29 seconds ago Up 28 seconds 0.0.0.0:81->80/tcp nginx02
浏览器访问:
映射多个指定端口
1 [root@docker01 ~]# docker run -d -p 85:80 -p 445:443 --name nginx03 nginx:1.17 2 5886e52ff8e934bc827c8d7753a532b9062bd045799d0658a008e371e6ecd09c 3 [root@docker01 ~]# docker ps | grep ‘nginx03‘ 4 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^-) !
标签:mysql mon oca stop version alt ade 结构 指定端口
原文地址:https://www.cnblogs.com/lonelyxmas/p/13138539.html