Docker------基础命令
一、安装Docker
1.1 安装依赖包
iptables -F
setenforce 0
yum install -y yum-utils device-mapper-persistent-data lvm2 #在线源安装
#yum-utils提供了yum-config-manager
#Device Mapper 存储驱动程序需要device-mapper-persistent-data和lvm2
#Device Mapper是Linux内核中支持逻辑卷管理的通用设备映射机制,它为实现用于存储资源管理的块设备驱动提供了一个高度模块化的内核架构。
1.2 设置阿里云镜像源
yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
1.3 安装Docker-CE
yum install -y docker-ce
vim /etc/selinux/config
SELINUX=disabled
[root@localhost etc]# cd /etc/docker
-bash: cd: /etc/docker: 没有那个文件或目录
[root@localhost etc]# systemctl start docker.service
[root@localhost etc]# systemctl enable docker.service
[root@localhost etc]# cd /etc/docker/
[root@localhost docker]# ls
key.json
[root@localhost docker]# vim key.json #对接docker共有仓库的密钥
#key.json中的数据格式是以键值对key:value的json格式
{"crv":"P-256","d":"rQw9EAqWUorGAIz6Kp0MpXIjd2EE1L7ugpN7WYllEL4","kid":"2LIM:GQWG:XXHM:LW4K:CCYM:MAJJ:BAG7:Q2NP:RN7A:EBJX:FGVJ:YT46","kty":"EC","x":"cLZ5e0UHgJ63_00sqSP7G9xmG-EJC2tuqO-wVRoJTlY","y":"KlA8UesRJBWpzKvM7Cx1IMHzvJgGfj7vi9KAQ-r1UbA"}
[root@localhost docker]# ifconfig #查看网卡多出一个docker0网卡
docker0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500
inet 172.17.0.1 netmask 255.255.0.0 broadcast 172.17.255.255
ether 02:42:d3:aa:41:77 txqueuelen 0 (Ethernet)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
1.4 镜像加速
tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://xxxxxxxx.mirror.aliyuncs.com"]
}
EOF
systemctl daemon-reload
systemctl restart docker
1.5 网络优化
vim /etc/sysctl.conf
net.ipv4.ip_forward=1
sysctl -p
systemctl restart network
systemctl restart docker
二、查看docker版本信息
docker version #查看docker版本信息
Client: Docker Engine - Community
Version: 20.10.5 #docker版本
API version: 1.41 #API版本
Go version: go1.13.15 #go语言版本,由go开发
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
三、查找指定镜像
docker search nginx #查找指定镜像
docker search nginx #查找指定镜像
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
#镜像名称 描述 星级(好评度)
nginx Official build of Nginx. 14607 [OK]
jwilder/nginx-proxy Automated Nginx reverse proxy for docker con… 1988 [OK] #jwilder/nginx-proxy 仓库名/镜像名
richarvey/nginx-php-fpm Container running Nginx + PHP-FPM capable of… 811 [OK]
jc21/nginx-proxy-manager Docker container for managing Nginx proxy ho… 163
linuxserver/nginx An Nginx container, brought to you by LinuxS… 142
tiangolo/nginx-rtmp Docker image with Nginx using the nginx-rtmp… 119 [OK]
jlesage/nginx-proxy-manager Docker container for Nginx Proxy Manager 97 [OK]
bitnami/nginx Bitnami nginx Docker Image 95 [OK]
alfg/nginx-rtmp NGINX, nginx-rtmp-module and FFmpeg from sou… 89 [OK]
jasonrivers/nginx-rtmp Docker images to host RTMP streams using NGI… 88 [OK]
nginxdemos/hello NGINX webserver that serves a simple page co… 67 [OK]
privatebin/nginx-fpm-alpine PrivateBin running on an Nginx, php-fpm & Al… 50 [OK]
nginx/nginx-ingress NGINX Ingress Controller for Kubernetes 49
nginxinc/nginx-unprivileged Unprivileged NGINX Dockerfiles 32
schmunk42/nginx-redirect A very simple container to redirect HTTP tra… 19 [OK]
staticfloat/nginx-certbot Opinionated setup for automatic TLS certs lo… 19 [OK]
nginx/nginx-prometheus-exporter NGINX Prometheus Exporter 16
centos/nginx-112-centos7 Platform for running nginx 1.12 or building … 15
centos/nginx-18-centos7 Platform for running nginx 1.8 or building n… 13
raulr/nginx-wordpress Nginx front-end for the official wordpress:f… 13 [OK]
flashspys/nginx-static Super Lightweight Nginx Image 9 [OK]
bitnami/nginx-ingress-controller Bitnami Docker Image for NGINX Ingress Contr… 8 [OK]
mailu/nginx Mailu nginx frontend 8 [OK]
ansibleplaybookbundle/nginx-apb An APB to deploy NGINX 2 [OK]
wodby/nginx Generic nginx 1 [OK]
四、查看镜像/下载镜像
docker images 仓库名称:标签 #查看下载镜像信息
docker inspect 镜像ID #获取镜像信息
docker pull 仓库名称:标签 #获取/下载镜像 ,AUFS(联合文件系统)分层下载
#下载后存放在/var/lib/docker中
/var/lib/docker/image/overlay/repositories.json #下载文件信息
[root@localhost docker]# docker images #查看镜像
REPOSITORY TAG IMAGE ID CREATED SIZE
[root@localhost docker]# docker pull nginx #下载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
[root@localhost docker]# docker images #查看镜像,已经下载
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest f6d0b4767a6c 2 months ago 133MB
五、删除docker镜像
docker rmi 仓库名称:标签
docker rmi 镜像ID
docker rmi nginx #以镜像名称删除
docker rmi f6d0b4767a6c #以镜像ID删除
六、添加新标签
docker tag 名称:标签 新名称:新标签
[root@localhost docker]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest f6d0b4767a6c 2 months ago 133MB
[root@localhost docker]# docker tag nginx:latest nginx:test #添加新标签
[root@localhost docker]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest f6d0b4767a6c 2 months ago 133MB
nginx test f6d0b4767a6c 2 months ago 133MB #新标签镜像
#此时不能使用镜像ID删除镜像,可以通过标签删除任一镜像
七、镜像备份
cd /opt/ #存放在此目录中
docker save -o xyw_nginx nginx:latest #镜像备份,文件名为xyw_nginx
docker load < xyw_nginx #载入镜像
docker --input xyw_nginx #载入镜像
[root@localhost docker]# docker images #此时镜像存在
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest f6d0b4767a6c 2 months ago 133MB
nginx test f6d0b4767a6c 2 months ago 133MB
[root@localhost docker]# cd /opt/
[root@localhost opt]# docker save -o xyw_nginx nginx:latest #镜像备份
[root@localhost opt]# ls
containerd rh xyw_nginx
[root@localhost opt]# ll -h
总用量 131M
drwx--x--x. 4 root root 28 3月 22 14:45 containerd
drwxr-xr-x. 2 root root 6 3月 26 2015 rh
-rw-------. 1 root root 131M 3月 22 15:56 xyw_nginx #备份的镜像文件
[root@localhost opt]# docker rmi nginx:latest #删除原有镜像
Untagged: nginx:latest
[root@localhost opt]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx test f6d0b4767a6c 2 months ago 133MB
[root@localhost opt]# docker rmi f6d0b4767a6c #继续删除
Untagged: nginx:test
Untagged: nginx@sha256:10b8cc432d56da8b61b070f4c7d2543a9ed17c2b23010b43af434fd40e2ca4aa
Deleted: sha256:f6d0b4767a6c466c178bf718f99bea0d3742b26679081e52dbf8e0c7c4c42d74
Deleted: sha256:4dfe71c4470c5920135f00af483556b09911b72547113512d36dc29bfc5f7445
Deleted: sha256:3c90a0917c79b758d74b7040f62d17a7680cd14077f734330b1994a2985283b8
Deleted: sha256:a1c538085c6f891424160d8db120ea093d4dda393e94cd4713e3fff3c82299b5
Deleted: sha256:a3ee2510dcf02c980d7aff635909612006fd1662084d6225e52e769b984abeb5
Deleted: sha256:cb42413394c4059335228c137fe884ff3ab8946a014014309676c25e3ac86864
[root@localhost opt]# docker images #此时已没有镜像
REPOSITORY TAG IMAGE ID CREATED SIZE
[root@localhost opt]# docker load < xyw_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@localhost opt]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest f6d0b4767a6c 2 months ago 133MB
八、上传镜像
8.1 登录
docker login
Username: docker
password:
Email:xxx@xxx.com
8.2 上传
docker push xyw/nginx:web
九、容器操作
9.1 创建容器
docker create -it nginx:latest /bin/bash #创建容器
-i:让容器的标准输入保持打开
-t:唐docker分配一个伪终端
/bin/bash:运行环境
9.2 查看容器的状态信息
docker ps #查看运行中容器的状态信息
docker ps -a #查看所有容器的状态信息
9.3 启动容器
docker start 868b3950cca5 #后面加容器的序列号
9.4 启动执行命令查看系统根目录
docker run centos:7 /bin/bash -c ls /
-d:表示开启守护进程daemon,后台运行
-c:表示继续执行后面的命令,ls / 查看系统根目录
docker run -d centos:7 /bin/bash -c "while reue;do echo hello;done" #死循环
#常见的坑,docker,容器使用后台运行,必须要有一个前台进程,docker发现没有应用就会自动停止,发现自己没提供服务就会自动停止,这就是为什么要创死循环的原因
# -it 可以保持up状态
[root@localhost ~]# docker run -d centos:7 /bin/bash
a2e79a14833ca866483deb1438c17f9e4c21dd254bb08568a1c69116f82c3625
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a2e79a14833c centos:7 "/bin/bash" 7 seconds ago Exited (0) 5 seconds ago crazy_khorana #即使启动了,因为没有服务运行,还是终止了
fe61b1e97eda centos:7 "/bin/bash -c 'while…" 3 hours ago Exited (137) 2 hours ago inspiring_jennings
e1d8e30f7902 centos:7 "/bin/bash -c 'while…" 4 hours ago Exited (137) 3 hours ago frosty_elbakyan
[root@localhost ~]# docker run -itd nginx:latest /bin/bash
4e0245a200ffb43b45b320f9b413616e7d14e8b5e304690e1d6622528a248fa2
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
4e0245a200ff nginx:latest "/docker-entrypoint.…" 2 seconds ago Up 2 seconds 80/tcp confident_cohen
[root@localhost ~]# docker run -itd centos:7 /bin/bash
f3754aa308bd1cb16c0d25bec57763729cb8067cc8890d4b17faa6f20aa4e36b
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f3754aa308bd centos:7 "/bin/bash" 2 seconds ago Up 1 second busy_moore
4e0245a200ff nginx:latest "/docker-entrypoint.…" 30 seconds ago Up 29 seconds 80/tcp confident_cohen
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest f6d0b4767a6c 2 months ago 133MB
[root@localhost ~]# docker ps -a #查看所有容器的状态信息
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES #有镜像但没有容器
[root@localhost ~]# docker create -it nginx:latest /bin/bash #创建容器
868b3950cca56f28a640926ee224c151f7ad58e4760d89ce29e4caba0365a6b9 #创建容器的序列号
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
868b3950cca5 nginx:latest "/docker-entrypoint.…" 10 seconds ago Created upbeat_swartz #状态是创建状态,COMMAND是分配的终端
[root@localhost ~]# docker start 868b3950cca5 #启动容器
868b3950cca5
[root@localhost ~]# docker ps -a #查看所有容器,docker ps 表示查看运行状态的容器
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
868b3950cca5 nginx:latest "/docker-entrypoint.…" 2 minutes ago Up 3 seconds 80/tcp upbeat_swartz
#开启后会有端口号
[root@localhost ~]# docker run centos:7 /bin/bash -c ls / #-c表示运行后面的命令
Unable to find image 'centos:7' locally
7: Pulling from library/centos
2d473b07cdd5: Pull complete
Digest: sha256:0f4ec88e21daf75124b8a9e5ca03c37a5e937e0e108a255d890492430789b60e
Status: Downloaded newer image for centos:7
anaconda-post.log #这里是ls的结果
bin
dev
etc
home
lib
lib64
media
mnt
opt
proc
root
run
sbin
srv
sys
tmp
usr
var
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest f6d0b4767a6c 2 months ago 133MB
centos 7 8652b9f0cb4c 4 months ago 204MB #下载了centos7的镜像
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b1cde108db4b centos:7 "/bin/bash -c ls /" 4 minutes ago Exited (0) 4 minutes ago sweet_wozniak #容器已创建完成
868b3950cca5 nginx:latest "/docker-entrypoint.…" 35 minutes ago Up 33 minutes 80/tcp upbeat_swartz
[root@localhost ~]# docker run -d centos:7 /bin/bash
2ca7c5a8ab5385da21e72609e42508361f70fd89bf4697e1efa45282f62b0e8a
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2ca7c5a8ab53 centos:7 "/bin/bash" 7 seconds ago Exited (0) 6 seconds ago tender_curran
b1cde108db4b centos:7 "/bin/bash -c ls /" 41 minutes ago Exited (0) 41 minutes ago sweet_wozniak
868b3950cca5 nginx:latest "/docker-entrypoint.…" About an hour ago Up About an hour 80/tcp upbeat_swartz
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest f6d0b4767a6c 2 months ago 133MB
centos 7 8652b9f0cb4c 4 months ago 204MB
[root@localhost ~]# docker run -d centos:7 /bin/bash -c "while true;do echo hello;done"
e1d8e30f79025228bade8faab7f7d9faaf6d1af5ba88b1490834fe350f818f95
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e1d8e30f7902 centos:7 "/bin/bash -c 'while…" 4 seconds ago Up 2 seconds frosty_elbakyan
2ca7c5a8ab53 centos:7 "/bin/bash" 2 minutes ago Exited (0) 2 minutes ago tender_curran
b1cde108db4b centos:7 "/bin/bash -c ls /" 43 minutes ago Exited (0) 43 minutes ago sweet_wozniak
868b3950cca5 nginx:latest "/docker-entrypoint.…" About an hour ago Up About an hour 80/tcp upbeat_swartz
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest f6d0b4767a6c 2 months ago 133MB
centos 7 8652b9f0cb4c 4 months ago 204MB
[root@localhost ~]# top
top - 11:00:30 up 4:09, 5 users, load average: 1.53, 0.40, 0.17
Tasks: 202 total, 4 running, 197 sleeping, 1 stopped, 0 zombie
%Cpu(s): 63.1 us, 34.2 sy, 0.0 ni, 2.5 id, 0.0 wa, 0.0 hi, 0.2 si, 0.0 st
KiB Mem : 2031912 total, 71700 free, 864396 used, 1095816 buff/cache
KiB Swap: 4192252 total, 4091376 free, 100876 used. 865444 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
50350 root 20 0 11692 1052 868 R 99.0 0.1 0:27.16 bash
43455 root 20 0 598100 55104 4884 S 76.4 2.7 0:39.00 dockerd
50328 root 20 0 112980 3408 2172 S 15.6 0.2 0:04.31 containerd-shim
46377 root 20 0 0 0 0 R 1.7 0.0 0:02.03 kworker/u256:0
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e1d8e30f7902 centos:7 "/bin/bash -c 'while…" 52 seconds ago Up 50 seconds frosty_elbakyan
2ca7c5a8ab53 centos:7 "/bin/bash" 2 minutes ago Exited (0) 2 minutes ago tender_curran
b1cde108db4b centos:7 "/bin/bash -c ls /" 44 minutes ago Exited (0) 44 minutes ago sweet_wozniak
868b3950cca5 nginx:latest "/docker-entrypoint.…" About an hour ago Up About an hour 80/tcp upbeat_swartz
9.5 终止运行
docker stop 868b3950cca5
[root@localhost ~]# docker stop 868b3950cca5
868b3950cca5
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e1d8e30f7902 centos:7 "/bin/bash -c 'while…" About a minute ago Up About a minute frosty_elbakyan
2ca7c5a8ab53 centos:7 "/bin/bash" 3 minutes ago Exited (0) 3 minutes ago tender_curran
b1cde108db4b centos:7 "/bin/bash -c ls /" 45 minutes ago Exited (0) 45 minutes ago sweet_wozniak
868b3950cca5 nginx:latest "/docker-entrypoint.…" About an hour ago Exited (137) 11 seconds ago upbeat_swartz
# Exited(0)、 Exited(137):0为正常退出;不为0则异常退出。
9.6 删除容器
docker rm cb9c9eb25ffb
docker ps -a | awk '{print "docker rm "$1}' | bash #删除非运行状态容器
docker rm `docker ps -aq` #删除非运行状态容器
docker rm -f `docker ps -aq` #删除所有容器
[root@localhost ~]# docker ps -a | awk '{print "docker rm "$1}' | bash #docker rm `docker ps -aq` 也可以删除非运行容器 -f强制删除所有容器
Error: No such container: CONTAINER
Error response from daemon: You cannot remove a running container e1d8e30f79025228bade8faab7f7d9faaf6d1af5ba88b1490834fe350f818f95. Stop the container before attempting removal or force remove
cb9c9eb25ffb
2ca7c5a8ab53
b1cde108db4b
868b3950cca5
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e1d8e30f7902 centos:7 "/bin/bash -c 'while…" 3 minutes ago Up 3 minutes frosty_elbakyan
[root@localhost ~]# top
top - 11:03:59 up 4:13, 5 users, load average: 4.95, 2.65, 1.11
Tasks: 199 total, 2 running, 196 sleeping, 1 stopped, 0 zombie
%Cpu(s): 66.1 us, 30.9 sy, 0.0 ni, 3.0 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 2031912 total, 74032 free, 358112 used, 1599768 buff/cache
KiB Swap: 4192252 total, 3596084 free, 596168 used. 1388780 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
50350 root 20 0 11692 340 156 R 100.0 0.0 3:54.26 bash
43455 root 20 0 598100 36620 3712 S 76.4 1.8 3:17.21 dockerd
50328 root 20 0 113108 1904 644 S 16.3 0.1 0:37.05 containerd-shim
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a2e79a14833c centos:7 "/bin/bash" About a minute ago Exited (0) 1 second ago crazy_khorana
fe61b1e97eda centos:7 "/bin/bash -c 'while…" 3 hours ago Exited (137) 2 hours ago inspiring_jennings
e1d8e30f7902 centos:7 "/bin/bash -c 'while…" 4 hours ago Exited (137) 3 hours ago frosty_elbakyan
[root@localhost ~]# docker rm
crazy_khorana frosty_elbakyan inspiring_jennings
[root@localhost ~]# docker rm `docker ps -aq` #删除非运行状态容器
a2e79a14833c
fe61b1e97eda
e1d8e30f7902
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9.7 进入/退出容器
[root@localhost ~]# docker exec -it f3754aa308bd /bin/bash #进入容器时,容器必须是up状态
[root@f3754aa308bd /]# ls
anaconda-post.log dev home lib64 mnt proc run srv tmp var
bin etc lib media opt root sbin sys usr
[root@f3754aa308bd /]# ifconfig
bash: ifconfig: command not found
[root@f3754aa308bd /]# yum -y install net-tools
[root@f3754aa308bd /]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.17.0.3 netmask 255.255.0.0 broadcast 172.17.255.255 #IP地址为172.17.0.3,局域网
ether 02:42:ac:11:00:03 txqueuelen 0 (Ethernet)
RX packets 9154 bytes 14341841 (13.6 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 5018 bytes 274305 (267.8 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
loop txqueuelen 1 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@f3754aa308bd /]# exit
exit
[root@localhost ~]#
9.8 容器导出
[root@localhost ~]# docker export f3754aa308bd > /opt/cent
[root@localhost ~]# cd /opt/
[root@localhost opt]# ls
cent containerd rh xyw_nginx
9.9 容器导入
#因为有centos镜像正在使用,想删除镜像,要先删除容器
#导入命令只生成镜像,不生成容器
[root@localhost opt]# docker stop f3754aa308bd
f3754aa308bd
[root@localhost opt]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c794a69bf6ce centos:7 "/bin/bash" 26 minutes ago Exited (0) 26 minutes ago hopeful_shtern
f3754aa308bd centos:7 "/bin/bash" 29 minutes ago Exited (137) 16 seconds ago busy_moore
4e0245a200ff nginx:latest "/docker-entrypoint.…" 29 minutes ago Up 29 minutes 80/tcp confident_cohen
[root@localhost opt]# docker rmi 8652b9f0cb4c
Error response from daemon: conflict: unable to delete 8652b9f0cb4c (must be forced) - image is being used by stopped container c794a69bf6ce
[root@localhost opt]# docker rm `docker ps -aq`
c794a69bf6ce
f3754aa308bd
Error response from daemon: You cannot remove a running container 4e0245a200ffb43b45b320f9b413616e7d14e8b5e304690e1d6622528a248fa2. Stop the container before attempting removal or force remove
[root@localhost opt]# docker rmi 8652b9f0cb4c
Untagged: centos:7
Untagged: centos@sha256:0f4ec88e21daf75124b8a9e5ca03c37a5e937e0e108a255d890492430789b60e
Deleted: sha256:8652b9f0cb4c0599575e5a003f5906876e10c1ceb2ab9fe1786712dac14a50cf
Deleted: sha256:174f5685490326fc0a1c0f5570b8663732189b327007e47ff13d2ca59673db02
[root@localhost opt]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest f6d0b4767a6c 2 months ago 133MB
[root@localhost opt]# cat /opt/cent | docker import - centos:os-7 #容器导入(centos名称:os-7标签)
sha256:400cf047cbc5df149e70efe6fe8a048aaed8d888bb1d4a292e96695b0cfd24d5
[root@localhost opt]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos os-7 400cf047cbc5 4 seconds ago 286MB
nginx latest f6d0b4767a6c 2 months ago 133MB
十、私有库创建
[root@localhost ~]# docker pull registry #下载私有库内容
Using default tag: latest
latest: Pulling from library/registry
0a6724ff3fcd: Pull complete
d550a247d74f: Pull complete
1a938458ca36: Pull complete
acd758c36fc9: Pull complete
9af6d68b484a: Pull complete
Digest: sha256:d5459fcb27aecc752520df4b492b08358a1912fcdfa454f7d2101d4b09991daa
Status: Downloaded newer image for registry:latest
docker.io/library/registry:latest
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos os-7 400cf047cbc5 11 minutes ago 286MB
nginx latest f6d0b4767a6c 2 months ago 133MB
registry latest 678dfa38fcfa 3 months ago 26.2MB #下载后会生成一个镜像
[root@localhost ~]# vim /etc/docker/daemon.json #修改此配置文件
{
"insecure-registries":["192.168.238.10:5000"], #添加内容,建在哪用哪的IP,端口为5000,注意逗号
"registry-mirrors": ["https://4xxglnmv.mirror.aliyuncs.com"]
}
[root@localhost ~]# systemctl restart docker.service
[root@localhost ~]# docker create -it registry /bin/bash
70673b43a6157cb802a741b4ff9f0d6748c7f65c89821a895d576f836889ef25
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
70673b43a615 registry "/entrypoint.sh /bin…" 7 seconds ago Created eloquent_hoover
4e0245a200ff nginx:latest "/docker-entrypoint.…" About an hour ago Exited (137) 5 minutes ago confident_cohen
[root@localhost ~]# docker start 70673b43a615
70673b43a615
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
70673b43a615 registry "/entrypoint.sh /bin…" About a minute ago Exited (127) 3 seconds ago eloquent_hoover
4e0245a200ff nginx:latest "/docker-entrypoint.…" About an hour ago Exited (137) 5 minutes ago confident_cohen
[root@localhost ~]# docker run -d -p 5000:5000 -v /data/registry:/tmp/registry registry 宿主机的/data/registry自动创建挂在容器中的/tmp/registry路径
375179e2066255ca9edab271e48fe462722cfd64dee968d70717b6e9c55ecd37
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
375179e20662 registry "/entrypoint.sh /etc…" 45 seconds ago Up 44 seconds 0.0.0.0:5000->5000/tcp hardcore_germain #端口映射
70673b43a615 registry "/entrypoint.sh /bin…" 5 minutes ago Exited (127) 4 minutes ago eloquent_hoover
4e0245a200ff nginx:latest "/docker-entrypoint.…" About an hour ago Exited (137) 10 minutes ago confident_cohen
[root@localhost ~]# cd /
[root@localhost /]# ls
bin data etc lib media opt root sbin sys usr #/data/registry为外部路径
boot dev home lib64 mnt proc run srv tmp var #/tmp/registry是内部路径
[root@localhost /]# cd /data/
[root@localhost data]# ls
registry
[root@localhost data]# docker tag nginx:latest 192.168.238.10:5000/nginx
[root@localhost data]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos os-7 400cf047cbc5 49 minutes ago 286MB
192.168.238.10:5000/nginx latest f6d0b4767a6c 2 months ago 133MB
nginx latest f6d0b4767a6c 2 months ago 133MB
registry latest 678dfa38fcfa 3 months ago 26.2MB
[root@localhost data]# docker push 192.168.238.10:5000/nginx:latest
The push refers to repository [192.168.238.10:5000/nginx]
85fcec7ef3ef: Pushed
3e5288f7a70f: Pushed
56bc37de0858: Pushed
1c91bf69a08b: Pushed
cb42413394c4: Pushed
latest: digest: sha256:0b159cd1ee1203dad901967ac55eee18c24da84ba3be384690304be93538bea8 size: 1362
[root@localhost data]# curl -XGET http://192.168.238.10:5000/v2/_catalog #获取私有仓库列表
{"repositories":["nginx"]} #显示上传成功
[root@localhost data]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos os-7 400cf047cbc5 About an hour ago 286MB
nginx latest f6d0b4767a6c 2 months ago 133MB #准备上传
192.168.238.10:5000/nginx latest f6d0b4767a6c 2 months ago 133MB
registry latest 678dfa38fcfa 3 months ago 26.2MB
[root@localhost data]# docker tag centos:os-7 192.168.238.10:5000/centos7 #打标签
[root@localhost data]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
192.168.238.10:5000/centos7 latest 400cf047cbc5 About an hour ago 286MB
centos os-7 400cf047cbc5 About an hour ago 286MB #标签
192.168.238.10:5000/nginx latest f6d0b4767a6c 2 months ago 133MB
nginx latest f6d0b4767a6c 2 months ago 133MB
registry latest 678dfa38fcfa 3 months ago 26.2MB
[root@localhost data]# docker push 192.168.238.10:5000/centos7 #上传
Using default tag: latest
The push refers to repository [192.168.238.10:5000/centos7]
957f31ef0423: Pushed
latest: digest: sha256:1751611aaad689a82346edd08f1c2b3525554fd9b2a9f00c625bdfec47033311 size: 529
[root@localhost data]# curl -XGET http://192.168.238.10:5000/v2/_catalog
{"repositories":["centos7","nginx"]} #验证
#删除不需要的容器
[root@localhost data]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
375179e20662 registry "/entrypoint.sh /etc…" 57 minutes ago Up 16 minutes 0.0.0.0:5000->5000/tcp hardcore_germain #只保留端口映射
70673b43a615 registry "/entrypoint.sh /bin…" About an hour ago Exited (127) About an hour ago eloquent_hoover
4e0245a200ff nginx:latest "/docker-entrypoint.…" 2 hours ago Exited (137) About an hour ago confident_cohen
[root@localhost data]# docker rm 4e0245a200ff
4e0245a200ff
[root@localhost data]# docker rm 70673b43a615
70673b43a615
[root@localhost data]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
375179e20662 registry "/entrypoint.sh /etc…" 58 minutes ago Up 17 minutes 0.0.0.0:5000->5000/tcp hardcore_germain
#删除不需要的镜像
[root@localhost data]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos os-7 400cf047cbc5 About an hour ago 286MB
192.168.238.10:5000/centos7 latest 400cf047cbc5 About an hour ago 286MB
192.168.238.10:5000/nginx latest f6d0b4767a6c 2 months ago 133MB
nginx latest f6d0b4767a6c 2 months ago 133MB
registry latest 678dfa38fcfa 3 months ago 26.2MB
[root@localhost data]# docker rmi centos
Error: No such image: centos
[root@localhost data]# docker rmi centos:os-7
Untagged: centos:os-7
[root@localhost data]# docker rmi 192.168.238.10:5000/centos7:latest
Untagged: 192.168.238.10:5000/centos7:latest
Untagged: 192.168.238.10:5000/centos7@sha256:1751611aaad689a82346edd08f1c2b3525554fd9b2a9f00c625bdfec47033311
Deleted: sha256:400cf047cbc5df149e70efe6fe8a048aaed8d888bb1d4a292e96695b0cfd24d5
Deleted: sha256:957f31ef0423eab08fa8228e7e1946d5f3097aa6fffbe6a3edca59d06823ff7a
[root@localhost data]# docker rmi nginx:latest
Untagged: nginx:latest
[root@localhost data]# docker rmi 192.168.238.10:5000/nginx:latest
Untagged: 192.168.238.10:5000/nginx:latest
Untagged: 192.168.238.10:5000/nginx@sha256:0b159cd1ee1203dad901967ac55eee18c24da84ba3be384690304be93538bea8
Deleted: sha256:f6d0b4767a6c466c178bf718f99bea0d3742b26679081e52dbf8e0c7c4c42d74
Deleted: sha256:4dfe71c4470c5920135f00af483556b09911b72547113512d36dc29bfc5f7445
Deleted: sha256:3c90a0917c79b758d74b7040f62d17a7680cd14077f734330b1994a2985283b8
Deleted: sha256:a1c538085c6f891424160d8db120ea093d4dda393e94cd4713e3fff3c82299b5
Deleted: sha256:a3ee2510dcf02c980d7aff635909612006fd1662084d6225e52e769b984abeb5
Deleted: sha256:cb42413394c4059335228c137fe884ff3ab8946a014014309676c25e3ac86864
[root@localhost data]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
registry latest 678dfa38fcfa 3 months ago 26.2MB #只保留registry
#从私有仓库下载镜像
[root@localhost data]# docker pull 192.168.238.10:5000/nginx
Using default tag: latest
latest: Pulling from nginx
a076a628af6f: Pull complete
0732ab25fa22: Pull complete
d7f36f6fe38f: Pull complete
f72584a26f32: Pull complete
7125e4df9063: Pull complete
Digest: sha256:0b159cd1ee1203dad901967ac55eee18c24da84ba3be384690304be93538bea8
Status: Downloaded newer image for 192.168.238.10:5000/nginx:latest
192.168.238.10:5000/nginx:latest
[root@localhost data]# docker pull 192.168.238.10:5000/centos7
Using default tag: latest
latest: Pulling from centos7
549d636f0460: Pull complete
Digest: sha256:1751611aaad689a82346edd08f1c2b3525554fd9b2a9f00c625bdfec47033311
Status: Downloaded newer image for 192.168.238.10:5000/centos7:latest
192.168.238.10:5000/centos7:latest
[root@localhost data]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
192.168.238.10:5000/centos7 latest 400cf047cbc5 2 hours ago 286MB
192.168.238.10:5000/nginx latest f6d0b4767a6c 2 months ago 133MB
registry latest 678dfa38fcfa 3 months ago 26.2MB
十一、docker容器卷
宿主机目录/share挂载容器中的/data1
[root@localhost data]# docker run -v /share:/data1 --name xyw1 -it centos /bin/bash
Unable to find image 'centos:latest' locally
latest: Pulling from library/centos
7a0437f04f83: Pull complete
Digest: sha256:5528e8b1b1719d34604c87e11dcd1c0a20bedf46e83b5632cdeac91b8c04efc1
Status: Downloaded newer image for centos:latest
[root@67f9702eb629 /]# ls #进入容器
bin dev home lib64 media opt root sbin sys usr
data1 etc lib lost+found mnt proc run srv tmp var
[root@67f9702eb629 /]# cd data1/
[root@67f9702eb629 data1]# ls
[root@67f9702eb629 data1]# touch abc
[root@67f9702eb629 data1]# ls
abc
[root@67f9702eb629 data1]# exit
exit
[root@localhost data]# cd /share/
[root@localhost share]# ls
abc
十二、数据卷容器
[root@localhost share]# docker run --name xyw100 -v /data1 -v /data2 -it centos /bin/bash
[root@4fd2f053bd45 /]# ls #xyw100
bin data2 etc lib lost+found mnt proc run srv tmp var
data1 dev home lib64 media opt root sbin sys usr
[root@4fd2f053bd45 /]# cd data1
[root@4fd2f053bd45 data1]# ls
[root@localhost ~]# docker run -it --volumes-from xyw100 --name xyw200 centos /bin/bash
[root@a872b9673627 /]# ls #xyw200
bin data2 etc lib lost+found mnt proc run srv tmp var
data1 dev home lib64 media opt root sbin sys usr
[root@a872b9673627 /]# cd data1
[root@a872b9673627 data1]# touch abc
[root@a872b9673627 data1]# ls
abc
[root@4fd2f053bd45 data1]# ls #xyw100
abc
[root@localhost share]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a872b9673627 centos "/bin/bash" 4 minutes ago Up 4 minutes xyw200
4fd2f053bd45 centos "/bin/bash" 6 minutes ago Exited (0) 24 seconds ago xyw100
67f9702eb629 centos "/bin/bash" 11 minutes ago Exited (0) 10 minutes ago xyw1
375179e20662 registry "/entrypoint.sh /etc…" 2 hours ago Up 51 minutes 0.0.0.0:5000->5000/tcp hardcore_germain
十三、端口映射
-P指定随机端口
[root@localhost share]# docker run -d -P nginx #-P指定随机端口
Unable to find image 'nginx:latest' locally
latest: Pulling from library/nginx
Digest: sha256:10b8cc432d56da8b61b070f4c7d2543a9ed17c2b23010b43af434fd40e2ca4aa
Status: Downloaded newer image for nginx:latest
1efdce46fc89e3e8f8e0842b3089dcbce911e910b71b468dacc8ad852cd8ff4b
[root@localhost share]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1efdce46fc89 nginx "/docker-entrypoint.…" 4 seconds ago Up 3 seconds 0.0.0.0:49153->80/tcp affectionate_dewdney #端口为49153
a872b9673627 centos "/bin/bash" 6 minutes ago Up 6 minutes xyw200
4fd2f053bd45 centos "/bin/bash" 8 minutes ago Exited (0) 2 minutes ago xyw100
67f9702eb629 centos "/bin/bash" 13 minutes ago Exited (0) 11 minutes ago xyw1
375179e20662 registry "/entrypoint.sh /etc…" 2 hours ago Up 52 minutes 0.0.0.0:5000->5000/tcp hardcore_germain
-p指定特定端口
[root@localhost share]# docker run -d -p 47777:80 nginx
d80d0a9c195ddd4df76fe8b732555d7e1199c2b7a4ca85092a35ebb17bcd8ad6
[root@localhost share]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d80d0a9c195d nginx "/docker-entrypoint.…" 25 seconds ago Up 24 seconds 0.0.0.0:47777->80/tcp kind_easley
1efdce46fc89 nginx "/docker-entrypoint.…" 5 minutes ago Up 5 minutes 0.0.0.0:49153->80/tcp affectionate_dewdney
a872b9673627 centos "/bin/bash" 11 minutes ago Up 11 minutes xyw200
4fd2f053bd45 centos "/bin/bash" 13 minutes ago Exited (0) 7 minutes ago xyw100
67f9702eb629 centos "/bin/bash" 18 minutes ago Exited (0) 17 minutes ago xyw1
375179e20662 registry "/entrypoint.sh /etc…" 2 hours ago Up 58 minutes 0.0.0.0:5000->5000/tcp hardcore_germain
十四、容器互联(使用centos镜像)
[root@localhost share]# docker run -itd -P --name web1 centos /bin/bash
150f93592c4d1aef2d7e3999eebba054161b7d9b44c7930560765bd27df2fa43
[root@localhost share]# docker run -itd -P --name web2 --link web1:web1 centos /bin/bash 第二个web1是别名,验证ping就要ping它
2afa140f8b4ecc180df348ae73ec82c40391b8d6b9c7b79374895956aac2edf3
[root@localhost share]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2afa140f8b4e centos "/bin/bash" 6 seconds ago Up 6 seconds web2 #web2
150f93592c4d centos "/bin/bash" About a minute ago Up About a minute web1 #web1
d80d0a9c195d nginx "/docker-entrypoint.…" About an hour ago Up About an hour 0.0.0.0:47777->80/tcp kind_easley
1efdce46fc89 nginx "/docker-entrypoint.…" About an hour ago Up About an hour 0.0.0.0:49153->80/tcp affectionate_dewdney
a872b9673627 centos "/bin/bash" About an hour ago Up About an hour xyw200
4fd2f053bd45 centos "/bin/bash" About an hour ago Exited (0) About an hour ago xyw100
67f9702eb629 centos "/bin/bash" 2 hours ago Exited (0) 2 hours ago xyw1
375179e20662 registry "/entrypoint.sh /etc…" 3 hours ago Up 2 hours 0.0.0.0:5000->5000/tcp hardcore_germain
[root@localhost share]# docker exec -it 150f93592c4d /bin/bash #进入web1
[root@150f93592c4d /]# ping web2 #这里ping不通,是因为命令做的是连接web1,别名创建的也是web1,所以应该进入web2内,ping web1
ping: web2: Name or service not known
[root@localhost share]# docker exec -it 2afa140f8b4e /bin/bash #进入web2
[root@2afa140f8b4e /]# ping web1 #ping web1 ,此处的web1是别名
PING web1 (172.17.0.6) 56(84) bytes of data.
64 bytes from web1 (172.17.0.6): icmp_seq=1 ttl=64 time=0.079 ms
64 bytes from web1 (172.17.0.6): icmp_seq=2 ttl=64 time=0.067 ms
64 bytes from web1 (172.17.0.6): icmp_seq=3 ttl=64 time=0.135 ms
64 bytes from web1 (172.17.0.6): icmp_seq=4 ttl=64 time=0.057 ms
64 bytes from web1 (172.17.0.6): icmp_seq=5 ttl=64 time=0.120 ms
64 bytes from web1 (172.17.0.6): icmp_seq=6 ttl=64 time=0.121 ms
64 bytes from web1 (172.17.0.6): icmp_seq=7 ttl=64 time=0.121 ms
^C
--- web1 ping statistics ---
7 packets transmitted, 7 received, 0% packet loss, time 7ms
rtt min/avg/max/mdev = 0.057/0.100/0.135/0.029 ms
十五、编写脚本的流程
1、根据需求定义变量
2、选用流程控制语句
3、试写功能
4、优化代码
5、BUG处理(异常)
6、想想第二种方法