root@test:/home/test# ls -lh /var/lib/docker/containers/
总用量 104K
drwx------ 4 root root 4.0K 7月 2 15:45 04f0406c122933a41c93018134734be792fd5dd6ea12d83ba40d352612d6db67
drwx------ 4 root root 4.0K 7月 2 01:07 082d3423b86cb80e208125510b27bd04986a8241b004211822291a11a7604545
drwx------ 3 root root 4.0K 7月 2 16:14 21b804a0949004020aa9df0169df86053c79a763b626a4afcf6a8db4e2ea59cf
drwx------ 4 root root 4.0K 7月 2 16:15 2216733dba525101caa36ae5d48afe5cc2e829361b5822c092c5fe48fa666c64
……
drwx------ 4 root root 4.0K 7月 2 00:34 f54f825f18f50d75c7922b1957183f4931bcb537f4c009083bf761ab3d31487e
drwx------ 4 root root 4.0K 7月 2 01:01 f6282f380d199ea6ad0a721eaaa96435a9db2160fcc6a17f36dc4b0b1c404730
docker create [OPTIONS] IMAGE [COMMAND]
创建容器,但不运行
docker start/stop contianer
运行/停止容器
docker run [OPTIONS] IMAGE [COMMAND]
创建容器并运行命令
docker ps
查看当前处于运行状态的容器,同docker container ls
docker ps -a
查看已创建的容器
docker container prune
删除所有停止的容器
docker container rm id/name
或者docker rm ID/name
删除指定的容器
docker rm $(dcoker ps -q)
删除未运行的容器
docker container kill ID/name或者docker kill ID/name
结束指定ID或者名字的容器
docker inspect ID/name
查看容器的基本信息,包括创建的镜像/运行情况/存储情况/网络设置等信息
docker inspect -f ID/name
:指定查看容器某项信息(区分大小写,)
root@test:/home/test# docker inspect -f {{.State.status}} 221673
Template parsing error: template: :1:8: executing "" at <.State.status>: map has no entry for key "status"
root@test:/home/test# docker inspect -f {{.State.Status}} 221673
running
root@test:/home/test# docker inspect -f {{.LogPath}} 221673
/var/lib/docker/containers/2216733dba525101caa36ae5d48afe5cc2e829361b5822c092c5fe48fa666c64/2216733dba525101caa36ae5d48afe5cc2e829361b5822c092c5fe48fa666c64-json.log
docker logs id/name
查看容器的日志
docker stats id/name
查看容器所占用的系统资源
docker exec id/name
容器内执行命令
docker create imageID/iamgeName
创建容器
--name name
指定容器名字,可通过名字来进行容器相关操作
-p 主机端口:容器端口
端口映射
root@test:/home/test# docker create --name tomcat_test -p 8081:8080 tomcat:v10.0.7
root@test:/home/test# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f05e223417e8 tomcat:v10.0.7 "./bin/catalina.sh r…" 11 minutes ago Created tomcat_test
2216733dba52 tomcat:v10.0.7 "./bin/catalina.sh r…" 3 days ago Up 35 minutes 0.0.0.0:8080->8080/tcp goofy_minsky
-v hostdir:docker_dir
:绑定数据卷,hostdir不存在时会自动创建
--mount type=bind,src=主机目录,dst=访问容器中的路径
:绑定数据卷,src所指定的目录必须存在(服务器的目录),target时容器中的目录
eg: 创建容器,将本机的/home/test目录挂接到容器的/home目录下,/home/test目录和容器的/home目录共享
root@test:/home/test# docker run --name tomcat_mount --mount type=bind,src=/home/test,target=/home -d tomcat:v10.0.7
8e0f94d6015a37a87ae4695fa8eb1d04f26451a0234d8cf3b751001127f44218
root@zhujunhan-PC:/home/test# docker exec tomcat_mount ls /home
Dockerfile1
apache-tomcat-10.0.7
apache-tomcat-10.0.7.tar.gz
jdk-8u291-linux-x64.tar.gz
jdk1.8.0_291
root@zhujunhan-PC:/home/test# mkdir newdir
root@zhujunhan-PC:/home/test# docker exec tomcat_mount ls /home
Dockerfile1
apache-tomcat-10.0.7
apache-tomcat-10.0.7.tar.gz
jdk-8u291-linux-x64.tar.gz
jdk1.8.0_291
newdir
root@zhujunhan-PC:/home/test# docker exec tomcat_mount mkdir /home/dockerdir
root@zhujunhan-PC:/home/test# docker exec tomcat_mount ls /home
Dockerfile1
apache-tomcat-10.0.7
apache-tomcat-10.0.7.tar.gz
dockerdir
jdk-8u291-linux-x64.tar.gz
jdk1.8.0_291
newdir
root@zhujunhan-PC:/home/test# ls -l
总用量 153120
drwxrwxrwx 9 root root 4096 7月 1 23:49 apache-tomcat-10.0.7
-rwxrwxrwx 1 zhujunhan docker 11834793 7月 1 16:20 apache-tomcat-10.0.7.tar.gz
drwxr-xr-x 2 root root 4096 7月 5 18:29 dockerdir
-rwxrwxrwx 1 root root 296 7月 1 17:10 Dockerfile1
drwxr-xr-x 8 10143 10143 4096 4月 8 03:26 jdk1.8.0_291
-rwxrwxrwx 1 zhujunhan docker 144935989 7月 1 16:45 jdk-8u291-linux-x64.tar.gz
drwxr-xr-x 2 root root 4096 7月 5 18:28 newdir
--env='a=b'
设置环境变量
--link containerA:alias
建立和容器间A的关联
docker exec containerName ps aux
查看容器内运行的进程
docker exec -it containerName /bin/bash
进入容器内,可执行多条命令,通过exit命令退出
root@test:/home/test# docker exec 221673 ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.9 2.7 4508020 104844 ? Ssl 09:18 0:08 /usr/local/jdk1.8.0_291/bin/java -Djava.util.logging.config.file=/usr/local/apache-tomcat-10.0.7/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/apache-tomcat-10.0.7/bin/bootstrap.jar:/usr/local/apache-tomcat-10.0.7/bin/tomcat-juli.jar -Dcatalina.base=/usr/local/apache-tomcat-10.0.7 -Dcatalina.home=/usr/local/apache-tomcat-10.0.7 -Djava.io.tmpdir=/usr/local/apache-tomcat-10.0.7/temp org.apache.catalina.startup.Bootstrap start
root 76 0.0 0.0 44628 3368 ? Rs 09:32 0:00 ps aux
root@test:/home/test# docker exec it 221673 /bin/bash
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.8 2.7 4508020 104844 ? Ssl 09:18 0:09 /usr/local/jdk1.8.0_291/bin/java -Djava.util.logging.config.file=/usr/local/apache
root 94 0.4 0.0 12020 3192 pts/0 Ss 09:34 0:00 /bin/bash
root 107 0.0 0.0 44628 3392 pts/0 R+ 09:34 0:00 ps aux
[root@2216733dba52 apache-tomcat-10.0.7]# ls -l
total 152
-rw-r----- 1 root root 18973 Jun 8 16:31 BUILDING.txt
-rw-r----- 1 root root 6210 Jun 8 16:31 CONTRIBUTING.md
-rw-r----- 1 root root 60269 Jun 8 16:31 LICENSE
-rw-r----- 1 root root 2333 Jun 8 16:31 NOTICE
-rw-r----- 1 root root 3372 Jun 8 16:31 README.md
-rw-r----- 1 root root 6904 Jun 8 16:31 RELEASE-NOTES
-rw-r----- 1 root root 16517 Jun 8 16:31 RUNNING.txt
drwxr-x--- 2 root root 4096 Jun 8 16:31 bin
drwx------ 1 root root 4096 Jul 2 08:15 conf
drwxr-x--- 2 root root 4096 Jun 8 16:31 lib
drwxr-x--- 1 root root 4096 Jul 5 09:18 logs
drwxr-x--- 2 root root 4096 Jun 8 16:31 temp
drwxr-x--- 7 root root 4096 Jun 8 16:31 webapps
drwxr-x--- 1 root root 4096 Jul 2 08:15 work
[root@2216733dba52 apache-tomcat-10.0.7]# exit
exit