docker中的容器常用命令
1、使用镜像创建容器并启动 docker run 镜像id
[root@jmeter_1 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE tomcat 8 926c7fd4777e 4 days ago 533MB [root@jmeter_1 ~]# docker run 926c7fd4777e 创建Tomcat的容器并启动Tomcat WARNING: IPv4 forwarding is disabled. Networking will not work. 15-Apr-2021 15:46:00.520 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version name: Apache Tomcat/8.5.65 15-Apr-2021 15:46:00.521 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server built: Mar 30 2021 12:28:40 UTC 15-Apr-2021 15:46:00.522 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version number: 8.5.65.0 15-Apr-2021 15:46:00.522 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Name: Linux 15-Apr-2021 15:46:00.522 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Version: 3.10.0-1062.el7.x86_64 15-Apr-2021 15:46:00.522 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Architecture: amd64 15-Apr-2021 15:46:00.522 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Java Home: /usr/local/openjdk-8/jre 15-Apr-2021 15:46:00.522 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Version: 1.8.0_282-b08 15-Apr-2021 15:46:00.522 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Vendor: Oracle Corporation 15-Apr-2021 15:46:00.522 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_BASE: /usr/local/tomcat 15-Apr-2021 15:46:00.522 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_HOME: /usr/local/tomcat 15-Apr-2021 15:46:00.522 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.config.file=/usr/local/tomcat/conf/logging.properties 15-Apr-2021 15:46:00.522 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager 15-Apr-2021 15:46:00.522 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djdk.tls.ephemeralDHKeySize=2048 15-Apr-2021 15:46:00.522 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.protocol.handler.pkgs=org.apache.catalina.webresources 15-Apr-2021 15:46:00.523 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dorg.apache.catalina.security.SecurityListener.UMASK=0027 15-Apr-2021 15:46:00.523 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dignore.endorsed.dirs= 15-Apr-2021 15:46:00.523 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.base=/usr/local/tomcat 15-Apr-2021 15:46:00.523 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.home=/usr/local/tomcat 15-Apr-2021 15:46:00.523 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.io.tmpdir=/usr/local/tomcat/temp 15-Apr-2021 15:46:00.523 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent Loaded Apache Tomcat Native library [1.2.27] using APR version [1.6.5]. 15-Apr-2021 15:46:00.523 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true]. 15-Apr-2021 15:46:00.523 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent APR/OpenSSL configuration: useAprConnector [false], useOpenSSL [true] 15-Apr-2021 15:46:00.531 INFO [main] org.apache.catalina.core.AprLifecycleListener.initializeSSL OpenSSL successfully initialized [OpenSSL 1.1.1d 10 Sep 2019] 15-Apr-2021 15:46:00.611 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["http-nio-8080"] 15-Apr-2021 15:46:00.625 INFO [main] org.apache.tomcat.util.net.NioSelectorPool.getSharedSelector Using a shared selector for servlet write/read 15-Apr-2021 15:46:00.651 INFO [main] org.apache.catalina.startup.Catalina.load Initialization processed in 503 ms 15-Apr-2021 15:46:00.676 INFO [main] org.apache.catalina.core.StandardService.startInternal Starting service [Catalina] 15-Apr-2021 15:46:00.677 INFO [main] org.apache.catalina.core.StandardEngine.startInternal Starting Servlet engine: [Apache Tomcat/8.5.65] 15-Apr-2021 15:46:00.692 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-nio-8080"] 15-Apr-2021 15:46:00.713 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in 62 ms
2、启动并进入容器的交互式环境中 docker run -it 镜像名 /bin/bash
[root@jmeter_1 ~]# docker run -it tomcat:8 /bin/bash # 启动并进入容器的交互式环境中 WARNING: IPv4 forwarding is disabled. Networking will not work. root@b3d4dadd3cbd:/usr/local/tomcat# ls BUILDING.txt LICENSE README.md RUNNING.txt conf logs temp webapps.dist CONTRIBUTING.md NOTICE RELEASE-NOTES bin lib native-jni-lib webapps work
root@b3d4dadd3cbd:/usr/local/tomcat# exit # 退出容器
exit
3、后台启动容器并给容器取别名 docker run --name=别名 -d 容器名 -d则是后台运行的参数,--name则是取别名的参数
[root@jmeter_1 ~]# docker run --name="tomcat1" -d tomcat:8 WARNING: IPv4 forwarding is disabled. Networking will not work. 33c2c00738f9f404aaa79292a1bdf568d664f0bfccc4ae4a42a0907937efbc6f [root@jmeter_1 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 33c2c00738f9 tomcat:8 "catalina.sh run" 10 seconds ago Up 9 seconds 8080/tcp tomcat1
4、进入后台运行的容器 docker exec -it 容器名 /bin/bash
[root@jmeter_1 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a119c4fcce10 tomcat:8 "catalina.sh run" 8 minutes ago Up 8 minutes 8080/tcp sweet_lederberg [root@jmeter_1 ~]# docker exec -it sweet_lederberg /bin/bash
这个命令有一个好处:使用exit命令退出容器后,并不会关闭这个容器
root@a119c4fcce10:/usr/local/tomcat# exit exit [root@jmeter_1 ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a119c4fcce10 tomcat:8 "catalina.sh run" 9 minutes ago Up 9 minutes 8080/tcp sweet_lederberg
可以看出容器:sweet_lederberg 还是在运行
docker attach 容器ID----这个命令也可以进入正在运行的容器,不过这是进入容器正在执行的终端,不会启动新的进程
5、退出容器
exit # 退出容器并停止
Ctrl + p +q # 不停止退出容器
6、查看容器 docker ps 【参数】,常见的参数有-a,-n,-q
其中
-a:列出历史运行过的容器
-n=?:列出最近创建的容器
-q:只显示容器的编号
[root@jmeter_1 ~]# docker ps 列出当前正在运行的容器
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
33c2c00738f9 tomcat:8 "catalina.sh run" 11 minutes ago Up 11 minutes 8080/tcp tomcat1
[root@jmeter_1 ~]# docker ps -a 列出曾经运行过的容器
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
33c2c00738f9 tomcat:8 "catalina.sh run" 12 minutes ago Up 12 minutes 8080/tcp tomcat1
c5281020036d tomcat:8 "/bin/bash" 9 days ago Exited (129) 9 days ago interesting_newton
b3d4dadd3cbd tomcat:8 "/bin/bash" 9 days ago Exited (0) 9 days ago friendly_elgamal
3cae14a36338 tomcat:8 "catalina.sh run" 9 days ago Exited (130) 9 days ago modest_stonebraker
86f73279f069 tomcat:8 "catalina.sh run" 9 days ago Exited (130) 9 days ago stupefied_ride
d6d6bdc6ef97 926c7fd4777e "catalina.sh run" 9 days ago Exited (130) 9 days ago affectionate_curran
61cc942354b6 d1165f221234 "/hello" 11 days ago Exited (0) 11 days ago ecstatic_wescoff
[root@jmeter_1 ~]# docker ps -n=1 列出最近创建的一个容器
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
33c2c00738f9 tomcat:8 "catalina.sh run" 12 minutes ago Up 12 minutes 8080/tcp tomcat1
[root@jmeter_1 ~]# docker ps -q 列出正在运行的容器的id
33c2c00738f9
7、删除容器 docker rm 容器id
[root@jmeter_1 ~]# docker rm 61cc942354b6 删除容器,但是不能删除正在运行的容器,删除正在运行的只能强制删除,加上-f参数 61cc942354b6 [root@jmeter_1 ~]# docker rm -f $(docker ps -aq) # 删除所有的容器 11e69a257368 f6c9b3a2ba04 d23dbf03b83d 87da33fcd2dc a119c4fcce10 33c2c00738f9 c5281020036d b3d4dadd3cbd 3cae14a36338 86f73279f069 d6d6bdc6ef97
8、启动和停止容器
[root@jmeter_1 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a474e8380020 tomcat:8 "/bin/bash" 2 minutes ago Exited (137) 8 seconds ago jolly_saha [root@jmeter_1 ~]# docker start a474e8380020 # 启动容器 a474e8380020 [root@jmeter_1 ~]# docker stop a474e8380020 # 停止容器 a474e8380020 [root@jmeter_1 ~]# docker restart a474e8380020 # 重启容器 a474e8380020 [root@jmeter_1 ~]# docker kill a474e8380020 # 强制停止容器 a474e8380020
9、查看容器的日志 docker logs -f -t --dince="时间" --tail="n" 镜像id
[root@jmeter_1 ~]# docker logs -t -f --since="2021-04-25" --tail="5" 5f04d3b93ce7 查看指定日期的最新的5行日志 2021-04-25T15:34:58.104225347Z 25-Apr-2021 15:34:58.103 INFO [main] org.apache.catalina.startup.Catalina.load Initialization processed in 651 ms 2021-04-25T15:34:58.132534968Z 25-Apr-2021 15:34:58.132 INFO [main] org.apache.catalina.core.StandardService.startInternal Starting service [Catalina] 2021-04-25T15:34:58.132872741Z 25-Apr-2021 15:34:58.132 INFO [main] org.apache.catalina.core.StandardEngine.startInternal Starting Servlet engine: [Apache Tomcat/8.5.65] 2021-04-25T15:34:58.146628743Z 25-Apr-2021 15:34:58.145 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-nio-8080"] 2021-04-25T15:34:58.162752740Z 25-Apr-2021 15:34:58.160 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in 56 ms
10、查看容器的进程信息 docker top 容器id
[root@jmeter_1 ~]# docker top 5f04d3b93ce7 UID PID PPID C STIME TTY TIME CMD root 6180 6160 66 23:53 ? 00:00:02 /usr/local/openjdk-8/bin/java -Djava.util.logging.config.file=/usr/local/tomcat/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djdk.tls.ephemeralDHKeySize=2048 -Djava.protocol.handler.pkgs=org.apache.catalina.webresources -Dorg.apache.catalina.security.SecurityListener.UMASK=0027 -Dignore.endorsed.dirs= -classpath /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar -Dcatalina.base=/usr/local/tomcat -Dcatalina.home=/usr/local/tomcat -Djava.io.tmpdir=/usr/local/tomcat/temp org.apache.catalina.startup.Bootstrap start
11、查看容器的元数据 docker inspect 容器id
[root@jmeter_1 ~]# docker inspect 5f04d3b93ce7 [ { "Id": "5f04d3b93ce7f9f32e8b64cd13c9f95fe18afec51427db694810869fc02d8af9", "Created": "2021-04-25T15:34:56.197461937Z", "Path": "catalina.sh", "Args": [ "run" ], "State": { "Status": "running", "Running": true, "Paused": false, "Restarting": false, "OOMKilled": false, "Dead": false, "Pid": 6180, "ExitCode": 0, "Error": "", "StartedAt": "2021-04-25T15:54:00.079983258Z", "FinishedAt": "2021-04-25T15:50:27.285726685Z" }, "Image": "sha256:926c7fd4777eb4742eceddf0fcc9f4cd33a124268735f559c955cafce935fb02", "ResolvConfPath": "/var/lib/docker/containers/5f04d3b93ce7f9f32e8b64cd13c9f95fe18afec51427db694810869fc02d8af9/resolv.conf", "HostnamePath": "/var/lib/docker/containers/5f04d3b93ce7f9f32e8b64cd13c9f95fe18afec51427db694810869fc02d8af9/hostname", "HostsPath": "/var/lib/docker/containers/5f04d3b93ce7f9f32e8b64cd13c9f95fe18afec51427db694810869fc02d8af9/hosts", "LogPath": "/var/lib/docker/containers/5f04d3b93ce7f9f32e8b64cd13c9f95fe18afec51427db694810869fc02d8af9/5f04d3b93ce7f9f32e8b64cd13c9f95fe18afec51427db694810869fc02d8af9-json.log", "Name": "/pensive_pare", "RestartCount": 0, "Driver": "overlay2", "Platform": "linux", "MountLabel": "", "ProcessLabel": "", "AppArmorProfile": "", "ExecIDs": null, "HostConfig": { "Binds": null, "ContainerIDFile": "", "LogConfig": { "Type": "json-file", "Config": {} }, "NetworkMode": "default", "PortBindings": {}, "RestartPolicy": { "Name": "no", "MaximumRetryCount": 0 }, "AutoRemove": false, "VolumeDriver": "", "VolumesFrom": null, "CapAdd": null, "CapDrop": null, "CgroupnsMode": "host", "Dns": [], "DnsOptions": [], "DnsSearch": [], "ExtraHosts": null, "GroupAdd": null, "IpcMode": "private", "Cgroup": "", "Links": null, "OomScoreAdj": 0, "PidMode": "", "Privileged": false, "PublishAllPorts": false, "ReadonlyRootfs": false, "SecurityOpt": null, "UTSMode": "", "UsernsMode": "", "ShmSize": 67108864, "Runtime": "runc", "ConsoleSize": [ 0, 0 ], "Isolation": "", "CpuShares": 0, "Memory": 0, "NanoCpus": 0, "CgroupParent": "", "BlkioWeight": 0, "BlkioWeightDevice": [], "BlkioDeviceReadBps": null, "BlkioDeviceWriteBps": null, "BlkioDeviceReadIOps": null, "BlkioDeviceWriteIOps": null, "CpuPeriod": 0, "CpuQuota": 0, "CpuRealtimePeriod": 0, "CpuRealtimeRuntime": 0, "CpusetCpus": "", "CpusetMems": "", "Devices": [], "DeviceCgroupRules": null, "DeviceRequests": null, "KernelMemory": 0, "KernelMemoryTCP": 0, "MemoryReservation": 0, "MemorySwap": 0, "MemorySwappiness": null, "OomKillDisable": false, "PidsLimit": null, "Ulimits": null, "CpuCount": 0, "CpuPercent": 0, "IOMaximumIOps": 0, "IOMaximumBandwidth": 0, "MaskedPaths": [ "/proc/asound", "/proc/acpi", "/proc/kcore", "/proc/keys", "/proc/latency_stats", "/proc/timer_list", "/proc/timer_stats", "/proc/sched_debug", "/proc/scsi", "/sys/firmware" ], "ReadonlyPaths": [ "/proc/bus", "/proc/fs", "/proc/irq", "/proc/sys", "/proc/sysrq-trigger" ] }, "GraphDriver": { "Data": { "LowerDir": "/var/lib/docker/overlay2/9bd70dc8ee12a698c041a5576c6a5628402ceea20518ffe3b4a5070b199d4beb-init/diff:/var/lib/docker/overlay2/e3fa396691f6b12dd26e390abf87c6fb205901a21ca53655524347a5bcedb594/diff:/var/lib/docker/overlay2/1aec3ba8be7d73c5ade57e26c60606294af147a26b08220bf629a87c9d690353/diff:/var/lib/docker/overlay2/d1cb1ebfb41a22e3df594153280364c327820549bb0ee96a00db7a8c9617fd65/diff:/var/lib/docker/overlay2/633743e55886476663781460b01f26514860bc42f46ccbf0fe373cab8fd948e5/diff:/var/lib/docker/overlay2/ca2dcc7ca30a86261955c80231558c3ac9b7b27fbd481e4891f4c4d429a91a71/diff:/var/lib/docker/overlay2/b3af5a206bd775d5ad239cba6e16f7aaffdeed3fa5985af2479cb82dafca363b/diff:/var/lib/docker/overlay2/5aecba16d7d06f210bb1ed402b143039f07b75541380e32892d58697a084ff41/diff:/var/lib/docker/overlay2/1954b908d242344496cc2c13c82218e1b7654bbe13fc5dcff8e66c217434c14e/diff:/var/lib/docker/overlay2/b952b9cc8bdf70fda7ab0a480fe3d7a5377d04c95e9a644457d52450a83193f4/diff:/var/lib/docker/overlay2/46fbaec8ce65ae62dfee8294822eb046fdb1a1297df164bfed61c71c37323013/diff", "MergedDir": "/var/lib/docker/overlay2/9bd70dc8ee12a698c041a5576c6a5628402ceea20518ffe3b4a5070b199d4beb/merged", "UpperDir": "/var/lib/docker/overlay2/9bd70dc8ee12a698c041a5576c6a5628402ceea20518ffe3b4a5070b199d4beb/diff", "WorkDir": "/var/lib/docker/overlay2/9bd70dc8ee12a698c041a5576c6a5628402ceea20518ffe3b4a5070b199d4beb/work" }, "Name": "overlay2" }, "Mounts": [], "Config": { "Hostname": "5f04d3b93ce7", "Domainname": "", "User": "", "AttachStdin": false, "AttachStdout": false, "AttachStderr": false, "ExposedPorts": { "8080/tcp": {} }, "Tty": false, "OpenStdin": false, "StdinOnce": false, "Env": [ "PATH=/usr/local/tomcat/bin:/usr/local/openjdk-8/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", "JAVA_HOME=/usr/local/openjdk-8", "LANG=C.UTF-8", "JAVA_VERSION=8u282", "CATALINA_HOME=/usr/local/tomcat", "TOMCAT_NATIVE_LIBDIR=/usr/local/tomcat/native-jni-lib", "LD_LIBRARY_PATH=/usr/local/tomcat/native-jni-lib", "GPG_KEYS=05AB33110949707C93A279E3D3EFE6B686867BA6 07E48665A34DCAFAE522E5E6266191C37C037D42 47309207D818FFD8DCD3F83F1931D684307A10A5 541FBE7D8F78B25E055DDEE13C370389288584E7 61B832AC2F1C5A90F0F9B00A1C506407564C17A3 713DA88BE50911535FE716F5208B0AB1D63011C7 79F7026C690BAA50B92CD8B66A3AD3F4F22C4FED 9BA44C2621385CB966EBA586F72C284D731FABEE A27677289986DB50844682F8ACB77FC2E86E29AC A9C5DF4D22E99998D9875A5110C01C5A2F6059E7 DCFD35E0BF8CA7344752DE8B6FB21E8933C60243 F3A04C595DB5B6A5F1ECA43E3B7BBB100D811BBE F7DA48BB64BCB84ECBA7EE6935CD23C10D498E23", "TOMCAT_MAJOR=8", "TOMCAT_VERSION=8.5.65", "TOMCAT_SHA512=eb5a77d75a46496f7de39c1cba5f4fc4991ec7da7717e7b37ad48b4ca2ea334aeabfd094f64977477b4b2352637b56e30e5d9acfcdf7ccd5f4269a824829dd39" ], "Cmd": [ "catalina.sh", "run" ], "Image": "tomcat:8", "Volumes": null, "WorkingDir": "/usr/local/tomcat", "Entrypoint": null, "OnBuild": null, "Labels": {} }, "NetworkSettings": { "Bridge": "", "SandboxID": "f18fd3afe058b70392ece66d5a9fe556e3fda0bfecb0ca399e58c4bba917b2ba", "HairpinMode": false, "LinkLocalIPv6Address": "", "LinkLocalIPv6PrefixLen": 0, "Ports": { "8080/tcp": null }, "SandboxKey": "/var/run/docker/netns/f18fd3afe058", "SecondaryIPAddresses": null, "SecondaryIPv6Addresses": null, "EndpointID": "c344809a20a1054b13c719f579809c517add673befbbcbb2afe0e1fa4cc4e21d", "Gateway": "172.17.0.1", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "IPAddress": "172.17.0.2", "IPPrefixLen": 16, "IPv6Gateway": "", "MacAddress": "02:42:ac:11:00:02", "Networks": { "bridge": { "IPAMConfig": null, "Links": null, "Aliases": null, "NetworkID": "be095993f48fd0c45aa6cb2a6b7c40267d07dda577649de97183f8a14aeed7de", "EndpointID": "c344809a20a1054b13c719f579809c517add673befbbcbb2afe0e1fa4cc4e21d", "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 } } } } ]
12、从容器内部拷贝到主机上 docker cp 容器ID:文件的路径 目标路径
root@6b8da9cc8cae:/usr/local/tomcat# touch aaa.txt root@6b8da9cc8cae:/usr/local/tomcat# ls BUILDING.txt CONTRIBUTING.md LICENSE NOTICE README.md RELEASE-NOTES RUNNING.txt aaa.txt bin conf lib logs native-jni-lib temp webapps webapps.dist work root@6b8da9cc8cae:/usr/local/tomcat# exit exit [root@localhost ~]# cd /home/ [root@localhost home]# ll 总用量 0 drwxr-xr-x. 2 root root 61 4月 21 11:16 apps drwxr-xr-x. 2 3434 3434 75 3月 23 15:28 mysqld_exporter drwxr-xr-x. 2 3434 3434 73 3月 19 14:09 node_exporter drwxr-xr-x. 5 root root 86 3月 24 15:31 server [root@localhost home]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 6b8da9cc8cae tomcat:8 "catalina.sh run" 12 minutes ago Up 12 minutes 8080/tcp boring_payne [root@localhost home]# docker cp 6b8da9cc8cae:/usr/local/tomcat/aaa.txt /home/ 将容器内的文件拷贝到主机上 [root@localhost home]# ls aaa.txt apps mysqld_exporter node_exporter server [root@localhost home]#