Linux-Docker镜像-基础操作命令

Docker镜像

Docker镜像还有启动容器所需要的文件系统及所需要的内容,因此镜像主要用于创建并启动docker容器。

Docker镜像含里面时一层层文件系统,叫做Union File System(Union FS 关联文件系统)

联合文件系统可以将多个目录挂载到一起从而形成一整个虚拟文件系统,该虚拟文件系统的目录结构就想普通Linux的目录结构一样,docker通过这些文件再加上宿主机的内核提供了一个Linux的虚拟环境,每一层文件系统叫做一层layer,联合文件系统可以对每一层文件系统设置三种权限,只读(readonly),读写(readwrite)和写出(whiteout-able),但是docker镜像中每一层文件系统都是只读的,构建镜像的时候,从一个最基本的操作系统开始,每个构建的操作都相当于做一层的修改,增加了一层文件系统,一层层往上叠加,上层的修改会覆盖底层该位置的可见性。

就像上层把底层遮住了 一样,当使用镜像的时候,我们只会看到一个完全的整体,不知道里面有几层也不需要知道里面有几层

 

搜索镜像:

在官方的docker仓库中搜索指定名称的docker镜像,也会有很多镜像。

docker search +镜像名称+加版本号

 

[root@centos7-liyj ~]#docker search centos
NAME                                         DESCRIPTION                                     STARS     
centos                                       The official build of CentOS.                   7265     
kasmweb/centos-7-desktop                     CentOS 7 desktop for Kasm Workspaces            23                   
continuumio/centos5_gcc5_base                                                                3                    
dokken/centos-7                              CentOS 7 image for kitchen-dokken               3                    
dokken/centos-stream-9                                                                       1                    
dokken/centos-stream-8                                                                       1                    
couchbase/centos7-systemd                    centos7-systemd images with additional debug…   1         
spack/centos7                                CentOS 7 with Spack preinstalled                1                    
datadog/centos-i386                                                                          0                    
dokken/centos-6                              CentOS 6 image for kitchen-dokken               0                    
spack/centos6                                CentOS 6 with Spack preinstalled                0                    
ustclug/centos                               Official CentOS Image with USTC Mirror          0                    
dokken/centos-8                              CentOS 8 image for kitchen-dokken               0                    
corpusops/centos-bare                        https://github.com/corpusops/docker-images/     0                    
bitnami/centos-extras-base                                                                   0                    
corpusops/centos                             centos corpusops baseimage                      0                    
couchbase/centos-72-java-sdk                                                                 0                    
couchbase/centos-72-jenkins-core                                                             0                    
bitnami/centos-base-buildpack                Centos base compilation image                   0                   
couchbase/centos-69-sdk-nodevtoolset-build                                                   0                    
fnndsc/centos-python3                        Source for a slim Centos-based Python3 image…   0                
couchbase/centos-69-sdk-build                                                                0                    
couchbase/centos-70-sdk-build                                                                0                    
dokken/centos-5                              EOL DISTRO: For use with kitchen-dokken, Bas…   0                    
spack/centos-stream                                                                          0                    
[root@centos7-liyj ~]#docker search nginx
NAME                                              DESCRIPTION                                     STARS    
nginx                                             Official build of Nginx.                        17183     
linuxserver/nginx                                 An Nginx container, brought to you by LinuxS…   173                  
bitnami/nginx                                     Bitnami nginx Docker Image                      137          
ubuntu/nginx                                      Nginx, a high-performance reverse proxy & we…   55                   
bitnami/nginx-ingress-controller                  Bitnami Docker Image for NGINX Ingress Contr…   19            
rancher/nginx-ingress-controller                                                                  10                   
ibmcom/nginx-ingress-controller                   Docker Image for IBM Cloud Private-CE (Commu…   4                    
clearlinux/nginx                                  Nginx reverse proxy server with the benefits…   4                    
bitnami/nginx-ldap-auth-daemon                                                                    3                    
kasmweb/nginx                                     An Nginx image based off nginx:alpine and in2                    
bitnami/nginx-exporter                                                                            2                    
rapidfort/nginx                                   RapidFort optimized, hardened image for NGINX   2                    
rancher/nginx-ingress-controller-defaultbackend                                                   2                    
vmware/nginx                                                                                      2                    
rancher/nginx                                                                                     2                    
bitnami/nginx-intel                                                                               1                    
vmware/nginx-photon                                                                               1                    
wallarm/nginx-ingress-controller                  Kubernetes Ingress Controller with Wallarm e…   1                    
ibmcom/nginx-ingress-controller-ppc64le           Docker Image for IBM Cloud Private-CE (Commu…   0                    
rancher/nginx-conf                                                                                0                    
rapidfort/nginx-ib                                RapidFort optimized, hardened image for NGIN…   0                    
rancher/nginx-ssl                                                                                 0                    
ibmcom/nginx-ppc64le                              Docker image for nginx-ppc64le                  0                    
continuumio/nginx-ingress-ws                                                                      0                    
rancher/nginx-ingress-controller-amd64                                                            0                    

下载镜像:

从docker仓库将镜像下载到本地,命令格式如下:

docker pull 仓库服务端:端口/项目名称/镜像名称:tag(版本)号

现在nginx镜像

[17:14:59 root@ubuntu-lyj ~]#docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
1efc276f4ff9: Downloading [====================>                              ]  13.04MB/31.37MB
baf2da91597d: Downloading [===================================>               ]  17.78MB/25.35MB
05396a986fd3: Download complete 
6a17c8e7063d: Waiting 
27e0d286aeab: Waiting 
b1349eea8fc5: Waiting 

下载完成

[17:14:59 root@ubuntu-lyj ~]#docker pull nginx
Using default tag: latest
latest: Pulling from library/nginx
1efc276f4ff9: Pull complete 
baf2da91597d: Pull complete 
05396a986fd3: Pull complete 
6a17c8e7063d: Pull complete 
27e0d286aeab: Pull complete 
b1349eea8fc5: Pull complete 
Digest: sha256:fd304ffea0c92df362f25021faf126d8307f52f352fe7635b7713e740388a3d7
Status: Downloaded newer image for nginx:latest

查看本地镜像:

下载完成的镜像比下载的大,因为下载完成后会解压

[17:17:31 root@ubuntu-lyj ~]#docker images
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
nginx        latest    b692a91e4e15   4 hours ago   142MB
  • REPOSITORY    镜像所属的仓库名称
  • TAG                    镜像版本号(标识符),默认为latest
  • IMAGE ID           镜像唯一ID标识
  • CREATED          镜像创建时间
  • SIZE                   镜像的大小

镜像导出:

可以将镜像从本地导出为一个压缩文件,然后复制到其他服务器进行导入使用

导出方法1:
[17:17:37 root@ubuntu-lyj ~]#docker save nginx -o /data/nginx.tar.gz [17:23:52 root@ubuntu-lyj ~]#ll /data/ total 142568 drwxr-xr-x 3 root root 4096 Aug 2 17:23 ./ drwxr-xr-x 24 root root 4096 Apr 4 22:23 ../ drwx------ 2 root root 16384 Apr 4 22:16 lost+found/ -rw------- 1 root root 145963520 Aug 2 17:23 nginx.tar.gz
导出方法2:
[
17:24:02 root@ubuntu-lyj ~]#docker save nginx > /data/nginx1.tar.gz [17:24:57 root@ubuntu-lyj ~]#ll /data/ total 285112 drwxr-xr-x 3 root root 4096 Aug 2 17:24 ./ drwxr-xr-x 24 root root 4096 Apr 4 22:23 ../ drwx------ 2 root root 16384 Apr 4 22:16 lost+found/ -rw-r--r-- 1 root root 145963520 Aug 2 17:24 nginx1.tar.gz -rw------- 1 root root 145963520 Aug 2 17:23 nginx.tar.gz

查看镜像内容

[17:25:35 root@ubuntu-lyj /data]#tar xvf nginx.tar.gz 
0b71b9002c2056073a621264c914f1778296599761548ff1e34d39dc883ed029/
0b71b9002c2056073a621264c914f1778296599761548ff1e34d39dc883ed029/VERSION
0b71b9002c2056073a621264c914f1778296599761548ff1e34d39dc883ed029/json
0b71b9002c2056073a621264c914f1778296599761548ff1e34d39dc883ed029/layer.tar
19dee729285a1f8f34b15874db48c7b4b60f6041914f2cad9b1de169795de836/
19dee729285a1f8f34b15874db48c7b4b60f6041914f2cad9b1de169795de836/VERSION
19dee729285a1f8f34b15874db48c7b4b60f6041914f2cad9b1de169795de836/json
19dee729285a1f8f34b15874db48c7b4b60f6041914f2cad9b1de169795de836/layer.tar
1c65531519b901e0508c411808f7dc2497a6549926bda748896f59aaddc4eed4/
1c65531519b901e0508c411808f7dc2497a6549926bda748896f59aaddc4eed4/VERSION
1c65531519b901e0508c411808f7dc2497a6549926bda748896f59aaddc4eed4/json
1c65531519b901e0508c411808f7dc2497a6549926bda748896f59aaddc4eed4/layer.tar
6387aff546d7c159c6e082ea910391bc9c331ff19f75c73d45780def09a8c989/
6387aff546d7c159c6e082ea910391bc9c331ff19f75c73d45780def09a8c989/VERSION
6387aff546d7c159c6e082ea910391bc9c331ff19f75c73d45780def09a8c989/json
6387aff546d7c159c6e082ea910391bc9c331ff19f75c73d45780def09a8c989/layer.tar
75e6a412e4bda1a8b9e5690c6f8c218c0649ca965207ff15f4f2e114e859ec86/
75e6a412e4bda1a8b9e5690c6f8c218c0649ca965207ff15f4f2e114e859ec86/VERSION
75e6a412e4bda1a8b9e5690c6f8c218c0649ca965207ff15f4f2e114e859ec86/json
75e6a412e4bda1a8b9e5690c6f8c218c0649ca965207ff15f4f2e114e859ec86/layer.tar
b692a91e4e1582db97076184dae0b2f4a7a86b68c4fe6f91affa50ae06369bf5.json
e9bee565b6a50b23d35427abf3f6756a842c5d75d3c8e3d63f412cb3dae90409/
e9bee565b6a50b23d35427abf3f6756a842c5d75d3c8e3d63f412cb3dae90409/VERSION
e9bee565b6a50b23d35427abf3f6756a842c5d75d3c8e3d63f412cb3dae90409/json
e9bee565b6a50b23d35427abf3f6756a842c5d75d3c8e3d63f412cb3dae90409/layer.tar
manifest.json
repositories
[17:25:51 root@ubuntu-lyj /data]#cat manifest.json   #包含了镜像的相关配置,配文件,分层
[{"Config":"b692a91e4e1582db97076184dae0b2f4a7a86b68c4fe6f91affa50ae06369bf5.json","RepoTags":["nginx:latest"],"Layers":["1c65531519b901e0508c411808f7dc2497a6549926bda748896f59aaddc4eed4/layer.tar","e9bee565b6a50b23d35427abf3f6756a842c5d75d3c8e3d63f412cb3dae90409/layer.tar","6387aff546d7c159c6e082ea910391bc9c331ff19f75c73d45780def09a8c989/layer.tar","75e6a412e4bda1a8b9e5690c6f8c218c0649ca965207ff15f4f2e114e859ec86/layer.tar","0b71b9002c2056073a621264c914f1778296599761548ff1e34d39dc883ed029/layer.tar","19dee729285a1f8f34b15874db48c7b4b60f6041914f2cad9b1de169795de836/layer.tar"]}]
[17:26:09 root@ubuntu-lyj /data]#ll
total 285152
drwxr-xr-x  9 root root      4096 Aug  2 17:25 ./
drwxr-xr-x 24 root root      4096 Apr  4 22:23 ../
drwxr-xr-x  2 root root      4096 Aug  2 13:17 0b71b9002c2056073a621264c914f1778296599761548ff1e34d39dc883ed029/
drwxr-xr-x  2 root root      4096 Aug  2 13:17 19dee729285a1f8f34b15874db48c7b4b60f6041914f2cad9b1de169795de836/
drwxr-xr-x  2 root root      4096 Aug  2 13:17 1c65531519b901e0508c411808f7dc2497a6549926bda748896f59aaddc4eed4/
drwxr-xr-x  2 root root      4096 Aug  2 13:17 6387aff546d7c159c6e082ea910391bc9c331ff19f75c73d45780def09a8c989/
drwxr-xr-x  2 root root      4096 Aug  2 13:17 75e6a412e4bda1a8b9e5690c6f8c218c0649ca965207ff15f4f2e114e859ec86/
-rw-r--r--  1 root root      7653 Aug  2 13:17 b692a91e4e1582db97076184dae0b2f4a7a86b68c4fe6f91affa50ae06369bf5.json
drwxr-xr-x  2 root root      4096 Aug  2 13:17 e9bee565b6a50b23d35427abf3f6756a842c5d75d3c8e3d63f412cb3dae90409/
drwx------  2 root root     16384 Apr  4 22:16 lost+found/
-rw-r--r--  1 root root       586 Jan  1  1970 manifest.json
-rw-r--r--  1 root root 145963520 Aug  2 17:24 nginx1.tar.gz
-rw-------  1 root root 145963520 Aug  2 17:23 nginx.tar.gz
-rw-r--r--  1 root root        88 Jan  1  1970 repositories

分层为了方便文件使用,即相同的文件可以共用

[{"Config":"配置文件.json","RepoTags":["docker.io/nginx:latest"],"Layers":["分层1//layer.tar","分层2/layer.tar","分层3/layer.tar"]}]

镜像导入

复制打包好的镜像到另外一台机器上

[17:33:17 root@ubuntu-lyj /data]#scp  /data/nginx.tar.gz 10.0.0.101:/data
The authenticity of host '10.0.0.101 (10.0.0.101)' can't be established.
ECDSA key fingerprint is SHA256:MTWz5GpZYQgmK+u/UeqLdeE/aCVyc0R7xghEfrJyNiM.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '10.0.0.101' (ECDSA) to the list of known hosts.
root@10.0.0.101's password: 
nginx.tar.gz                                                                  100%  139MB 148.0MB/s   00:00    
[17:33:29 root@ubuntu-lyj /data]#

导入命令 docker load  <   /path/to/file

[17:39:21 root@ubuntu-lyj ~]#docker load < /data/nginx.tar.gz 
92a4e8a3140f: Loading layer [==================================================>]  83.87MB/83.87MB
e3257a399753: Loading layer [==================================================>]  62.04MB/62.04MB
3a89c8160a43: Loading layer [==================================================>]  3.072kB/3.072kB
f91d0987b144: Loading layer [==================================================>]  4.096kB/4.096kB
bdc7a32279cc: Loading layer [==================================================>]  3.584kB/3.584kB
b539cf60d7bb: Loading layer [==================================================>]  7.168kB/7.168kB
Loaded image: nginx:latest
[17:40:11 root@ubuntu-lyj ~]#docker images
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
nginx        latest    b692a91e4e15   4 hours ago   142MB

删除镜像

  • docker rmi   +镜像名称
[17:33:29 root@ubuntu-lyj /data]#docker images
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
nginx        latest    b692a91e4e15   4 hours ago   142MB
[17:38:23 root@ubuntu-lyj /data]#docker rmi nginx
Untagged: nginx:latest
Untagged: nginx@sha256:fd304ffea0c92df362f25021faf126d8307f52f352fe7635b7713e740388a3d7
Deleted: sha256:b692a91e4e1582db97076184dae0b2f4a7a86b68c4fe6f91affa50ae06369bf5
Deleted: sha256:20fe57e949a4f70bf714590d9d5a78d158d12f4619d148619427a86dfc2e5a7a
Deleted: sha256:042a89e2d80d230c47e0f2add6e13a5958cf18b039f04a3751200937ef76ba03
Deleted: sha256:9e20f968300754f2d3ace5b726448b9a4249bb8196aded53b36ae8b6d3e8c174
Deleted: sha256:15e9cede496de643a978a58c0b49ef7beea83b368dfefc2e46fa0e8dd589f099
Deleted: sha256:d2850ddb0c4ca9f6289f624b27f987873e556c41250f5c5ed47a69c6c2529e4b
Deleted: sha256:92a4e8a3140f7a04a0e5a15793adef2d0e8889ed306a8f95a6cfb67cecb5f212
[17:38:36 root@ubuntu-lyj /data]#docker images
REPOSITORY   TAG       IMAGE ID   CREATED   SIZE

总结:企业使用镜像及常见操作

搜索、下载、导出、导入、删除

命令总结

#导入本地镜像
docker load -i /data/nginx.tar.gz
docker load < /data/nginx.tar.gz

#导出镜像
docker save nginx -o /data/nginx.tar.gz
docker save nginx > /data/nginx1.tar.gz

#删除镜像
docker rmi  镜像ID/镜像名称     #删除指定ID的镜像,通过镜像启动容器的时候镜像不能被删除,除非将容器全部关闭

#删除容器
docker rm  容器ID/容器名称

#强制删除正在运行的容器
docker rm  容器ID/容器名称 -f

容器操作基础命令

命令格式

docker run [选项] [镜像名] [shell 命令] [参数]
docker run [参数选项] [镜像名,必须在所有选项的后面] [/bin/echo 'hello wold'] #单次执行,没有自定义容器名称

docker run centos /bin/echo 'hello wold' #启动的容器在执行完shell命令就退出了
[17:51:58 root@ubuntu-lyj /data]#docker run centos /bin/echo 'hello wold'
hello wold

从镜像启动一个容器

会直接进入到容器,并随机生成容器ID和名称

退出容器不注销:ctrl+p+q

[17:54:28 root@ubuntu-lyj ~]#docker run -it docker.io/nginx bash
root@ba40f1c83a28:/# ll
bash: ll: command not found
root@ba40f1c83a28:/# [17:55:04 root@ubuntu-lyj ~]#

显示正在运行的容器

  • docker ps
  • docker ps -a  #显示所有容器(正在运行及关闭的所有容器)
[17:56:48 root@ubuntu-lyj ~]#docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS         PORTS     NAMES
ba40f1c83a28   nginx     "/docker-entrypoint.…"   2 minutes ago   Up 2 minutes   80/tcp    tender_euler

删除运行中的容器:

即使正在运行中的容器,也会被相知删除掉

[17:56:59 root@ubuntu-lyj ~]#docker rm -f ba40f1c83a28
ba40f1c83a28
[17:59:18 root@ubuntu-lyj ~]#docker images
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
nginx        latest    b692a91e4e15   5 hours ago   142MB
[17:59:28 root@ubuntu-lyj ~]#docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[17:59:33 root@ubuntu-lyj ~]#

随机映射端口

  • docker pull nginx  #下载nginx 镜像
  • docker run -P docker.io/nginx #前台启动并随机映射本地端口到容器的80

 前台启动的会话窗口无法进行其他操作,除非退出,但是退出后容器也会退出

[18:21:10 root@ubuntu-lyj ~]#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
2022/08/02 10:21:23 [notice] 1#1: using the "epoll" event method
2022/08/02 10:21:23 [notice] 1#1: nginx/1.23.1
2022/08/02 10:21:23 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6) 
2022/08/02 10:21:23 [notice] 1#1: OS: Linux 4.15.0-112-generic
2022/08/02 10:21:23 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2022/08/02 10:21:23 [notice] 1#1: start worker processes
2022/08/02 10:21:23 [notice] 1#1: start worker process 31
2022/08/02 10:21:23 [notice] 1#1: start worker process 32

随机端口映射,其实是默认从32768开始

本次随机的端口49153

[18:20:27 root@ubuntu-lyj ~]#ss -tnl
State        Recv-Q        Send-Q                  Local Address:Port                  Peer Address:Port        
LISTEN       0             128                     127.0.0.53%lo:53                         0.0.0.0:*           
LISTEN       0             128                           0.0.0.0:22                         0.0.0.0:*           
LISTEN       0             128                         127.0.0.1:6010                       0.0.0.0:*           
LISTEN       0             128                         127.0.0.1:6011                       0.0.0.0:*           
LISTEN       0             128                           0.0.0.0:49153                      0.0.0.0:*           
LISTEN       0             128                              [::]:22                            [::]:*           
LISTEN       0             128                             [::1]:6010                          [::]:*           
LISTEN       0             128                             [::1]:6011                          [::]:*           
LISTEN       0             128                              [::]:49153                         [::]:*           

 

 指定端口映射

方式1:本地端口81映射到容器80端口:
#docker run -p 81:80 --name nginx-test-port1 nginx

方式2:本地IP:本地端口:容器端口
#docker run -p 10.0.0.101:82:80 --name nginx-test-port2 docker.io/nginx

方式3:本地IP:本地随机端口:容器端口
#docker run -p 10.0.0.101::80 --name nginx-test-port3 docker.io/nginx

方式4:本机IP:本地端口:容器端口/协议,默认tcp协议
#docker run -p 10.0.0.101:83:80/udp --name nginx-test-port4 docker.io/nginx

方式5:一次性映射多个端口+协议
#docker run -p 86:80/tcp -p 443:443/tcp -p 53:53/udp --name nginx-test-port5 docker.io/nginx

docker run -d    #-d 后台运行

[19:22:45 root@ubuntu-lyj ~]#docker run -d -p 81:80 --name nginx-test-port1 nginx
7cb2a2bbd437848f9eff00addf810f8410228f2b129dd58d178cdab241bb22d6
[19:23:02 root@ubuntu-lyj ~]#docker run -d -p 10.0.0.101:82:80 --name nginx-test-port2 docker.io/nginx
354386106e3dd0ebda748c6ff524de46c571e7ef1067d86c82d30b77ded248dc
[19:23:26 root@ubuntu-lyj ~]#docker run -d -p 10.0.0.101::80 --name nginx-test-port3 docker.io/nginx
505cf45a1de2720805a3cd43e9e86235f25d67e5cfc3dfa3d7c29cfa9894383a
[19:23:41 root@ubuntu-lyj ~]#docker run -d -p 10.0.0.101:83:80/udp --name nginx-test-port4 docker.io/nginx
973beb9a9810f28a8befff82e749b9b9b63373ccdddd5152180aa6080aab358f
[19:29:15 root@ubuntu-lyj ~]#docker run -d -p 86:80/tcp -p 443:443/tcp -p 55:55/udp --name nginx-test-port5 docker.io/nginx
2b0827b23cb904bcdbb74c3347ed99bdfdf7ecc3af666980946270e35f049539
[19:29:42 root@ubuntu-lyj ~]#docker ps -a
CONTAINER ID  IMAGE  COMMAND                  CREATED          STATUS        PORTS                              NAMES
973beb9a9810  nginx  "/docker-entrypoint.…"   7 minutes ago    Up 7 minutes  80/tcp, 10.0.0.101:83->80/udp      nginx-test-port4
505cf45a1de2  nginx  "/docker-entrypoint.…"   7 minutes ago    Up 7 minutes  10.0.0.101:49154->80/tcp           nginx-test-port3
354386106e3d  nginx  "/docker-entrypoint.…"   8 minutes ago    Up 8 minutes  10.0.0.101:82->80/tcp              nginx-test-port2
7cb2a2bbd437  nginx  "/docker-entrypoint.…"   8 minutes ago    Up 8 minutes  0.0.0.0:81->80/tcp, :::81->80/tcp  nginx-test-port1
2b0827b23cb9  nginx  "/docker-entrypoint.…"   About a minute ago   Up About a minute   0.0.0.0:55->55/udp, :::55->55/udp, 0.0.0.0:443->443/tcp, :::443->443/tcp, 0.0.0.0:86->80/tcp, :::86->80/tcp   nginx-test-port5

 

查看nginx容器访问日志:

[19:32:50 root@ubuntu-lyj ~]#docker logs nginx-test-port4              #一次性查看
[19:35:15 root@ubuntu-lyj ~]#docker logs -f  nginx-test-port4           #持续性查看
/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
2022/08/02 11:23:57 [notice] 1#1: using the "epoll" event method
2022/08/02 11:23:57 [notice] 1#1: nginx/1.23.1
2022/08/02 11:23:57 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6) 
2022/08/02 11:23:57 [notice] 1#1: OS: Linux 4.15.0-112-generic
2022/08/02 11:23:57 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2022/08/02 11:23:57 [notice] 1#1: start worker processes
2022/08/02 11:23:57 [notice] 1#1: start worker process 31
2022/08/02 11:23:57 [notice] 1#1: start worker process 32

查看容器已经映射的端口:

[19:31:21 root@ubuntu-lyj ~]#docker port nginx-test-port5
55/udp -> 0.0.0.0:55
55/udp -> :::55
80/tcp -> 0.0.0.0:86
80/tcp -> :::86
443/tcp -> 0.0.0.0:443
443/tcp -> :::443

自定义容器名称

[19:29:34 root@ubuntu-lyj ~]#docker ps 
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS               NAMES
ee3aed9830f3   nginx     "/docker-entrypoint.…"   10 seconds ago   Up 9 seconds    80/tcp            nginx-test

 创建并进入容器:

[20:14:55 root@ubuntu-lyj ~]#docker run -it --name test-centos docker.io/centos /bin/bash
[root@c98e857dd515 /]# ps -aux
USER        PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root          1  0.1  0.1  12048  3320 pts/0    Ss   12:15   0:00 /bin/bash
root         14  0.0  0.1  47584  3484 pts/0    R+   12:15   0:00 ps -aux
[root@c98e857dd515 /]# ll
bash: ll: command not found
[root@c98e857dd515 /]# ls
bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
[root@c98e857dd515 /]# 

  [root@c98e857dd515 /]# exit
   exit
  [20:17:01 root@ubuntu-lyj ~]#

#退出 exit,执行exit命令退出后容器关闭

单词运行:

容器推出后自动删除

[20:19:08 root@ubuntu-lyj ~]#docker ps -a   #容器运行和已经停止的为空
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[20:20:03 root@ubuntu-lyj ~]#docker run -it --rm  --name nginx-delete-test 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
2022/08/02 12:20:39 [notice] 1#1: using the "epoll" event method
2022/08/02 12:20:39 [notice] 1#1: nginx/1.23.1
2022/08/02 12:20:39 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6) 
2022/08/02 12:20:39 [notice] 1#1: OS: Linux 4.15.0-112-generic
2022/08/02 12:20:39 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2022/08/02 12:20:39 [notice] 1#1: start worker processes
2022/08/02 12:20:39 [notice] 1#1: start worker process 30
2022/08/02 12:20:39 [notice] 1#1: start worker process 31
^C2022/08/02 12:20:45 [notice] 1#1: signal 2 (SIGINT) received, exiting
2022/08/02 12:20:45 [notice] 31#31: signal 2 (SIGINT) received, exiting
2022/08/02 12:20:45 [notice] 31#31: exiting
2022/08/02 12:20:45 [notice] 30#30: signal 2 (SIGINT) received, exiting
2022/08/02 12:20:45 [notice] 30#30: exiting
2022/08/02 12:20:45 [notice] 31#31: exit
2022/08/02 12:20:45 [notice] 30#30: exit
2022/08/02 12:20:45 [notice] 1#1: signal 17 (SIGCHLD) received from 30
2022/08/02 12:20:45 [notice] 1#1: worker process 30 exited with code 0
2022/08/02 12:20:45 [notice] 1#1: worker process 31 exited with code 0
2022/08/02 12:20:45 [notice] 1#1: exit
[20:20:45 root@ubuntu-lyj ~]#docker ps -a  #已经自动删除,没有记录
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

传递运行命令

容器需要有一个前台运行的进程才能保持容器的运行,通过传递运行参数是一种方式,另外也可以在构建镜像的时候指定容器启动时运行的前台命令。

[20:25:16 root@ubuntu-lyj ~]#docker run -d centos /usr/bin/tail -f 'etc/hosts'
e73b551fafbfb72c634ee3f9df3d71266cd2343c8f4ce2c4d18842142c72b246
[20:25:48 root@ubuntu-lyj ~]#docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS         PORTS     NAMES
e73b551fafbf   centos    "/usr/bin/tail -f et…"   5 seconds ago   Up 4 seconds             relaxed_gould

容器的启动和关闭:

[20:26:54 root@ubuntu-lyj ~]#docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED              STATUS              PORTS     NAMES
e73b551fafbf   centos    "/usr/bin/tail -f et…"   About a minute ago   Up About a minute             relaxed_gould
[20:27:33 root@ubuntu-lyj ~]#docker stop e73b551fafbf        #停止   
e73b551fafbf 
[20:27:58 root@ubuntu-lyj ~]#docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[20:28:00 root@ubuntu-lyj ~]#docker start  e73b551fafbf      #启动
e73b551fafbf
[20:28:06 root@ubuntu-lyj ~]#docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS        PORTS     NAMES
e73b551fafbf   centos    "/usr/bin/tail -f et…"   2 minutes ago   Up 1 second             relaxed_gould
[20:28:08 root@ubuntu-lyj ~]#

进入到正在运行的容器:

使用attach命令

使用方式docker attach 容器名, attch类似于vnc,操作会在各个容器界面显示,所有使用此方式进入容器的操作都是同步显示的且exit后容器将被关闭,且使用exit推出后容器关闭,不推荐使用

 

需要进入到有shell环境的容器

[10:22:49 root@ubuntu-lyj ~]#docker run  -d -it --name centos-test docker.io/centos
[10:24:53 root@ubuntu-lyj ~]#docker attach 9642120ae4aa
[root@9642120ae4aa /]# ls
bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
[root@9642120ae4aa /]# pwd
/

没有shell环境的容器

[10:21:29 root@ubuntu-lyj ~]#docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS         PORTS                               NAMES
8e63d78e5a33   nginx     "/docker-entrypoint.…"   6 minutes ago   Up 5 seconds   0.0.0.0:81->80/tcp, :::81->80/tcp   nginx-test-port1
[10:21:34 root@ubuntu-lyj ~]#docker attach 8e63d78e5a33  #docker attach DockerID





在另外一个窗口启动测试页面是否同步

 

 

 使用exec命令

执行单词命令与进入容器,通常使用此命令,虽然exit退出容器还在运行

[10:34:46 root@ubuntu-lyj ~]#docker exec -it centos-test 
"docker exec" requires at least 2 arguments.
See 'docker exec --help'.

Usage:  docker exec [OPTIONS] CONTAINER COMMAND [ARG...]

Run a command in a running container
[10:35:15 root@ubuntu-lyj ~]#docker exec -it centos-test /bin/bash   #docker exec -it 容器自定义名称  /bin/bash
[root@9642120ae4aa /]# ls
bin  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
[root@9642120ae4aa /]# exit
exit
[10:35:28 root@ubuntu-lyj ~]#docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS                               NAMES
9642120ae4aa   centos    "/bin/bash"              12 minutes ago   Up 3 minutes                                        centos-test
8e63d78e5a33   nginx     "/docker-entrypoint.…"   21 minutes ago   Up 12 minutes   0.0.0.0:81->80/tcp, :::81->80/tcp   nginx-test-port1

使用nsenter命令:

推荐使用此方式,nsenter命令需要通过PID进入容器内部,不过可以使用 docker inspect 获取到容器的PID

[10:41:19 root@ubuntu-lyj ~]#apt install util-linux

docker  inspect  镜像名称或DockerID

[12:02:34 root@ubuntu-lyj ~]#docker inspect centos
[
    {
        "Id": "sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6",
        "RepoTags": [
            "centos:latest"
        ],
        "RepoDigests": [
            "centos@sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177"
        ],
        "Parent": "",
        "Comment": "",
        "Created": "2021-09-15T18:20:05.184694267Z",
        "Container": "9bf8a9e2ddff4c0d76a587c40239679f29c863a967f23abf7a5babb6c2121bf1",
        "ContainerConfig": {
            "Hostname": "9bf8a9e2ddff",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
            ],
            "Cmd": [
                "/bin/sh",
                "-c",
                "#(nop) ",
                "CMD [\"/bin/bash\"]"
            ],
            "Image": "sha256:f5b050f177fd426be8fe998a8ecf3fb1858d7e26dff4080b29a327d1bd5ba422",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {
                "org.label-schema.build-date": "20210915",
                "org.label-schema.license": "GPLv2",
                "org.label-schema.name": "CentOS Base Image",
                "org.label-schema.schema-version": "1.0",
                "org.label-schema.vendor": "CentOS"
            }
        },
        "DockerVersion": "20.10.7",
        "Author": "",
        "Config": {
            "Hostname": "",
            "Domainname": "",
            "User": "",
            "AttachStdin": false,
            "AttachStdout": false,
            "AttachStderr": false,
            "Tty": false,
            "OpenStdin": false,
            "StdinOnce": false,
            "Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
            ],
            "Cmd": [
                "/bin/bash"
            ],
            "Image": "sha256:f5b050f177fd426be8fe998a8ecf3fb1858d7e26dff4080b29a327d1bd5ba422",
            "Volumes": null,
            "WorkingDir": "",
            "Entrypoint": null,
            "OnBuild": null,
            "Labels": {
                "org.label-schema.build-date": "20210915",
                "org.label-schema.license": "GPLv2",
                "org.label-schema.name": "CentOS Base Image",
                "org.label-schema.schema-version": "1.0",
                "org.label-schema.vendor": "CentOS"
            }
        },
        "Architecture": "amd64",
        "Os": "linux",
        "Size": 231268856,
        "VirtualSize": 231268856,
        "GraphDriver": {
            "Data": {
                "MergedDir": "/var/lib/docker/overlay2/9ebbe4a7c8840cc811532096a26c92def8e4cd76826e231d0cfb44365bc244c2/merged",
                "UpperDir": "/var/lib/docker/overlay2/9ebbe4a7c8840cc811532096a26c92def8e4cd76826e231d0cfb44365bc244c2/diff",
                "WorkDir": "/var/lib/docker/overlay2/9ebbe4a7c8840cc811532096a26c92def8e4cd76826e231d0cfb44365bc244c2/work"
            },
            "Name": "overlay2"
        },
        "RootFS": {
            "Type": "layers",
            "Layers": [
                "sha256:74ddd0ec08fa43d09f32636ba91a0a3053b02cb4627c35051aff89f853606b59"
            ]
        },
        "Metadata": {
            "LastTagTime": "0001-01-01T00:00:00Z"
        }
    }
]
查看容器镜像详细信息

取某个docker容器的PID

docker inspect -f "{{.State.Pid}}"   容器自定义名称或DockeID
[13:30:54 root@ubuntu-lyj ~]#docker inspect -f "{{.State.Pid}}" 8e63d78e5a33
3298
[13:31:12 root@ubuntu-lyj ~]#docker inspect -f "{{.State.Pid}}" nginx-test-port1
3298

进入容器

[13:31:23 root@ubuntu-lyj ~]#nsenter -t 3298 -m -u -i -n -p
root@8e63d78e5a33:/# ls
bin   dev           docker-entrypoint.sh  home  lib64  mnt  proc  run   srv  tmp  var
boot  docker-entrypoint.d  etc             lib   media  opt  root  sbin  sys  usr
root@8e63d78e5a33:/# 

nsenter命令,需要先取docker容器的PID,才能进入容器,操作繁琐,写成脚本进行调用

脚本方式

[13:35:08 root@ubuntu-lyj ~]#vim docker-in.sh
[13:38:03 root@ubuntu-lyj ~]#chmod a+x docker-in.sh 
[13:38:17 root@ubuntu-lyj ~]#./docker-in.sh nginx-test-port1
root@ubuntu-lyj:/# 
root@ubuntu-lyj:/# 
root@ubuntu-lyj:/# 
root@ubuntu-lyj:/# 
root@ubuntu-lyj:/# exit
logout
[13:38:39 root@ubuntu-lyj ~]#cat docker-in.sh 


#
!/bin/bash docker_in(){ NAME_ID=$1 PID=$(docker inspect -f "{{.State.Pid}}" ${NAME_ID}) nsenter -t ${PID} -m -i -n -p } docker_in $1

 

查看容器内部的hosts文件:

[13:38:45 root@ubuntu-lyj ~]#./docker-in.sh nginx-test-port1
root@ubuntu-lyj:/# 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.2    8e63d78e5a33  #默认会将实例的ID添加到自己的hosts文件

 

批量关闭正在运行的容器:

[13:42:04 root@ubuntu-lyj ~]#docker stop $(docker ps -a -q)
9642120ae4aa
728b689e73f2
8e63d78e5a33
405235c81420
[13:42:22 root@ubuntu-lyj ~]#docker ps 
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES

批量强制关闭正在运行的容器

[13:43:22 root@ubuntu-lyj ~]#docker kill  $(docker ps -a -q)
9642120ae4aa
728b689e73f2
8e63d78e5a33

批量删除已退出容器

[13:44:17 root@ubuntu-lyj ~]#docker rm -f `docker ps -aq -f status=exited`
9642120ae4aa
728b689e73f2
8e63d78e5a33
405235c81420

批量删除所有容器

[13:44:48 root@ubuntu-lyj ~]#docker rm -f $(docker ps -a -q)

 

posted @ 2022-08-06 13:47  goodbay说拜拜  阅读(1377)  评论(0编辑  收藏  举报