【CentOS7】【docker】常用操作命令
目錄
- 前言
- 查找鏡像
- 可視化查找鏡像
- docker命令查找鏡像
- 鏡像操作
- 拉取鏡像
- 查看本地已拉取的鏡像
- 刪除鏡像
- 創(chuàng)建容器
- 創(chuàng)建但不運(yùn)行容器
- 創(chuàng)建并運(yùn)行容器
- 管理容器
- 查看運(yùn)行著的容器
- 查看本地已創(chuàng)建的容器(運(yùn)行著的+未運(yùn)行著的)
- 刪除容器
- 啟動(dòng)容器
- 停止運(yùn)行容器
- 重啟容器
- 查看容器的運(yùn)行日志
- 更新容器配置
- 鏡像自動(dòng)啟動(dòng)/鏡像隨docker自動(dòng)啟動(dòng)
- 登錄到容器內(nèi)進(jìn)行操作
- 登錄到容器的控制臺(tái)
- 安裝vim命令
- 端口
- 查看容器的端口映射
- 修改docker容器配置,設(shè)置/修改端口映射
- IP
- 查看容器的IP
- 固定容器的IP地址
- 網(wǎng)絡(luò)
- 查看Docker創(chuàng)建的網(wǎng)絡(luò)
- Docker創(chuàng)建網(wǎng)絡(luò)
- 查看容器的網(wǎng)絡(luò)
前言
- CentOS Linux release 7.9.2009
- Docker version 19.03.14
- 先拉取鏡像 -> 通過鏡像創(chuàng)建容器 -> 啟動(dòng)、關(guān)閉、重啟容器 -> 再登錄到容器的控制臺(tái)進(jìn)行操作
查找鏡像
可視化查找鏡像
在 docker hub 上搜索鏡像。
docker命令查找鏡像
這種方式查找鏡像時(shí),結(jié)果比較糙,不能查看每個(gè)鏡像有哪些tag。
shell> docker search mysql NAME DESCRIPTION STARS OFFICIAL AUTOMATED mysql MySQL is a widely used, open-source relation… 10637 [OK] mariadb MariaDB Server is a high performing open sou… 3991 [OK] mysql/mysql-server Optimized MySQL Server Docker images. Create… 779 [OK] percona Percona Server is a fork of the MySQL relati… 528 [OK] centos/mysql-57-centos7 MySQL 5.7 SQL database server 87 mysql/mysql-cluster Experimental MySQL Cluster Docker images. Cr… 79 centurylink/mysql Image containing mysql. Optimized to be link… 59 [OK] bitnami/mysql Bitnami MySQL Docker Image 49 [OK] deitch/mysql-backup REPLACED! Please use http://hub.docker.com/r… 41 [OK] databack/mysql-backup Back up mysql databases to... anywhere! 41 prom/mysqld-exporter 37 [OK] tutum/mysql Base docker image to run a MySQL database se… 35 schickling/mysql-backup-s3 Backup MySQL to S3 (supports periodic backup… 29 [OK] linuxserver/mysql A Mysql container, brought to you by LinuxSe… 27 centos/mysql-56-centos7 MySQL 5.6 SQL database server 20 circleci/mysql MySQL is a widely used, open-source relation… 20 mysql/mysql-router MySQL Router provides transparent routing be… 18 arey/mysql-client Run a MySQL client from a docker container 17 [OK] fradelg/mysql-cron-backup MySQL/MariaDB database backup using cron tas… 12 [OK] yloeffler/mysql-backup This image runs mysqldump to backup data usi… 7 [OK] openshift/mysql-55-centos7 DEPRECATED: A Centos7 based MySQL v5.5 image… 6 devilbox/mysql Retagged MySQL, MariaDB and PerconaDB offici… 3 ansibleplaybookbundle/mysql-apb An APB which deploys RHSCL MySQL 2 [OK] jelastic/mysql An image of the MySQL database server mainta… 1 widdpim/mysql-client Dockerized MySQL Client (5.7) including Curl… 1 [OK]鏡像操作
拉取鏡像
shell> docker pull 鏡像名字:tag- tag 不寫時(shí),默認(rèn)為latest
比如:
shell> docker pull redis等價(jià)于
shell> docker pull redis:latest指定tag的示例:
shell> docker pull redis:6.2.1 shell> docker pull redis:6.2.1-alpine shell> docker pull redis:6.2.1-alpine3.13 shell> docker pull redis:6.2-buster查看本地已拉取的鏡像
shell> docker images -a --no-trunc REPOSITORY TAG IMAGE ID CREATED SIZE redis latest sha256:a617c1c92774952d26fb87ba9a32fdc4d424fb7be02bbc84d6fefb517f3d4c6c 9 days ago 105MB hello-world latest sha256:d1165f2212346b2bab48cb01c1e39ee8ad1be46b87873d9ca7a4e434980a7726 2 weeks ago 13.3kB rabbitmq 3-management sha256:800294d91e3181d14a3b4920bbf348b5d407fc603f91f2b6310ea07284d8f6d2 2 months ago 198MB mysql 5.6.40 sha256:50328380b2b44db6826909771a7059dbea575af52bc242dacd4f6e365f3592b7 2 years ago 256MB或者:
shell> docker images -a REPOSITORY TAG IMAGE ID CREATED SIZE redis latest a617c1c92774 9 days ago 105MB hello-world latest d1165f221234 2 weeks ago 13.3kB rabbitmq 3-management 800294d91e31 2 months ago 198MB mysql 5.6.40 50328380b2b4 2 years ago 256MB區(qū)別在于IMAGE ID。簡(jiǎn)寫的IMAGE ID也有效。
刪除鏡像
使用 docker rmi 刪除鏡像:
shell> docker rmi --helpUsage: docker rmi [OPTIONS] IMAGE [IMAGE...]Remove one or more imagesOptions:-f, --force Force removal of the image--no-prune Do not delete untagged parents示例:
# 通過 鏡像名字:tag 定位鏡像 shell> docker rmi mysql:5.6.40 Untagged: mysql:5.6.40 Untagged: mysql@sha256:4902cd80486dca889205576a698a3e6534376ef6d8b94ef9fc9c61e891f1cd15 Deleted: sha256:50328380b2b44db6826909771a7059dbea575af52bc242dacd4f6e365f3592b7 Deleted: sha256:697d89d941b59add425566c6c0c60d10bb4b21bf6f86ef7ddccb94ddae6cf20a Deleted: sha256:c3e5f383007dbd1b8a4c327429272f3ab98cd183b6c17dbe281c0db2af0c5af3 Deleted: sha256:27d9bcc11f4e4db5f8b4327ec3aa29fd323e36a6c8a47bc17b9893981a0f4e56 Deleted: sha256:578ecce506d2e9293a0d3cd898345901dec4aaa3650f6e98235617838968f86b Deleted: sha256:431c1168801316fa15f53294aa6769b97686371b056c9415672f700b0308bf2b Deleted: sha256:bff479cb74c3b47307144d06e2a36f81ff96ee897afe374ef1c6ff6d83776738 Deleted: sha256:202db300227b8c870b421aa0a0a11b44cb916401ef3a34bcc2a5efcb30a3ea78 Deleted: sha256:20f3dff2c1b6f8409b53e4e203132f7ebb2dfd7dc612d87e4a93302a5a8f5c1a Deleted: sha256:3131a4917b53634699929d8ff7e2b2bd9469f3f5ab08daf41d1c6b90e0f18b44 Deleted: sha256:9996a15396359708cb2177cfdbe8fcb9f65124142edb9b1d3550f3eb87360676 Deleted: sha256:cdb3f9544e4c61d45da1ea44f7d92386639a052c620d1550376f22f5b46981af # 通過 鏡像ID 定位鏡像 shell> docker rmi 50328380b2b4 # 通過 鏡像的長(zhǎng)ID 定位鏡像 shell> docker rmi 50328380b2b44db6826909771a7059dbea575af52bc242dacd4f6e365f3592b7刪除鏡像的限制:
- docker會(huì)檢查該鏡像是否已經(jīng)創(chuàng)建過容器。如果該鏡像已經(jīng)創(chuàng)建過容器,則不能刪除。
- 先刪除對(duì)應(yīng)的容器,再刪除鏡像。
創(chuàng)建容器
創(chuàng)建但不運(yùn)行容器
shell> docker create [OPTIONS] IMAGE [COMMAND] [ARG...]例如:
shell> docker create --name redis1 -p 6379:6379 redis --requirepass "redispassword"創(chuàng)建并運(yùn)行容器
shell> docker run [OPTIONS] IMAGE [COMMAND] [ARG...]例如:
shell> docker run -d --name redis1 -p 6379:6379 redis --requirepass "redispassword"- docker run [-d --name redis1 -p 6379:6379] redis --requirepass "redispassword" []中是 OPTIONS
- docker run -d --name redis1 -p 6379:6379 [redis] --requirepass "redispassword" []中是 IMAGE
- docker run -d --name redis1 -p 6379:6379 redis [--requirepass "redispassword"] []中是 ARG
管理容器
查看運(yùn)行著的容器
使用 docker ps 查看正在運(yùn)行的容器:
shell> docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 566cc23c3aa1 redis "docker-entrypoint.s…" 4 hours ago Up 2 hours 0.0.0.0:6379->6379/tcp redis1 9ac952ee4921 rabbitmq:3-management "docker-entrypoint.s…" 4 hours ago Up 2 hours 4369/tcp, 5671/tcp, 0.0.0.0:5672->5672/tcp, 15671/tcp, 15691-15692/tcp, 25672/tcp, 0.0.0.0:15672->15672/tcp rabbitmq1想看容器啟動(dòng)時(shí)執(zhí)行的命令:
shell> docker ps --no-trunc CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 566cc23c3aa1af21cd55c8c87bf80b7b7318914cf84defc9ec0b36aa815fc0d1 redis "docker-entrypoint.sh --requirepass redispass" 4 hours ago Up 2 hours 0.0.0.0:6379->6379/tcp redis1 9ac952ee49214c5e3ec43ef1c21e0580cf65439edc23cf4f1712a0572e41df20 rabbitmq:3-management "docker-entrypoint.sh rabbitmq-server" 4 hours ago Up 2 hours 4369/tcp, 5671/tcp, 0.0.0.0:5672->5672/tcp, 15671/tcp, 15691-15692/tcp, 25672/tcp, 0.0.0.0:15672->15672/tcp rabbitmq1查看本地已創(chuàng)建的容器(運(yùn)行著的+未運(yùn)行著的)
shell> docker ps -a --no-trunc CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES d66021564fd4260cc31840dd231a1c15e4928f46c4154f16f1cb8e524371e3cc redis "docker-entrypoint.sh --requirepass redispass" 35 seconds ago Exited (0) 28 seconds ago redisTest 566cc23c3aa1af21cd55c8c87bf80b7b7318914cf84defc9ec0b36aa815fc0d1 redis "docker-entrypoint.sh --requirepass redis#abc" 4 hours ago Up 2 hours 0.0.0.0:6379->6379/tcp redis1 9ac952ee49214c5e3ec43ef1c21e0580cf65439edc23cf4f1712a0572e41df20 rabbitmq:3-management "docker-entrypoint.sh rabbitmq-server" 4 hours ago Up 2 hours 4369/tcp, 5671/tcp, 0.0.0.0:5672->5672/tcp, 15671/tcp, 15691-15692/tcp, 25672/tcp, 0.0.0.0:15672->15672/tcp rabbitmq1 d582dd3bbcea15b94676a8a7cb1bd30214cc071853fa39cd7a22142a6ff51b62 hello-world "/hello" 4 hours ago Exited (0) 4 hours ago pedantic_elgamal刪除容器
使用 docker rm 容器:
shell> docker rm --helpUsage: docker rm [OPTIONS] CONTAINER [CONTAINER...]Remove one or more containersOptions:-f, --force Force the removal of a running container (uses SIGKILL)-l, --link Remove the specified link-v, --volumes Remove anonymous volumes associated with the container示例:
# 通過 容器名稱 定位容器 shell> docker rm redisTest # 通過 容器ID 定位容器 shell> docker rm d66021564fd4 # 通過 容器的長(zhǎng)ID 定位容器 shell> docker rm d66021564fd4260cc31840dd231a1c15e4928f46c4154f16f1cb8e524371e3cc刪除容器的限制:
- docker會(huì)檢查該容器是否處于運(yùn)行中。如果該容器處于運(yùn)行中,則不能刪除。
- 先停止運(yùn)行容器,再刪除容器。
啟動(dòng)容器
shell> docker start redis1停止運(yùn)行容器
shell> docker stop redis1重啟容器
shell> docker restart redis1查看容器的運(yùn)行日志
shell> docker logs -f redisTest 1:C 22 Mar 2021 09:35:32.126 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo 1:C 22 Mar 2021 09:35:32.126 # Redis version=6.2.1, bits=64, commit=00000000, modified=0, pid=1, just started 1:C 22 Mar 2021 09:35:32.126 # Configuration loaded 1:M 22 Mar 2021 09:35:32.127 * monotonic clock: POSIX clock_gettime 1:M 22 Mar 2021 09:35:32.127 * Running mode=standalone, port=6379. 1:M 22 Mar 2021 09:35:32.127 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128. 1:M 22 Mar 2021 09:35:32.127 # Server initialized 1:M 22 Mar 2021 09:35:32.127 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect. 1:M 22 Mar 2021 09:35:32.128 * Ready to accept connections更新容器配置
shell> docker update --restart=always redisTest- 給名為redisTest的容器添加配置項(xiàng)。相當(dāng)于docker run -d --restart=always --name redisTest -p 6380:6379 redis --requirepass "redispass"。
鏡像自動(dòng)啟動(dòng)/鏡像隨docker自動(dòng)啟動(dòng)
shell> docker update --restart=always redisTest- --restart=always 讓容器自動(dòng)運(yùn)行。
--restart選項(xiàng):
- no:默認(rèn)策略,不自動(dòng)重啟容器。
- on-failure:容器非正常退出時(shí)(退出狀態(tài)非0)自動(dòng)重啟容器。
- on-failure:3:容器非正常退出時(shí)自動(dòng)重啟容器,同時(shí)限制次數(shù)3次。
- always:總是自動(dòng)重啟容器。
- unless-stopped:除了已經(jīng)停止的容器外,自動(dòng)重啟。這里的已停止的容器指:在Docker守護(hù)進(jìn)程啟動(dòng)時(shí)就已停止了的容器。
登錄到容器內(nèi)進(jìn)行操作
登錄到容器的控制臺(tái)
幫助文檔:
shell> docker exec --helpUsage: docker exec [OPTIONS] CONTAINER COMMAND [ARG...]Run a command in a running containerOptions:-d, --detach Detached mode: run command in the background--detach-keys string Override the key sequence for detaching a container-e, --env list Set environment variables-i, --interactive Keep STDIN open even if not attached--privileged Give extended privileges to the command-t, --tty Allocate a pseudo-TTY-u, --user string Username or UID (format: <name|uid>[:<group|gid>])-w, --workdir string Working directory inside the container示例1:登錄控制臺(tái)
shell> docker exec -it redisTest /bin/bash root@d66021564fd4:/data# redis-cli 127.0.0.1:6379> ping (error) NOAUTH Authentication required. 127.0.0.1:6379> auth redispass OK 127.0.0.1:6379> ping PONG 127.0.0.1:6379> exit root@d66021564fd4:/data# exit exit shell>示例2:redis client
shell> docker exec -it redisTest redis-cli 127.0.0.1:6379> exit shell> docker exec -it redisTest redis-cli 127.0.0.1:6379> ping (error) NOAUTH Authentication required. 127.0.0.1:6379> auth redispass OK 127.0.0.1:6379> ping PONG 127.0.0.1:6379> exit shell>示例3:以root用戶登錄控制臺(tái)
shell> docker exec -it --user root redisTest /bin/bash root@d66021564fd4:/data#安裝vim命令
shell> docker exec -it redisTest root@d66021564fd4:/data# apt-get install vim Reading package lists... Done Building dependency tree Reading state information... Done The following additional packages will be installed:vim-common vim-runtime xxd Suggested packages:ctags vim-doc vim-scripts The following NEW packages will be installed:vim vim-common vim-runtime xxd 0 upgraded, 4 newly installed, 0 to remove and 25 not upgraded. Need to get 7390 kB of archives. After this operation, 33.7 MB of additional disk space will be used. Do you want to continue? [Y/n] y Get:1 http://deb.debian.org/debian buster/main amd64 xxd amd64 2:8.1.0875-5 [140 kB] Get:2 http://deb.debian.org/debian buster/main amd64 vim-common all 2:8.1.0875-5 [195 kB] Get:3 http://deb.debian.org/debian buster/main amd64 vim-runtime all 2:8.1.0875-5 [5775 kB] Get:4 http://deb.debian.org/debian buster/main amd64 vim amd64 2:8.1.0875-5 [1280 kB] Fetched 7390 kB in 13min 14s (9304 B/s) ... # 太長(zhǎng)了,省略一部分可能會(huì)遇到下面的錯(cuò)誤
shell> docker exec -it redisTest root@d66021564fd4:/data# apt-get install vim Reading package lists... Done Building dependency tree Reading state information... Done E: Unable to locate package vim此時(shí),需要更新一下apt-get命令
shell> docker exec -it redisTest root@d66021564fd4:/data# apt-get update Get:1 http://deb.debian.org/debian buster InRelease [122 kB] Get:2 http://repo.mysql.com/apt/debian buster InRelease [21.5 kB] Get:3 http://security.debian.org/debian-security buster/updates InRelease [65.4 kB] Get:4 http://repo.mysql.com/apt/debian buster/mysql-5.7 amd64 Packages [5673 B] Get:5 http://deb.debian.org/debian buster-updates InRelease [51.9 kB] Get:6 http://deb.debian.org/debian buster/main amd64 Packages [7906 kB] Get:7 http://security.debian.org/debian-security buster/updates/main amd64 Packages [308 kB] Get:8 http://deb.debian.org/debian buster-updates/main amd64 Packages [15.2 kB] Fetched 8496 kB in 6min 33s (21.6 kB/s) Reading package lists... Done然后再安裝vim命令即可
端口
查看容器的端口映射
shell> docker port redisTest 6379/tcp -> 0.0.0.0:6380修改docker容器配置,設(shè)置/修改端口映射
參考這里:【docker】修改docker容器配置,設(shè)置/修改端口映射
IP
查看容器的IP
shell> docker inspect -f '{{.Name}} - {{.NetworkSettings.IPAddress }}' redisTest /redisTest - 172.17.0.2- docker 網(wǎng)絡(luò)模式參考這里。
- 命令解釋參考這里,這里。
固定容器的IP地址
- 創(chuàng)建新的網(wǎng)絡(luò)(docker默認(rèn)的網(wǎng)絡(luò)不支持指派固定IP)
- docker run 或 docker create 命令中指定網(wǎng)絡(luò)和IP地址
參考:
- https://www.cnblogs.com/xuezhigu/p/8257129.html
- https://blog.csdn.net/baidu_26504655/article/details/90409811
網(wǎng)絡(luò)
查看Docker創(chuàng)建的網(wǎng)絡(luò)
shell> docker network ls NETWORK ID NAME DRIVER SCOPE de30d6f965d0 bridge bridge local 80fed3e97a40 host host local 0c8ca63a758d kafka1_default bridge local ad16e24b37de none null local shell> docker network ls --no-trunc NETWORK ID NAME DRIVER SCOPE 0c3ad235b6e5502be222590a5edb3d93cf949fefcbd37a24caecfeb6ebec3768 bridge bridge local 17b6e488a32811eba75f1d622c5549b21ce700b2bcc267a1e4419ba6b4193339 host host local 8c553b00471ea945b62f4ed0baf2800c3bcf6ee7c2d137dd9cc478d635d7d3a2 kafka1_default bridge local 3fede20325dd4ca9efde17d4c91e6fca3663a452d2c2208430d555d9577743fb none null localDocker創(chuàng)建網(wǎng)絡(luò)
shell> docker network create --subnet=172.19.0.0/16 mynetwork 3ec9da56ffec8a748f0a3d032d90d556ea9b458870165df1fca9769aa0a728e4 shell> docker network ls NETWORK ID NAME DRIVER SCOPE 3ec9da56ffec mynetwork bridge local ... shell> ifconfig br-3ec9da56ffec: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500inet 172.19.0.1 netmask 255.255.0.0 broadcast 0.0.0.0ether 02:42:a9:2d:94:a7 txqueuelen 0 (Ethernet)RX packets 123595 bytes 7722085 (7.3 MiB)RX errors 0 dropped 0 overruns 0 frame 0TX packets 130743 bytes 9963707 (9.5 MiB)TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0查看容器的網(wǎng)絡(luò)
容器的配置文件中的Networks節(jié)點(diǎn)配置該容器的網(wǎng)絡(luò)(可理解為網(wǎng)卡),配置容器連接docker的哪個(gè)網(wǎng)絡(luò)、使用哪個(gè)IP等。
容器的配置文件中的NetworkMode節(jié)點(diǎn)配置使用哪個(gè)網(wǎng)絡(luò)(可理解為激活哪個(gè)網(wǎng)卡)。容器可具備多個(gè)網(wǎng)絡(luò)配置,可通過配置進(jìn)行網(wǎng)絡(luò)切換。
看看下面的內(nèi)容有助于理解上面的內(nèi)容:
- 查看docker下創(chuàng)建了哪些網(wǎng)絡(luò):
- 查看kafka1_kafka_1所屬的網(wǎng)絡(luò):
- 查看redis1所屬的網(wǎng)絡(luò):
總結(jié)
以上是生活随笔為你收集整理的【CentOS7】【docker】常用操作命令的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 风衣腰带系法(风衣背后腰带的7种系法)
- 下一篇: 百加手机电池电量用百分比显示如何设置?