Docker
type
status
date
slug
summary
tags
category
icon
password

镜像以及仓库

搜索镜像

//-f STARS=3000 搜索 STARS 大于 300 [root@localhost ~]# docker search mysql -f STARS=3000 NAME      DESCRIPTION                                      STARS     OFFICIAL   AUTOMATED mysql     MySQL is a widely used, open-source relation…   14043     [OK] mariadb   MariaDB Server is a high performing open sou…   5357     [OK]

下载镜像

[root@localhost ~]# docker pull centos Using default tag: latest latest: Pulling from library/centos a1d0c7532777: Pull complete Digest: sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177 Status: Downloaded newer image for centos:latest docker.io/library/centos:latest

下载指定的标签tag

docker pull centos:8.3.2011 [root@localhost /]# docker pull centos:8.3.2011 8.3.2011: Pulling from library/centos Digest: sha256:5528e8b1b1719d34604c87e11dcd1c0a20bedf46e83b5632cdeac91b8c04efc1 Status: Downloaded newer image for centos:8.3.2011 docker.io/library/centos:8.3.2011

查看本地镜像

[root@localhost ~]# docker images REPOSITORY                                  TAG            IMAGE ID       CREATED         SIZE registry.sim.scutbot.cn/simulatorx/2023ul   beta-1.1.1.5   b6e6318c172b   2 weeks ago     940MB minio/minio                                 latest         c5442c4190b8   2 months ago    254MB whyour/qinglong                             2.10.13        5997b3824cbe   15 months ago   434MB centos                                      latest         5d0da3dc9764   19 months ago   231MB

给镜像打标签

[root@localhost ~]# docker tag b6e6318c172b docker.io/scutbot/simlatorx [root@localhost ~]# docker images REPOSITORY                                  TAG            IMAGE ID       CREATED         SIZE registry.sim.scutbot.cn/simulatorx/2023ul   beta-1.1.1.5   b6e6318c172b   2 weeks ago     940MB scutbot/simlatorx                           latest         b6e6318c172b   2 weeks ago     940MB minio/minio                                 latest         c5442c4190b8   2 months ago    254MB whyour/qinglong                             2.10.13        5997b3824cbe   15 months ago   434MB centos                                      latest         5d0da3dc9764   19 months ago   231MB [root@localhost ~]# docker tag b6e6318c172b docker.io/scutbot/simlatorx:v1.0.1 [root@localhost ~]# docker images REPOSITORY                                  TAG            IMAGE ID       CREATED         SIZE scutbot/simlatorx                           latest         b6e6318c172b   2 weeks ago     940MB scutbot/simlatorx                           v1.0.1         b6e6318c172b   2 weeks ago     940MB registry.sim.scutbot.cn/simulatorx/2023ul   beta-1.1.1.5   b6e6318c172b   2 weeks ago     940MB minio/minio                                 latest         c5442c4190b8   2 months ago    254MB whyour/qinglong                             2.10.13        5997b3824cbe   15 months ago   434MB centos                                      latest         5d0da3dc9764   19 months ago   231MB

删除镜像

[root@localhost ~]# docker images | grep scutbot scutbot/simlatorx                           latest         b6e6318c172b   2 weeks ago     940MB scutbot/simlatorx                           v1.0.1         b6e6318c172b   2 weeks ago     940MB registry.sim.scutbot.cn/simulatorx/2023ul   beta-1.1.1.5   b6e6318c172b   2 weeks ago     940MB //删除对应为lasest标签的镜像 [root@localhost ~]# docker rmi scutbot/simlatorx:latest Untagged: scutbot/simlatorx:latest [root@localhost ~]# docker images | grep scutbot scutbot/simlatorx                           v1.0.1         b6e6318c172b   2 weeks ago     940MB registry.sim.scutbot.cn/simulatorx/2023ul   beta-1.1.1.5   b6e6318c172b   2 weeks ago     940MB //删除同样imagesID的镜像 [root@localhost /]# docker rmi -f 300e315adb2f Untagged: itying/centos:v1.0.1 Untagged: centos:8.3.2011

本地镜像推送

[root@localhost ~]# docker login Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one. Username: dreamini Password: ******* WARNING! Your password will be stored unencrypted in /root/.docker/config.json. Configure a credential helper to remove this warning. See https://docs.docker.com/engine/reference/commandline/login/#credentials-store Login Succeeded [root@localhost ~]# cat .docker/config.json { "auths": { "https://index.docker.io/v1/": { "auth": "ZHJlYW1pbmk6dipjMlVtaTYmLXRCTEIy" } } }

推送本地镜像到远程

[root@localhost ~]# docker tag 9ed4aefc74f6 docker.io/dreamini/alpine:v1.01 [root@localhost ~]# docker push docker.io/dreamini/alpine:v1.01 The push refers to repository [docker.io/dreamini/alpine] f1417ff83b31: Pushed v1.01: digest: sha256:b6ca290b6b4cdcca5b3db3ffa338ee0285c11744b4a6abaa9627746ee3291d8d size: 528 [root@localhost ~]#
如果遇到*denied: requested access to the resource is denied* ,意思是没有权限,请将标签打为docker.io/本人用户名/***

查看容器操作日志

docker logs [OPTIONS] CONTAINER
[OPTIONS]
  • f:跟踪日志输出
  • -since:显示某个时间开始的所有日志
  • t:显示时间戳
  • -tail:仅列出最新N条容器日志

镜像的导入导出

  1. 镜像导出
[root@VM-12-2-centos ~]# docker images REPOSITORY       TAG       IMAGE ID       CREATED         SIZE alpine           latest   5e2b554c1c45   3 weeks ago     7.33MB minio/minio       latest   c5442c4190b8   3 months ago   254MB whyour/qinglong   2.10.13   5997b3824cbe   17 months ago   434MB [root@VM-12-2-centos ~]# docker save 5e2b554c1c45 > dreamin.apline.txt.tar [root@VM-12-2-centos ~]# ls bt-mysql-install.log bt-pure-ftpd.log dreamin.apline.txt.tar ghproxy.sh mysql.sh off_install.sh phpmyadmin.sh
  1. 镜像导入
[root@VM-12-2-centos ~]# docker load < dreamin.apline_1.txt.tar bb01bd7e32b5: Loading layer [==================================================>]  7.618MB/7.618MB Loaded image ID: sha256:5e2b554c1c45d22c9d1aa836828828e320a26011b76c08631ac896cbc3625e3e [root@VM-12-2-centos ~]# docker images REPOSITORY       TAG       IMAGE ID       CREATED         SIZE <none>           <none>   5e2b554c1c45   3 weeks ago     7.33MB minio/minio       latest   c5442c4190b8   3 months ago   254MB whyour/qinglong   2.10.13   5997b3824cbe   17 months ago   434MB [root@VM-12-2-centos ~]# docker tag 5e2b554c1c45 dremain/alpine:latest [root@VM-12-2-centos ~]# docker images REPOSITORY       TAG       IMAGE ID       CREATED         SIZE dremain/alpine   latest   5e2b554c1c45   3 weeks ago     7.33MB minio/minio       latest   c5442c4190b8   3 months ago   254MB whyour/qinglong   2.10.13   5997b3824cbe   17 months ago   434MB

容器

类似 linux 系统环境,运行和隔离应用。容器从镜像启动的时候,docker会在镜像的最上一层创建一个可写层,镜像本身是只读的,保持不变。容器与镜像的关系,就如同面向编程中 对象与类之间的关系。 因为容器是通过镜像来创建的,所以必须先有镜像才能创建容器,而生成的容器是一个独立 于宿主机的隔离进程,并且有属于容器自己的网络和命名空间。

查看容器

[root@localhost ~]# docker ps CONTAINER ID   IMAGE                                                    COMMAND                    CREATED        STATUS      PORTS                                                                                  NAMES 2970f02eee30   registry.sim.scutbot.cn/simulatorx/2023ul:beta-1.1.1.5   "/bin/sh -c '\"python…"   3 weeks ago    Up 5 days                                                                                          SimulatorX-2023ul 132b83309076   minio/minio                                              "/usr/bin/docker-ent…"    2 months ago   Up 5 days   0.0.0.0:9000->9000/tcp, :::9000->9000/tcp, 0.0.0.0:9090->9090/tcp, :::9090->9090/tcp   minio1 c66fc605c364   whyour/qinglong:2.10.13                                  "./docker/docker-ent…"    2 months ago   Up 5 days   0.0.0.0:5700->5700/tcp, :::5700->5700/tcp                                              qinglong

run容器

-i :表示启动一个可交互的容器,并持续打开标准输入 -t: 表示使用终端关联到容器的标准输入输出上 -d :表示容器放置后台运行 --rm:退出后即删除容器 --name :表示定义容器唯一名称 -p 映射端口 -v 指定路径挂载数据卷 -e 运行容器传递环境变量 IMAGE :表示要运行的镜像 COMMAND :表示启动容器时要运行的命令

ti 创建交互式容器

[root@localhost ~]# docker run -ti centos /bin/bash [root@badc3a042c24 /]# ls bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var [root@badc3a042c24 /]# ip addr 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever 10: eth0@if11: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default link/ether 02:42:ac:11:00:04 brd ff:ff:ff:ff:ff:ff link-netnsid 0 inet 172.17.0.4/16 brd 172.17.255.255 scope global eth0 valid_lft forever preferred_lft forever //exit: 容器停止并退出 //ctrl+p+q:容器不停止退出 [root@localhost ~]# docker ps -a CONTAINER ID   IMAGE                                                   COMMAND                   CREATED             STATUS                       PORTS                                                                                 NAMES badc3a042c24   centos                                                   "/bin/bash"               About a minute ago   Up About a minute                                                                                                   peaceful_roentgen a4e36e1f4282   centos                                                   "/bin/bash"                2 minutes ago       Exited (130) 2 minutes ago                                                                                         gifted_hofstadter

-rm 启动一个退出即删除容器

[root@localhost ~]# docker run --rm centos /bin/echo hello hello

-d 启动一个后台容器

[root@localhost ~]# docker run -ti -d centos 70fac0e7d2dd215c3cde6d30a88392cbeefb8f7fd869aea63acfa7ad2b8d2488 [root@localhost ~]# docker ps CONTAINER ID   IMAGE                                                   COMMAND                   CREATED         STATUS         PORTS                                                                                 NAMES 70fac0e7d2dd   centos                                                   "/bin/bash"                16 seconds ago   Up 15 seconds                                                                                         beautiful_edison

exec/attach 进入置为后台已经启动的容器

[root@localhost ~]# docker exec -ti 70fac0e7d2dd /bin/sh sh-4.4# ls bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var [root@localhost ~]# docker exec -ti 70fac0e7d2dd /bin/bash [root@70fac0e7d2dd /]# ls bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var [root@localhost ~]# docker attach 70fac0e7d2dd [root@70fac0e7d2dd /]# ls bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var [root@70fac0e7d2dd /]# exit exit
区别:
  • docker exec:进入容器开启一个新的终端(常用)执行exit退出的时候不会停止容器
  • docker attach:进入容器正在执行的终端 exit 退出会停止容器

--name 启动容器的时候指定名称

[root@localhost ~]# docker run -it -d --name MyCentOs centos /bin/bash 9685a41507e9a02dc81f0fa2ead5d7bb7591e4e0ba9e429ba76b21e3a4ec6c93 [root@localhost ~]# docker ps CONTAINER ID   IMAGE                                                   COMMAND                   CREATED         STATUS         PORTS                                                                                 NAMES 9685a41507e9   centos                                                   "/bin/bash"                54 seconds ago   Up 53 seconds                                                                                         MyCentOs
另:start 启动 stop 停止 restart 重启容器 exit 退出容器

删除容器

[root@localhost ~]# docker rm MyCentOs MyCentOs [root@localhost ~]# docker rm -f badc3a042c24 badc3a042c24
删除所有容器docker rm -f $(docker ps -q)
根据容器的状态,删除 Exited 状态的容器
[root@localhost ~]# docker rm $(docker ps -qf status=exited) 70bb49df69ad 68a20b2c5e62 70fac0e7d2dd a4e36e1f4282

实现数据拷贝

docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH
docker cp [OPTIONS] SRC_PATH CONTAINER:DEST_PATH
[root@VM-12-2-centos ~]# docker cp /root/mysql.sh dc5e3e7058bc:/root/ [root@VM-12-2-centos ~]# docker exec -it dc5e3e7058bc /bin/bash [root@dc5e3e7058bc /]# cd root/ [root@dc5e3e7058bc ~]# ls anaconda-ks.cfg anaconda-post-nochroot.log anaconda-post.log buildinfo mysql.sh original-ks.cfg [root@dc5e3e7058bc ~]# exit exit [root@VM-12-2-centos ~]# docker cp dc5e3e7058bc:/root/mysql.sh /mnt [root@VM-12-2-centos ~]# cd /mnt [root@VM-12-2-centos mnt]# ls mysql.sh

部署Nginx 以及端口映射

[root@localhost ~]# docker pull nginx Using default tag: latest Trying to pull repository docker.io/library/nginx ... latest: Pulling from docker.io/library/nginx f03b40093957: Pull complete eed12bbd6494: Pull complete fa7eb8c8eee8: Pull complete 7ff3b2b12318: Pull complete 0f67c7de5f2c: Pull complete 831f51541d38: Pull complete Digest: sha256:af296b188c7b7df99ba960ca614439c99cb7cf252ed7bbc23e90cfda59092305 Status: Downloaded newer image for docker.io/nginx:latest [root@localhost ~]# docker images REPOSITORY         TAG                 IMAGE ID           CREATED             SIZE docker.io/nginx     latest             f9c14fe76d50        11 days ago         143 MB //打个标签 [root@localhost ~]# docker tag f9c14fe76d50 docker.io/dreamin/nginx:latest [root@localhost ~]# docker images REPOSITORY               TAG                 IMAGE ID           CREATED             SIZE docker.io/dreamin/nginx   latest             f9c14fe76d50        11 days ago         143 MB docker.io/nginx           latest             f9c14fe76d50        11 days ago         143 MB //运行容器 [root@localhost ~]# docker run -it -d --rm --name Mynginx dreamin/nginx 91ffd56990b0fc629630d1de34a6a3060835fe1a3550886df6f8bcf2e11ad80a [root@localhost ~]# docker ps \CONTAINER ID       IMAGE               COMMAND                 CREATED             STATUS             PORTS               NAMES 91ffd56990b0       dreamin/nginx       "/docker-entrypoin..."   10 seconds ago     Up 8 seconds        80/tcp             Mynginx [root@localhost ~]# docker exec -ti 91ffd56990b0 /bin/bash root@91ffd56990b0:/# curl 127.0.0.1 <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style> html { color-scheme: light dark; } body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style> </head> <body> <h1>Welcome to nginx!</h1> <p>If you see this page, the nginx web server is successfully installed and working. Further configuration is required.</p> <p>For online documentation and support please refer to <a href="http://nginx.org/">nginx.org</a>.<br/> Commercial support is available at <a href="http://nginx.com/">nginx.com</a>.</p> <p><em>Thank you for using nginx.</em></p> </body> </html> root@91ffd56990b0:/# exit exit

p 映射端口

notion image
从外部访问容器里面的nginx时,我们需要进行端口的映射。
运行命令为:docker run -p 容器外端口:容器内端口 容器 ID
[root@localhost ~]# docker run -ti -d --rm -p 81:80 91ffd56990b0 Unable to find image '91ffd56990b0:latest' locally Trying to pull repository docker.io/library/91ffd56990b0 ... /usr/bin/docker-current: repository docker.io/91ffd56990b0 not found: does not exist or no pull access. See '/usr/bin/docker-current run --help'.

挂载数据卷

运行命令为:docker run -v 容器外目录:容器内目录 容器ID
//创建目录 [root@localhost ~]# mkdir wwwroot [root@localhost ~]# ls install.sh wwwroot //把百度的index.html下载到本地 [root@localhost ~]# wget www.baidu.com -O index.html --2023-06-05 09:36:18-- http://www.baidu.com/ Resolving www.baidu.com (www.baidu.com)... 124.237.176.4, 124.237.176.3 Connecting to www.baidu.com (www.baidu.com)|124.237.176.4|:80... connected. HTTP request sent, awaiting response... 200 OK Length: 2381 (2.3K) [text/html] Saving to: ‘index.html’ 100%[=======================================================================================================================================================================================================>] 2,381 --.-K/s in 0s 2023-06-05 09:36:18 (214 MB/s) - ‘index.html’ saved [2381/2381] //运行容器并挂载目录 [root@localhost wwwroot]# dokcer run -ti -d --rm --name nginx_with_baidu -p 82:80 -v /root/wwwroot/:/usr/share/nginx/html dreamin/nginx -bash: dokcer: command not found [root@localhost wwwroot]# docker run -ti -d --rm --name nginx_with_baidu -p 82:80 -v /root/wwwroot/:/usr/share/nginx/html dreamin/nginx 01278235ffa023516f3ef921ae175f0ae6514b353839fd25e66a7231333e1f3e
notion image

匿名挂载数据卷

运行命令为:docker run -v 容器内目录 容器ID
//运行匿名容器 注:-P表示随机映射端口 [root@localhost ~]# docker run -d -P --name nginx01 -v /etc/nginx nginx 363f2b4ad961571dd872a2073f4f29549e7f842df73cca7cbc9e07bd41a24a18 [root@localhost ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 363f2b4ad961 nginx "/docker-entrypoin..." 17 seconds ago Up 15 seconds 0.0.0.0:32768->80/tcp nginx01 01278235ffa0 dreamin/nginx "/docker-entrypoin..." 16 minutes ago Up 16 minutes 0.0.0.0:82->80/tcp nginx_with_baidu 91ffd56990b0 dreamin/nginx "/docker-entrypoin..." 42 minutes ago Up 42 minutes 80/tcp Mynginx //查看挂载详情 [root@localhost ~]# docker volume ls DRIVER VOLUME NAME local 37539a0143126f17e4ad9a9044a6c9087414e2c371ea404a89d89cc65203e362 [root@localhost ~]# docker inspect 363f2b4ad961 | grep volume "Type": "volume", "Source": "/var/lib/docker/volumes/37539a0143126f17e4ad9a9044a6c9087414e2c371ea404a89d89cc65203e362/_data",
notion image

具体挂载数据卷

运行命令为:docker run -v 卷名称:容器内目录 容器ID
[root@localhost ~]# docker run -d -P --name nginx02 -v jumping:/etc/nginx nginx 9742bfe27d5cbb1efee7118512aa62727ef1668c12044f2d4e3f08ca63164d29 [root@localhost ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 9742bfe27d5c nginx "/docker-entrypoin..." 25 seconds ago Up 22 seconds 0.0.0.0:32769->80/tcp nginx02 363f2b4ad961 nginx "/docker-entrypoin..." 6 minutes ago Up 6 minutes 0.0.0.0:32768->80/tcp nginx01 01278235ffa0 dreamin/nginx "/docker-entrypoin..." 22 minutes ago Up 22 minutes 0.0.0.0:82->80/tcp nginx_with_baidu 91ffd56990b0 dreamin/nginx "/docker-entrypoin..." 49 minutes ago Up 49 minutes 80/tcp Mynginx [root@localhost ~]# docker volume ls DRIVER VOLUME NAME local 37539a0143126f17e4ad9a9044a6c9087414e2c371ea404a89d89cc65203e362 local jumping //查看挂载详情 [root@localhost ~]# docker volume inspect jumping [ { "Driver": "local", "Labels": null, "Mountpoint": "/var/lib/docker/volumes/jumping/_data", "Name": "jumping", "Options": {}, "Scope": "local" } ]
notion image

查看容器运行的细节

//查看详细的映射关系 [root@localhost ~]# docker inspect 01278235ffa0 | grep share "/root/wwwroot/:/usr/share/nginx/html" "Destination": "/usr/share/nginx/html",

运行容器传递环境变量

运行命令为:docker run -e 变量名=变量值 容器ID
//传递单个环境变量 [root@localhost ~]# docker run --rm -e name=dreamin dreamin/alpine printenv PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin HOSTNAME=9969e29efd62 name=dreamin HOME=/root //传递多个环境变量 [root@localhost ~]# docker run --rm -e name=dreamin -e age=21 dreamin/alpine printenv PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin HOSTNAME=ae7263222e57 name=dreamin age=21 HOME=/root
 
 
 


开往-友链接力