在官方的docker仓库中搜索指定名称的docker镜像,也会有很多三方镜像。
docker search centos:7.2.1511 #带指定版本号
docker search centos #不带版本号默认latest
从docker仓库将镜像下载到本地
docker pull 仓库服务器:端口/项目名称/镜像名称:tag(版本)号
docker pull alpine
docker pull nginx
docker pull hello-world
docker pull centos
下载完成的镜像比下载的大,因为下载完成后会解压
docker images
REPOSITORY #镜像所属的仓库名称
TAG #镜像版本号(标识符),默认为latest
IMAGE ID #镜像唯一ID标识
CREATED #镜像创建的时间
VIRTUAL SIZE #镜像的大小
可以将镜像从本地导出为一个压缩文件,然后复制到其他服务器进行导入使用
导出方法1:
docker save centos -o /opt/centos.tar.gz
ll /opt/centos.tar.gz
导出方法2:
docker save centos > /opt/centos-1.tar.gz
ll /opt/centos-1.tar.gz
查看镜像内容:
tar xvf centos.tar.gz
cat manifest.json #包含了镜像的相关配置,配置文件、分层
[root@gbase8c_private opt]# cat manifest.json
[{"Config":"5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6.json","RepoTags":["centos:latest"],"Layers":["079bc5e75545bf45253ab44ce73fbd51d96fa52ee799031e60b65a82e89df662/layer.tar"]}]
#分层为了方便文件的使用,即相同的文件可以共用
docker load < /opt/centos.tar.gz
[root@gbase8c_1 opt]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
[root@gbase8c_1 opt]# docker load < /opt/centos.tar.gz
74ddd0ec08fa: Loading layer [==================================================>] 238.6MB/238.6MB
Loaded image: centos:latest
验证镜像:
docker images
[root@gbase8c_1 opt]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos latest 5d0da3dc9764 2 years ago 231MB
docker rmi centos
日志:
[root@gbase8c_private opt]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
alpine latest 8ca4688f4f35 7 weeks ago 7.34MB
nginx latest 605c77e624dd 23 months ago 141MB
hello-world latest feb5d9fea6a5 2 years ago 13.3kB
centos latest 5d0da3dc9764 2 years ago 231MB
[root@gbase8c_private opt]# docker rmi centos
Untagged: centos:latest
Untagged: centos@sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177
Deleted: sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6
Deleted: sha256:74ddd0ec08fa43d09f32636ba91a0a3053b02cb4627c35051aff89f853606b59
[root@gbase8c_private opt]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
alpine latest 8ca4688f4f35 7 weeks ago 7.34MB
nginx latest 605c77e624dd 23 months ago 141MB
hello-world latest feb5d9fea6a5 2 years ago 13.3kB
命令格式:
#docker run [选项] [镜像名] [shell命令] [参数]
#docker run [参数选项] [镜像名称,必须在所有选项的后面] [/bin/echo/ 'hello world'] #单次执行,没有自定义容器名称
#docker run centos /bin/echo 'hello world' #启动的容器在执行完shell命令就退出了
会直接进入到容器,并随机生成容器ID和名称
docker run -it docker.io/centos bash
#退出容器不注销
ctrl+p+q
docker ps
包括当前正在运行以及以及关闭的所有容器
docker ps -a
[root@gbase8c_private ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7d6158088cbd centos "bash" About a minute ago Up About a minute loving_mcnulty
e3e5fc354a71 hello-world "/hello" About a minute ago Exited (0) About a minute ago determined_mayer
即使容器正在运行当中,也会被强制删除掉
docker rm -f id
[root@gbase8c_private ~]# docker rm -f 7d6158088cbd
7d6158088cbd
[root@gbase8c_private ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e3e5fc354a71 hello-world "/hello" 2 minutes ago Exited (0) 2 minutes ago determined_mayer
docker pull nginx #下载nginx 镜像
docker run -P docker.io/nginx #前台启动并随机映射本地端口到容器的80
前台启动的会话窗口无法进行其他操作,除非退出,但是退出后容器也会退出
随机端口映射,其实是默认从32768开始
[root@gbase8c_private ~]# docker run -P docker.io/nginx
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2023/11/21 11:43:14 [notice] 1#1: using the "epoll" event method
2023/11/21 11:43:14 [notice] 1#1: nginx/1.21.5
2023/11/21 11:43:14 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6)
2023/11/21 11:43:14 [notice] 1#1: OS: Linux 3.10.0-1160.71.1.el7.x86_64
2023/11/21 11:43:14 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2023/11/21 11:43:14 [notice] 1#1: start worker processes
2023/11/21 11:43:14 [notice] 1#1: start worker process 29
2023/11/21 11:43:14 [notice] 1#1: start worker process 30
[root@gbase8c_private ~]# ss -nlt | grep 32
LISTEN 0 128 [::]:32768 [::]:*
[root@gbase8c_private ~]# lsof -i:32768
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
docker-pr 18312 root 4u IPv6 1918183 0t0 TCP *:filenet-tms (LISTEN)
docker run -d -p 81:80 --name nginx-test-port1 nginx
docker run -d -p 192.168.56.199:82:80 --name nginx-test-port2 docker.io/nginx
docker run -d -p 192.168.56.199::80 --name nginx-test-port3 docker.io/nginx
docker run -d -p 192.168.56.199:83:80/udp --name nginx-test-port4 docker.io/nginx
docker run -d -p 86:80/tcp -p 443:443/tcp -p 54:54/udp --name nginx-test-port5 docker.io/nginx
[root@gbase8c_private ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e98563df7763 nginx "/docker-entrypoint.…" 31 seconds ago Up 30 seconds 0.0.0.0:54->54/udp, 0.0.0.0:443->443/tcp, 0.0.0.0:86->80/tcp nginx-test-port5
d906beba9990 nginx "/docker-entrypoint.…" 2 minutes ago Up 2 minutes 80/tcp, 192.168.56.199:83->80/udp nginx-test-port4
3a11ac02e895 nginx "/docker-entrypoint.…" 2 minutes ago Up 2 minutes 192.168.56.199:32768->80/tcp nginx-test-port3
8af8fc6162d8 nginx "/docker-entrypoint.…" 3 minutes ago Up 3 minutes 192.168.56.199:82->80/tcp nginx-test-port2
6f9517d4c2d6 nginx "/docker-entrypoint.…" 3 minutes ago Up 3 minutes 0.0.0.0:81->80/tcp nginx-test-port1
docker logs nginx-test-port3
docker logs -f nginx-test-port3
docker port nginx-test-port5
日志:
[root@gbase8c_private ~]# docker port nginx-test-port5
443/tcp -> 0.0.0.0:443
54/udp -> 0.0.0.0:54
80/tcp -> 0.0.0.0:86
docker run -it --name nginx-test nginx
docker run -d -P --name nginx-test1 docker.io/nginx
docker run -d --name centos-test1 docker.io/centos
[root@gbase8c_private ~]# docker run -d --name centos-test1 docker.io/centos
e5ff3cdb2754c37f706e29dd4be5a37e755e46889cada528562def7798899af2
创建容器后直接进入,执行exit退出后容器关闭
docker run -t -i --name test-centos2 docker.io/centos /bin/bash
日志:
[root@gbase8c_private ~]# docker run -t -i --name test-centos2 docker.io/centos /bin/bash
[root@5cb9c84e7d17 /]# ps -aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 1.2 0.0 12036 2108 pts/0 Ss 01:55 0:00 /bin/bash
root 13 0.0 0.0 47572 2060 pts/0 R+ 01:55 0:00 ps -aux
容器退出后自动删除
docker run -it --rm --name nginx-delete-test docker.io/nginx
日志在4.7.13
容器需要有一个前台运行的进程才能保持容器的运行,通过传递运行参数是一种方式,另外一可以在构建镜像的时候指定容器启动时运行的前台命令。
docker run -d centos /usr/bin/tail -f ‘/etc/hosts’
日志:
[root@gbase8c_private ~]# docker run -d centos /usr/bin/tail -f '/etc/hosts'
74c0f99166085629ed7c488769505a7c35c9a98f96962594593ff0cc8960485d
[root@gbase8c_private ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
74c0f9916608 centos "/usr/bin/tail -f /e…" 3 seconds ago Up 3 seconds competent_jepsen
bd16c6917a24 nginx "/docker-entrypoint.…" 4 minutes ago Up 4 minutes 80/tcp nginx-delete-test1
docker stop 容器ID
docker start 容器ID
日志:
[root@gbase8c_private ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
74c0f9916608 centos "/usr/bin/tail -f /e…" 3 seconds ago Up 3 seconds competent_jepsen
bd16c6917a24 nginx "/docker-entrypoint.…" 4 minutes ago Up 4 minutes 80/tcp nginx-delete-test1
c844e569456f nginx "/docker-entrypoint.…" 4 minutes ago Up 4 minutes 80/tcp nginx-delete-test
e98563df7763 nginx "/docker-entrypoint.…" 18 minutes ago Up 18 minutes 0.0.0.0:54->54/udp, 0.0.0.0:443->443/tcp, 0.0.0.0:86->80/tcp nginx-test-port5
d906beba9990 nginx "/docker-entrypoint.…" 20 minutes ago Up 20 minutes 80/tcp, 192.168.56.199:83->80/udp nginx-test-port4
3a11ac02e895 nginx "/docker-entrypoint.…" 20 minutes ago Up 20 minutes 192.168.56.199:32768->80/tcp nginx-test-port3
8af8fc6162d8 nginx "/docker-entrypoint.…" 20 minutes ago Up 20 minutes 192.168.56.199:82->80/tcp nginx-test-port2
6f9517d4c2d6 nginx "/docker-entrypoint.…" 21 minutes ago Up 21 minutes 0.0.0.0:81->80/tcp nginx-test-port1
[root@gbase8c_private ~]# docker stop bd16c6917a24
bd16c6917a24
[root@gbase8c_private ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
74c0f9916608 centos "/usr/bin/tail -f /e…" About a minute ago Up About a minute competent_jepsen
c844e569456f nginx "/docker-entrypoint.…" 5 minutes ago Up 5 minutes 80/tcp nginx-delete-test
e98563df7763 nginx "/docker-entrypoint.…" 19 minutes ago Up 19 minutes 0.0.0.0:54->54/udp, 0.0.0.0:443->443/tcp, 0.0.0.0:86->80/tcp nginx-test-port5
d906beba9990 nginx "/docker-entrypoint.…" 21 minutes ago Up 21 minutes 80/tcp, 192.168.56.199:83->80/udp nginx-test-port4
3a11ac02e895 nginx "/docker-entrypoint.…" 21 minutes ago Up 21 minutes 192.168.56.199:32768->80/tcp nginx-test-port3
8af8fc6162d8 nginx "/docker-entrypoint.…" 22 minutes ago Up 22 minutes 192.168.56.199:82->80/tcp nginx-test-port2
6f9517d4c2d6 nginx "/docker-entrypoint.…" 22 minutes ago Up 22 minutes 0.0.0.0:81->80/tcp nginx-test-port1
[root@gbase8c_private ~]# docker start bd16c6917a24
Error response from daemon: No such container: bd16c6917a24
Error: failed to start containers: bd16c6917a24
[root@gbase8c_private ~]# docker stop 74c0f9916608
74c0f9916608
[root@gbase8c_private ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c844e569456f nginx "/docker-entrypoint.…" 6 minutes ago Up 6 minutes 80/tcp nginx-delete-test
e98563df7763 nginx "/docker-entrypoint.…" 19 minutes ago Up 19 minutes 0.0.0.0:54->54/udp, 0.0.0.0:443->443/tcp, 0.0.0.0:86->80/tcp nginx-test-port5
d906beba9990 nginx "/docker-entrypoint.…" 22 minutes ago Up 22 minutes 80/tcp, 192.168.56.199:83->80/udp nginx-test-port4
3a11ac02e895 nginx "/docker-entrypoint.…" 22 minutes ago Up 22 minutes 192.168.56.199:32768->80/tcp nginx-test-port3
8af8fc6162d8 nginx "/docker-entrypoint.…" 22 minutes ago Up 22 minutes 192.168.56.199:82->80/tcp nginx-test-port2
6f9517d4c2d6 nginx "/docker-entrypoint.…" 23 minutes ago Up 23 minutes 0.0.0.0:81->80/tcp nginx-test-port1
[root@gbase8c_private ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
74c0f9916608 centos "/usr/bin/tail -f /e…" 2 minutes ago Exited (137) 11 seconds ago competent_jepsen
c844e569456f nginx "/docker-entrypoint.…" 6 minutes ago Up 6 minutes 80/tcp nginx-delete-test
e5ff3cdb2754 centos "/bin/bash" 7 minutes ago Exited (0) 7 minutes ago centos-test1
5cb9c84e7d17 centos "/bin/bash" 11 minutes ago Exited (130) 7 minutes ago test-centos2
264f9bd66c3f nginx "/docker-entrypoint.…" 15 minutes ago Exited (0) 15 minutes ago nginx-test
e98563df7763 nginx "/docker-entrypoint.…" 20 minutes ago Up 20 minutes 0.0.0.0:54->54/udp, 0.0.0.0:443->443/tcp, 0.0.0.0:86->80/tcp nginx-test-port5
d906beba9990 nginx "/docker-entrypoint.…" 22 minutes ago Up 22 minutes 80/tcp, 192.168.56.199:83->80/udp nginx-test-port4
3a11ac02e895 nginx "/docker-entrypoint.…" 22 minutes ago Up 22 minutes 192.168.56.199:32768->80/tcp nginx-test-port3
8af8fc6162d8 nginx "/docker-entrypoint.…" 22 minutes ago Up 22 minutes 192.168.56.199:82->80/tcp nginx-test-port2
6f9517d4c2d6 nginx "/docker-entrypoint.…" 23 minutes ago Up 23 minutes 0.0.0.0:81->80/tcp nginx-test-port1
5f5d8410b29a nginx "/docker-entrypoint.…" 14 hours ago Exited (0) 14 hours ago musing_wozniak
e3e5fc354a71 hello-world "/hello" 15 hours ago Exited (0) 15 hours ago determined_mayer
[root@gbase8c_private ~]# docker start 74c0f9916608
74c0f9916608
[root@gbase8c_private ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
74c0f9916608 centos "/usr/bin/tail -f /e…" 2 minutes ago Up 5 seconds competent_jepsen
c844e569456f nginx "/docker-entrypoint.…" 7 minutes ago Up 7 minutes 80/tcp nginx-delete-test
e98563df7763 nginx "/docker-entrypoint.…" 21 minutes ago Up 20 minutes 0.0.0.0:54->54/udp, 0.0.0.0:443->443/tcp, 0.0.0.0:86->80/tcp nginx-test-port5
d906beba9990 nginx "/docker-entrypoint.…" 23 minutes ago Up 23 minutes 80/tcp, 192.168.56.199:83->80/udp nginx-test-port4
3a11ac02e895 nginx "/docker-entrypoint.…" 23 minutes ago Up 23 minutes 192.168.56.199:32768->80/tcp nginx-test-port3
8af8fc6162d8 nginx "/docker-entrypoint.…" 23 minutes ago Up 23 minutes 192.168.56.199:82->80/tcp nginx-test-port2
6f9517d4c2d6 nginx "/docker-entrypoint.…" 24 minutes ago Up 24 minutes 0.0.0.0:81->80/tcp nginx-test-port1
docker run -it centos centos bash
[root@gbase8c_private ~]# docker run --name centos-test3 -itd docker.io/centos /bin/bash
46d17824678e24c8642e9e782e21c556b041889192733869c9dba2f50c120347
[root@gbase8c_private ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
46d17824678e centos "/bin/bash" 5 seconds ago Up 4 seconds centos-test3
[root@gbase8c_private ~]# docker exec -it centos-test3 /bin/bash
[root@46d17824678e /]# ls
bin etc lib lost+found mnt proc run srv tmp var
dev home lib64 media opt root sbin sys usr
[root@46d17824678e /]# exit
exit
[root@gbase8c_private ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
46d17824678e centos "/bin/bash" 49 seconds ago Up 49 seconds centos-test3
yum install util-linux #安装nsenter命令
docker inspect -f "{{.NetworkSettings.IPAddress}}" 46d17824678e
docker inspect -f "{{.State.Pid}}" mydocker #获取到某个docker容器的PID,可以通过PID进入到容器内
docker inspect -f "{{.State.Pid}}" centos-test3
nsenter -t PID -m -u -i -n -p
日志:
[root@gbase8c_private ~]# docker inspect -f "{{.NetworkSettings.IPAddress}}" 46d17824678e
172.17.0.2
[root@gbase8c_private ~]# docker inspect -f "{{.State.Pid}}" centos-test3
8645
[root@gbase8c_private ~]# nsenter -t 8645 -m -u -i -n -p
[root@46d17824678e /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
chmod a+x docker-in.sh
./docker-in.sh centos-test3
日志:
[root@gbase8c_private docker]# cat docker-in.sh
#!/bin/bash
docker_in(){
NAME_ID=$1
PID=$(docker inspect -f "{{.State.Pid}}" ${NAME_ID})
nsenter -t ${PID} -m -u -i -n -p
}
docker_in $1
[root@gbase8c_private docker]# ./docker-in.sh centos-test3
[root@46d17824678e /]# pwd
/
[root@46d17824678e /]# exit
logout
[root@gbase8c_private docker]# ./docker-in.sh centos-test3
[root@46d17824678e /]# exit
logout
默认会将实例的ID添加到自己的hosts文件
日志:
[root@gbase8c_private docker]# docker run -it --name test-centos3 docker.io/centos /bin/bash
[root@cccb2108056e /]# cat /etc/hosts
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.3 cccb2108056e
#ping 容器ID
[root@cccb2108056e /]# ping cccb2108056e
PING cccb2108056e (172.17.0.3) 56(84) bytes of data.
64 bytes from cccb2108056e (172.17.0.3): icmp_seq=1 ttl=64 time=0.029 ms
64 bytes from cccb2108056e (172.17.0.3): icmp_seq=2 ttl=64 time=0.037 ms
64 bytes from cccb2108056e (172.17.0.3): icmp_seq=3 ttl=64 time=0.034 ms
^C
--- cccb2108056e ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2007ms
rtt min/avg/max/mdev = 0.029/0.033/0.037/0.005 ms
docker stop $(docker ps -a -q) #正常关闭所有运行中的容器
日志:
[root@gbase8c_private docker]# docker stop $(docker ps -a -q)
cccb2108056e
46d17824678e
docker kill $(docker ps -a -q)
日志:
[root@gbase8c_private docker]# docker kill $(docker ps -a -q)
Error response from daemon: Cannot kill container: cccb2108056e: Container cccb2108056e5fea630892d67139ad6ee2bfa666f9d0eae738ff8c5d34984d29 is not running
[root@gbase8c_private docker]# docker start cccb2108056e
cccb2108056e
[root@gbase8c_private docker]# docker kill $(docker ps -a -q)
cccb2108056e
docker rm -f docker ps -aq -f status=exited
日志:
[root@gbase8c_private docker]# docker rm -f `docker ps -aq -f status=exited`
cccb2108056e
docker rm -f $(docker ps -a -q)
日志:
[root@gbase8c_private docker]# docker run -itd --name centos-test4 docker.io/centos /bin/bash
64bf39002c67ff9aded56780315a4a8c4639a3189d891cdd35092e1c203d4f6d
[root@gbase8c_private docker]# docker rm -f $(docker ps -a -q)
64bf39002c67
261a6c2dd260
Dns服务,默认采用宿主机的dns地址
一是将dns地址配置在宿主机
二是将参数配置在docker启动脚本里面 -dns=1.1.1.1
docker run -it --rm --dns 223.6.6.6 centos bash
cat /etc/resolv.conf
日志:
[root@gbase8c_private docker]# docker run -it --rm --dns 223.6.6.6 centos bash
[root@c01a69476a06 /]# cat /etc/resolv.conf
nameserver 223.6.6.6