官方文档:https://docs.docker.com/engine/reference/run/
Docker 镜像命令 查看镜像 1 2 3 4 5 6 7 8 9 10 11 $ docker images REPOSITORY TAG IMAGE ID CREATED SIZE tomcat latest 040bdb29ab37 3 weeks ago 649MB hello-world latest bf756fb1ae65 13 months ago 13.3kB
搜索镜像 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 $ docker search tomcat NAME DESCRIPTION STARS OFFICIAL AUTOMATED tomcat Apache Tomcat is an open source implementati… 2934 [OK] tomee Apache TomEE is an all-Apache Java EE certif… 86 [OK] dordoka/tomcat Ubuntu 14.04, Oracle JDK 8 and Tomcat 8 base… 55 [OK] bitnami/tomcat Bitnami Tomcat Docker Image 37 [OK] kubeguide/tomcat-app Tomcat image for Chapter 1 29 consol/tomcat-7.0 Tomcat 7.0.57, 8080, "admin/admin" 18 [OK] cloudesire/tomcat Tomcat server, 6/7/8 15 [OK] aallam/tomcat-mysql Debian, Oracle JDK, Tomcat & MySQL 13 [OK] arm32v7/tomcat Apache Tomcat is an open source implementati… 10 rightctrl/tomcat CentOS , Oracle Java, tomcat application ssl… 6 [OK] unidata/tomcat-docker Security-hardened Tomcat Docker container. 5 [OK] arm64v8/tomcat Apache Tomcat is an open source implementati… 3 fabric8/tomcat-8 Fabric8 Tomcat 8 Image 2 [OK] amd64/tomcat Apache Tomcat is an open source implementati… 2 jelastic/tomcat An image of the Tomcat Java application serv… 2 cfje/tomcat-resource Tomcat Concourse Resource 2 chenyufeng/tomcat-centos tomcat基于centos6的镜像 1 [OK] picoded/tomcat7 tomcat7 with jre8 and MANAGER_USER / MANAGER… 1 [OK] camptocamp/tomcat-logback Docker image for tomcat with logback integra… 1 [OK] oobsri/tomcat8 Testing CI Jobs with different names. 1 ppc64le/tomcat Apache Tomcat is an open source implementati… 1 99taxis/tomcat7 Tomcat7 1 [OK] secoresearch/tomcat-varnish Tomcat and Varnish 5.0 0 [OK] s390x/tomcat Apache Tomcat is an open source implementati… 0 softwareplant/tomcat Tomcat images for jira-cloud testing 0 [OK]
拉取镜像 拉取镜像就是从中央仓库下载镜像到本地,假如我要拉取 centos 镜像到本地,如果不申明 tag 镜像标签信息则默认拉取 lastest 版本,也可以通过:https://hub.docker.com/搜索该镜像,查看支持的tag信息。
1 2 3 4 5 6 7 8 9 10 11 12 13 $ docker pull tomcat:10.0.2-jdk15-openjdk-oraclelinux7 10.0.2-jdk15-openjdk-oraclelinux7: Pulling from library/tomcat 69d77e45731b: Pull complete f5df3ce42f8a: Pull complete 134dabdfc154: Pull complete 0ccb10e0758f: Pull complete ef0d135b9267: Pull complete 4f1699714bab: Pull complete Digest: sha256:f4f3fa5591c90b244b652c13f52e6a4d926872b6a19765d093309a0cab4112f2 Status: Downloaded newer image for tomcat:10.0.2-jdk15-openjdk-oraclelinux7 docker.io/library/tomcat:10.0.2-jdk15-openjdk-oraclelinux7
删除镜像 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 $ docker images REPOSITORY TAG IMAGE ID CREATED SIZE tomcat 10.0.2-jdk15-openjdk-oraclelinux7 992794023f69 5 days ago 547MB tomcat latest 040bdb29ab37 3 weeks ago 649MB hello-world latest bf756fb1ae65 13 months ago 13.3kB $ docker rmi tomcat:10.0.2-jdk15-openjdk-oraclelinux7 or $ docker rmi 992794023f69 Untagged: tomcat:10.0.2-jdk15-openjdk-oraclelinux7 Untagged: tomcat@sha256:f4f3fa5591c90b244b652c13f52e6a4d926872b6a19765d093309a0cab4112f2 Deleted: sha256:992794023f69c089cfba9e4723ffa04db5b866fe82af5009edc6eabb2530f991 Deleted: sha256:91f5272fe951d440d2e787e7d056a4b2ecadc8abbee55d0f96a1602206cd5934 Deleted: sha256:626b650fca6e0ddda262c9c0925b928adeae0911c3279eeac96f2ed041d3f39b Deleted: sha256:c4efc30ac32c3eb213cbd6f1407dc9beea818b96ae937b1b86a733b4cc9066e8 Deleted: sha256:7b754813f2f0a3cc3be05fc100229ede3eea2fb0ad7348046c9bf2dae4e85d36 Deleted: sha256:d8b0e1c06af8257dd06c83759f0fda6f3681eb4b411ae94e91bc1218b3c632f0 Deleted: sha256:8a53a328e1e98e7885efc2e3feae692ea62ac9de7b8982401fda9fe584a66320
容器命令 查看容器 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 $ docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 014baa637786 hello-world "/hello" 5 hours ago Exited (0) 5 hours ago pedantic_chebyshev $ docker ps $ docker ps -f status=exited $ docker ps --help Usage: docker ps [OPTIONS] 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 -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 container IDs -s, --size Display total file sizes
创建与启动容器 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 docker run [OPTIONS] IMAGE [COMMAND] [ARG...] $ docker run --name mytomcat -p 8080:8080 tomcat $ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 30b69f378dc8 tomcat "catalina.sh run" 3 minutes ago Up 3 minutes 0.0.0.0:8080->8080/tcp mytomcat $ docker rm mytomcat or $ docker rm 30b69f378dc8 $ docker run -it --name mycentos centos:7 [root@94ad6b97cf1a /] root $ docker run -id --name mycentos centos:7 0e63b59c5aa977fc197aad30ee53499448dce8c78ba82ae1ac409f124c90ffeb $ docker exec -it mycentos /bin/bash [root@0e63b59c5aa9 /]
停止与启动容器 1 2 3 4 5 6 7 8 9 10 11 12 $ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 0e63b59c5aa9 centos:7 "/bin/bash" 5 minutes ago Up 5 minutes mycentos 30b69f378dc8 tomcat "catalina.sh run" 25 minutes ago Up 13 minutes 0.0.0.0:8080->8080/tcp mytomcat $ docker stop mycentos mycentos $ docker stop 30b69f378dc8 30b69f378dc8
文件拷贝 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 $ docker cp echo-ls mycentos:/etc/echo-ls $ docker exec -it mycentos /bin/bash [root@0e63b59c5aa9 /] anaconda-ks.cfg echo-ls docker cp mycentos:/docker-ls ~/docker-ls [root@localhost ~] anaconda-post.log bin dev ......
目录挂载 我们可以在创建容器的时候,将宿主机的目录与容器内的目录进行映射,这样我们就可以通过修改宿主机某个目录的文件从而去影响容器,而且这个操作是双向绑定的,也就是说容器内的操作也会影响到宿主机,实现备份功能。但是容器被删除的时候,宿主机的内容并不会被删除。如果多个容器挂载同一个目录,其中一个容器被删除,其他容器的内容也不会受到影响。 容器与宿主机之间的数据卷属于引用的关系,数据卷是从外界挂载到容器内部中的,所以可以脱离容器的生命周期而独立存在,正是由于数据卷的生命周期并不等同于容器的生命周期,在容器退出或者删除以后,数据卷仍然不会受到影响,数据卷的生命周期会一直持续到没有容器使用它为止。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 $ docker run -di -v /var/docker/:/etc/docker --name mycentos centos:7 fb9cd00cae0a8dce03f94d0802328ff12961984295ae55f24e15feaccaed60fa $ docker exec -it mycentos /bin/bash [root@fb9cd00cae0a /] DIR_COLORS docker issue openldap rc2.d subgid $ docker inspect mycentos | grep Mounts -A 5 "Mounts" : [ { "Type" : "bind" , "Source" : "/var/docker" , "Destination" : "/etc/docker" , $ docker run -di --name mycentos --volume-from mycentos_two centos:7
匿名挂载 匿名挂载只需要写容器目录即可,容器外对应的目录会在/var/lib/docker/volumes
中生成。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 $ docker run -id -v /etc/docker_test --name mycentos centos:7 48ea0a97a1997d8116064c7a2aa36f451186b4e927ae43791006ae6429c5bc1c $ cd /var/lib/docker/volumes/ $ ls backingFsBlockDev c44f7aff34c05ff57fa9a129649e3fd4fda7b773ed63b8ecc5ad7bca2131bed4 metadata.db $ cd c44f7aff34c05ff57fa9a129649e3fd4fda7b773ed63b8ecc5ad7bca2131bed4/ $ ls _data $ cd _data $ docker exec -it mycentos /bin/bash [root@48ea0a97a199 /] [root@48ea0a97a199 docker_test] [root@localhost _data] test.txt $ docker inspect mycentos | grep volume "Type" : "volume" , "Source" : "/var/lib/docker/volumes/c44f7aff34c05ff57fa9a129649e3fd4fda7b773ed63b8ecc5ad7bca2131bed4/_data" , $ cd /var/lib/docker/volumes/c44f7aff34c05ff57fa9a129649e3fd4fda7b773ed63b8ecc5ad7bca2131bed4/_data"
具名挂载 所谓的具名挂载就是 docker 容器数据的目录名字并不是随机的,可以通过手动指定。
1 2 3 4 5 6 $ docker run -id -v mycentos_docker:/etc/docker_test --name mycentos centos:7 e02e21d85570ff82b698f3ab73fc71179b6fa9ded167b5b4c1f0a83b88a39df3 $ docker inspect mycentos | grep volumes "Source" : "/var/lib/docker/volumes/mycentos_docker/_data" , $ ls /var/lib/docker/volumes/ backingFsBlockDev c44f7aff34c05ff57fa9a129649e3fd4fda7b773ed63b8ecc5ad7bca2131bed4 metadata.db mycentos_docker
挂载只读 1 2 3 4 5 6 7 8 $ docker run -id -v /etc/docker/:/readonly :ro --name mycentos centos:7 0dcd031349ad88a9b5231e310ac859695582752d3ee2944fde7b05c03460c7e1 $ docker exec -it mycentos /bin/bash [root@0dcd031349ad /] [root@0dcd031349ad readonly ] touch: cannot touch '1.txt' : Read-only file system
输出容器的 IP 地址 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 docker inspect mycentos | grep "Networks" -A 20 "Networks" : { "bridge" : { "IPAMConfig" : null, "Links" : null, "Aliases" : null, "NetworkID" : "ad28a256012d9287317da239f56d3183a2876df76631917c0ea713695b40d640" , "EndpointID" : "2d85becf3835dc1fd0b2af6c80dcdf4d0519d513fc3f62eb70356c1fa99763c9" , "Gateway" : "172.17.0.1" , "IPAddress" : "172.17.0.2" , "IPPrefixLen" : 16, "IPv6Gateway" : "" , "GlobalIPv6Address" : "" , "GlobalIPv6PrefixLen" : 0, "MacAddress" : "02:42:ac:11:00:02" , "DriverOpts" : null $ docker inspect mycentos --format="{{.NetworkSettings.IPAddress}}" mycentos 172.17.0.2