三、Docker的常用命令
一、帮助命令
docker version #显示docker的版本信息
docker info #显示docker的系统信息,system级别的 包括容器和镜像的数量
docker --help #万能命令,能看到所有的命令
文档地址:https://docs.docker.com/reference/
二、镜像命令
2.2.1docker images 查看当前主机的所有镜像
# 1. 查询 images 的帮助命令
[root@iZhp3do4qhzu84445osa3sZ ~]# docker images --help
Usage: docker images [OPTIONS] [REPOSITORY[:TAG]]
List images
Options:
-a, --all Show all images (default hides intermediate images)
--digests Show digests
-f, --filter filter Filter output based on conditions provided
--format string Pretty-print images using a Go template
--help Print usage
--no-trunc Don't truncate output
-q, --quiet Only show numeric IDs
# 2. docker images 查看所有本地主机上的镜像
[root@iZhp3do4qhzu84445osa3sZ ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/rabbitmq latest 7e50c60f7e3e 3 days ago 156 MB
docker.io/redis latest 235592615444 10 days ago 104 MB
docker.io/nginx latest 2622e6cca7eb 10 days ago 132 MB
docker.io/sonatype/nexus3 latest 57a6261043b9 2 months ago 644 MB
docker.io/hello-world latest bf756fb1ae65 5 months ago 13.3 kB
docker.io/mysql 5.7.25 984Z
REPOSITORY 当前镜像的仓库源
TAG 当前镜像的版本
IMAGE ID 当前镜像的ID
CREATED 当前创建时间
SIZE 当前镜像的文件大小
# asdzzzzzz z z z z
-a, --all # 列出所有的镜像
-q, --quiet # 只显示镜像的ID
2.2.2 docker search 搜索镜像
[root@iZhp3do4qhzu84445osa3sZ ~]# docker search mysql
INDEX NAME DESCRIPTION STARS OFFICIAL AUTOMATED
docker.io docker.io/mysql MySQL is a widely used, open-source relati... 9647 [OK]
docker.io docker.io/mariadb MariaDB is a community-developed fork of M... 3510 [OK]
docker.io docker.io/mysql/mysql-server Optimized MySQL Server Docker images. Crea... 704 [OK]
# 可选项
[root@iZhp3do4qhzu84445osa3sZ ~]# docker search --help
Usage: docker search [OPTIONS] TERM
Search the Docker Hub for images
Options:
-f, --filter filter
--help Print usage
--limit int Max number of search results (default 25)
--no-index Don't truncate output
--no-trunc Don't truncate output
# docker search mysql --filter=STARS=3000 通过filter来过滤名字为mysql 且stars大于3000的镜像
[root@iZhp3do4qhzu84445osa3sZ ~]# docker search mysql --filter=STARS=3000
INDEX NAME DESCRIPTION STARS OFFICIAL AUTOMATED
docker.io docker.io/mysql MySQL is a widely used, open-source relati... 9647 [OK]
docker.io docker.io/mariadb MariaDB is a community-developed fork of M... 3510 [OK]
2.2.3 docker pull 下载镜像
# docker pull 的帮助命令
[root@iZhp3do4qhzu84445osa3sZ ~]# docker pull --help
Usage: docker pull [OPTIONS] NAME[:TAG|@DIGEST]
Pull an image or a repository from a registry
Options:
-a, --all-tags Download all tagged images in the repository
--disable-content-trust Skip image verification (default true)
--help Print usage
# docker pull mysql
[root@iZhp3do4qhzu84445osa3sZ ~]# docker pull mysql
Using default tag: latest #默认下载最新的版本
Trying to pull repository docker.io/library/mysql ...
# 下载完成
[root@iZhp3do4qhzu84445osa3sZ ~]# docker pull mysql
Using default tag: latest
Trying to pull repository docker.io/library/mysql ...
latest: Pulling from docker.io/library/mysql
8559a31e96f4: Already exists
d51ce1c2e575: Pull complete # 分层下载 docker的核心 联合文件系统(后面讲)
c2344adc4858: Pull complete
fcf3ceff18fc: Pull complete
16da0c38dc5b: Pull complete
b905d1797e97: Pull complete
4b50d1c6b05c: Pull complete
c75914a65ca2: Pull complete
1ae8042bdd09: Pull complete
453ac13c00a3: Pull complete
9e680cd72f08: Pull complete
a6b5dc864b6c: Pull complete
Digest: sha256:8b7b328a7ff6de46ef96bcf83af048cb00a1c86282bfca0cb119c84568b4caf6 #签名
Status: Downloaded newer image for docker.io/mysql:latest #真实的地址
# docker pull 等价于 docker pull docker.io/mysql:latest
# 下载指定的版本
[root@iZhp3do4qhzu84445osa3sZ ~]# docker pull mysql:5.7 #版本一定是官方仓库支持的版本
Trying to pull repository docker.io/library/mysql ...
5.7: Pulling from docker.io/library/mysql
8559a31e96f4: Already exists #分层下载 很多镜像的内容是相同的就不会重复下载
d51ce1c2e575: Already exists #这样会极大地减少宿主机的内存损耗
c2344adc4858: Already exists
fcf3ceff18fc: Already exists
16da0c38dc5b: Already exists
b905d1797e97: Already exists
4b50d1c6b05c: Already exists
d85174a87144: Pull complete
a4ad33703fa8: Pull complete
f7a5433ce20d: Pull complete
3dcd2a278b4a: Pull complete
Digest: sha256:32f9d9a069f7a735e28fd44ea944d53c61f990ba71460c5c183e610854ca4854
Status: Downloaded newer image for docker.io/mysql:5.7
2.2.4 docker rmi 删除镜像
# docker rmi --help 删除镜像的帮助手册
[root@iZhp3do4qhzu84445osa3sZ ~]# docker rimi --help
Usage: docker COMMAND
A self-sufficient runtime for containers
Options:
--config string Location of client config files (default "/root/.docker")
-D, --debug Enable debug mode
--help Print usage
-H, --host list Daemon socket(s) to connect to (default [])
-l, --log-level string Set the logging level ("debug", "info", "warn", "error", "fatal") (default "info")
--tls Use TLS; implied by --tlsverify
--tlscacert string Trust certs signed only by this CA (default "/root/.docker/ca.pem")
--tlscert string Path to TLS certificate file (default "/root/.docker/cert.pem")
--tlskey string Path to TLS key file (default "/root/.docker/key.pem")
--tlsverify Use TLS and verify the remote
-v, --version Print version information and quit
# 根据image name 删除镜像
[root@iZhp3do4qhzu84445osa3sZ ~]# docker rmi docker.io/mysql
Untagged: docker.io/mysql:latest
Untagged: docker.io/mysql@sha256:8b7b328a7ff6de46ef96bcf83af048cb00a1c86282bfca0cb119c84568b4caf6
Deleted: sha256:be0dbf01a0f3f46fc8c88b67696e74e7005c3e16d9071032fa0cd89773771576
Deleted: sha256:086d66e8d1cb0d52e9337eabb11fb9b95960e2e1628d90100c62ea5e8bf72306
Deleted: sha256:f37c61ee1973b18c285d0d5fcf02da4bcdb1f3920981499d2a20b2858500a110
Deleted: sha256:e40b8bca7dc63fc8d188a412328e56caf179022f5e5d5b323aae57d233fb1069
Deleted: sha256:339f6b96b27eb035cbedc510adad2560132925a835f0afddbcc1d311c961c14b
Deleted: sha256:d38b06cdb26a5c98857ddbc6ef531d3f57b00e325c0c314600b712efc7ff6ab0
# 根据 IMAGE ID 删除镜像
[root@iZhp3do4qhzu84445osa3sZ ~]# docker rmi 9cfcce23593a
Untagged: docker.io/mysql:5.7
Untagged: docker.io/mysql@sha256:32f9d9a069f7a735e28fd44ea944d53c61f990ba71460c5c183e610854ca4854
Deleted: sha256:9cfcce23593a93135ca6dbf3ed544d1db9324d4c40b5c0d56958165bfaa2d46a
Deleted: sha256:98de3e212919056def8c639045293658f6e6022794807d4b0126945ddc8324be
Deleted: sha256:17e8b88858e400f8c5e10e7cb3fbab9477f6d8aacba03b8167d34a91dbe4d8c1
Deleted: sha256:c04c087c2af9abd64ba32fe89d65e6d83da514758923de5da154541cc01a3a1e
Deleted: sha256:ab8bf065b402b99aec4f12c648535ef1b8dc954b4e1773bdffa10ae2027d3e00
Deleted: sha256:09687cd9cdf4c704fde969fdba370c2d848bc614689712bef1a31d0d581f2007
Deleted: sha256:b704a4a65bf536f82e5d8b86e633d19185e26313de8380162e778feb2852011a
Deleted: sha256:c37206160543786228aa0cce738e85343173851faa44bb4dc07dc9b7dc4ff1c1
Deleted: sha256:12912c9ec523f648130e663d9d4f0a47c1841a0064d4152bcf7b2a97f96326eb
Deleted: sha256:57d29ad88aa49f0f439592755722e70710501b366e2be6125c95accc43464844
Deleted: sha256:b17c024283d0302615c6f0c825137da9db607d49a83d2215a79733afbbaeb7c3
# docker rmi -f IMAGE ID IMAGE ID IMAGE ID IMAGE ID #空格分割删除多个镜像
# docker rmi -f $(docker images -aq) #递归删除所有镜像
# 这里有比较重要的镜像 就不演示了
三、容器命令
说明:我们有了镜像才能创建容器,在docker中在装一个centos镜像来测试学习
2.3.1 docker run 启动容器
# 1. 下载一个最新版本的centos镜像
docker pull contos
# 2. 启动contos镜像
docker run [可选参数] image
# 2.1 介绍可选参数
--name="name" #容器的别称 例如启动多个tomcat时 tomcat01 tomcat02 来区分容器
-d #后台方式运行
-it #使用交互方式运行,进入容器查看内容
-p(小写) #指定容器的端口 -p 8080:8080
-p 172.0.0.1:8080:8080 #ip:主机端口:容器端口
-p 8080:8080 #主机端口:容器端口(常用)
-p 8080 #容器端口
-P(大写) #随机指定端口
# 3. 启动centos镜像
[root@iZhp3do4qhzu84445osa3sZ ~]# docker run -it docker.io/centos /bin/bash #启动并进入容器 ,以交互的方式,不然进不去
[root@9612d2aecd2f /]# # 已经进入centos镜像的操作界面
# 4. 清楚的发现root@主机名称发生了改变
# 5. 查看centos镜像内部文件-本质上就是一个centos系统,只不过是基础版本,很多的命令不完善
[root@9612d2aecd2f /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var #内部的centos和外部centos没有一点关系
# 6. 退出当前centos镜像
[root@9612d2aecd2f /]# exit
exit
[root@iZhp3do4qhzu84445osa3sZ ~]# # 清楚的发现root@主机名又一次发生了改变
# 7. 判断当前环境是centos镜像还是我们的宿主机centos系统
[root@iZhp3do4qhzu84445osa3sZ /]# ls
bin dev home lib lost+found mnt opt proc run srv temp Users var
boot etc install.sh lib64 media nexus-data patch root sbin sys tmp usr www
[root@iZhp3do4qhzu84445osa3sZ /]# # 可以发现现在的fileTree比上次查看的多很多,可以确定当前的环境为我们宿主机的centos系统
2.3.2 docker ps 查看运行的容器
# 1. docker ps --help 帮助手册
[root@iZhp3do4qhzu84445osa3sZ /]# docker ps --help
Usage: docker ps [OPTIONS]
List containers
Options:
-a, --all Show all containers (default shows just running)
-f, --filter filter Filter output based on conditions provided
--format string Pretty-print containers using a Go template
--help Print usage
-n, --last int Show n last created containers (includes all states) (default -1)
-l, --latest Show the latest created container (includes all states)
--no-trunc Don't truncate output
-q, --quiet Only display numeric IDs
-s, --size Display total file sizes
# 2. docker ps 查看当前正在运行的镜像
[root@iZhp3do4qhzu84445osa3sZ /]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3466fcdf89fb sonatype/nexus3 "sh -c ${SONATYPE_..." 7 weeks ago Up About an hour 0.0.0.0:8081->8081/tcp nexus
# 3. docker ps -a 查看历史运行的镜像
[root@iZhp3do4qhzu84445osa3sZ /]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9612d2aecd2f docker.io/centos "/bin/bash" 11 minutes ago Exited (0) 6 minutes ago friendly_jones
f8728bfa5820 docker.io/centos "/bin/bash" 12 minutes ago Exited (0) 12 minutes ago objective_ardinghelli
223c74a730f5 docker.io/centos "/bin/bash" 12 minutes ago Exited (0) 12 minutes ago amazing_keller
60c8aa9d8801 hello-world "/hello" 2 hours ago Exited (0) 2 hours ago goofy_meninsky
269c071b447b mysql:5.7.25 "docker-entrypoint..." 3 weeks ago Created 33060/tcp, 0.0.0.0:3308->3306/tcp mysql
3466fcdf89fb sonatype/nexus3 "sh -c ${SONATYPE_..." 7 weeks ago Up About an hour 0.0.0.0:8081->8081/tcp nexus
# 4. docker ps -a -n=1 查看历史运行的镜像并且只显示一个 默认显示的是最近运行过的镜像
[root@iZhp3do4qhzu84445osa3sZ /]# docker ps -a -n=1
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9612d2aecd2f docker.io/centos "/bin/bash" 13 minutes ago Exited (0) 8 minutes ago friendly_jones
# 4. docker ps -aq 查看历史运行的镜像 并且只显示编号
[root@iZhp3do4qhzu84445osa3sZ /]# docker ps -aq
9612d2aecd2f
f8728bfa5820
223c74a730f5
60c8aa9d8801
269c071b447b
3466fcdf89fb
2.3.3 退出容器
exit # 直接容器停止并退出
Ctrl + P + Q # 容器不停止并且退出
2.3.4 docker rm 删除容器
docker rm 容器ID #根据容器的id进行删除 不能删除正在运行的容器,如果想强制删除 -f
docker rm -f $(docker ps -aq) #递归删除所有容器
2.3.5 启动和停止容器
docker start [容器ID] # 根据容器ID 启动容器
docker restart [容器ID] # 根据容器ID 重启容器
docker stop [容器ID] # 根据容器ID 停止当前运行的容器
docker kill[容器ID] # 根据容器ID 强制停止当前运行的容器进程
四、常用的其他命令
2.4.1 后台启动容器
# 命令 docker run -d [镜像名称]
# 问题:docker ps 发现centos 停止了
# 常见的坑: docker 容器使用后台运行,就必须要有一个前台进程,docker 发现没有应用,就会停止
# nginx:容器启动后,发现自己没有提供服务,就会自动停止,就是没有程序了
2.4.2 查看日志
docker logs -tf --tail [容器ID] # 如果没有日志
# 自己编写一段shell脚本
docker run -d centos /bin/sh -c "while true;do echo hzbs;slepp 1;done"
# 显示日志
-tf
--tail [NUMBER] #要显示的日志的行数
2.4.3 docker top [容器ID]查看容器中的进程信息
[root@iZhp3do4qhzu84445osa3sZ ~]# docker top 3466fcdf89fb
UID PID PPID C STIME TTY TIME CMD
200 6293 6275 0 Jun20 ? 00:01:51 /usr/lib/jvm/java-1.8.0/bin/java -server -Dinstall4j.jvmDir=/usr/lib/jvm/java-1.8.0 -Dexe4j.moduleName=/opt/sonatype/nexus/bin/nexus -XX:+UnlockDiagnosticVMOptions -Dinstall4j.launcherId=245 -Dinstall4j.swt=false -Di4jv=0 -Di4jv=0 -Di4jv=0 -Di4jv=0 -Di4jv=0 -Xms1200m -Xmx1200m -XX:MaxDirectMemorySize=2g -Djava.util.prefs.userRoot=/nexus-data/javaprefs -XX:+UnlockDiagnosticVMOptions -XX:+LogVMOutput -XX:LogFile=../sonatype-work/nexus3/log/jvm.log -XX:-OmitStackTraceInFastThrow -Djava.net.preferIPv4Stack=true -Dkaraf.home=. -Dkaraf.base=. -Dkaraf.etc=etc/karaf -Djava.util.logging.config.file=etc/karaf/java.util.logging.properties -Dkaraf.data=../sonatype-work/nexus3 -Dkaraf.log=../sonatype-work/nexus3/log -Djava.io.tmpdir=../sonatype-work/nexus3/tmp -Dkaraf.startLocalConsole=false -Djava.endorsed.dirs=lib/endorsed -Di4j.vpt=true -classpath /opt/sonatype/nexus/.install4j/i4jruntime.jar:/opt/sonatype/nexus/lib/boot/nexus-main.jar:/opt/sonatype/nexus/lib/boot/activation-1.1.1.jar:/opt/sonatype/nexus/lib/boot/jaxb-api-2.2.7.jar:/opt/sonatype/nexus/lib/boot/jaxb-core-2.2.7.jar:/opt/sonatype/nexus/lib/boot/jaxb-impl-2.2.7.jar:/opt/sonatype/nexus/lib/boot/org.apache.karaf.main-4.2.6.jar:/opt/sonatype/nexus/lib/boot/org.osgi.core-6.0.0.jar:/opt/sonatype/nexus/lib/boot/org.apache.karaf.specs.activator-4.2.6.jar:/opt/sonatype/nexus/lib/boot/org.apache.karaf.diagnostic.boot-4.2.6.jar:/opt/sonatype/nexus/lib/boot/org.apache.karaf.jaas.boot-4.2.6.jar com.install4j.runtime.launcher.UnixLauncher run 9d17dc87 0 0 org.sonatype.nexus.karaf.NexusMain
2.4.4 docker inspect [容器的ID]查看容器内部的源数据
[root@iZhp3do4qhzu84445osa3sZ ~]# docker inspect 3466fcdf89fb #这里查看的是ne