[Docker] 01 - Container
容器命令
-
生成 容器
--name mycentos0115 设置别名
jeff@ThinkPad-T490:docker_study$ sudo docker run -it 0d120b6ccaa8 [root@56fc42e8d432 /]# [root@56fc42e8d432 /]# ls bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
容器地址:56fc42e8d432
重启容器:docker restart <id>
以后台模式启动:docker run -d <name>
-
进入 容器
进入已开启的容器中。
jeff@ThinkPad-T490:docker_study$ sudo docker attach 73327b735156
[root@73327b735156 /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var
-
列出 容器
-a 所有的运行过的容器。
-n 3 最近三次。
$ sudo docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 56fc42e8d432 0d120b6ccaa8 "/bin/bash" 4 minutes ago Up 4 minutes stoic_payne
-
退出、删除 容器
exit 即可退出。或者:
ctrl + P + Q 容器不停,退出。
docker stop <id> 慢慢停止,正藏关闭流程。
docker kill <id> 强制停止
终止运行的容器文件,依然会占据硬盘空间,故彻底删除。
docker rm <id> 删除容器
docker rm -f $(docker ps -q) 批量删除
然后查看记录,发现“当前消失了”,使用-l查看上一个/最近运行的容器,可见到状态是:Exited。
jeff@ThinkPad-T490:docker_study$ sudo docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES jeff@ThinkPad-T490:docker_study$ sudo docker ps -l CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 56fc42e8d432 0d120b6ccaa8 "/bin/bash" 19 minutes ago Exited (0) 27 seconds ago stoic_payne
-
容器日志
docker logs -f -t --tail 3 <ID>
-
容器外命令操作
”Docker容器后台运行,就必须有一个前台进程.“
[直接操作]:在容器内的centos中直接使用linux命令操作。
[root@73327b735156 /]# ls /tmp -l total 8 -rwx------ 1 root root 671 Aug 9 21:40 ks-script-2n9owwnh -rwx------ 1 root root 1379 Aug 9 21:40 ks-script-xm1o5azb
[间接操作]:在容器外也可以注入命令间接操作。
$ sudo docker exec -t 73327b735156 ls -l /tmp total 8 -rwx------ 1 root root 671 Aug 9 21:40 ks-script-2n9owwnh -rwx------ 1 root root 1379 Aug 9 21:40 ks-script-xm1o5azb
-
拷贝文件
拷贝出容器内的文件,示范如下。
$ sudo docker cp 73327b735156:/tmp/yum.log /root
Docker Commands Diagram
From: Reference commands
UnionFS
一层一层的文件系统组成:UnionFS。
Ref: 020-docker镜像UnionFS、Docker镜像加载原理、分层的镜像与容器、结合docker命令理解镜像【写的不错,是一系列文章】
rootfs (root file system) ,在bootfs之上。包含的就是典型 Linux 系统中的 /dev, /proc, /bin, /etc 等标准目录和文件。
rootfs就是各种不同的操作系统发行版,比如Ubuntu,Centos等等。
提交容器副本
-
启动容器的 Tomcat 服务
tomcat启动,docker的8888端口对应其中tomcat的端口8080。
-P 随机分配端口。
$ sudo docker run -it -p 8888:8080 tomcat
Using CATALINA_BASE: /usr/local/tomcat Using CATALINA_HOME: /usr/local/tomcat Using CATALINA_TMPDIR: /usr/local/tomcat/temp Using JRE_HOME: /usr/local/openjdk-11 Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar NOTE: Picked up JDK_JAVA_OPTIONS: --add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.base/java.io=ALL-UNNAMED --add-opens=java.rmi/sun.rmi.transport=ALL-UNNAMED 26-Aug-2020 13:05:22.945 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version name: Apache Tomcat/9.0.37 26-Aug-2020 13:05:22.950 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server built: Jun 30 2020 20:09:49 UTC 26-Aug-2020 13:05:22.951 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version number: 9.0.37.0 26-Aug-2020 13:05:22.951 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Name: Linux 26-Aug-2020 13:05:22.951 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Version: 5.4.0-42-generic 26-Aug-2020 13:05:22.951 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Architecture: amd64 26-Aug-2020 13:05:22.951 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Java Home: /usr/local/openjdk-11 26-Aug-2020 13:05:22.951 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Version: 11.0.8+10 26-Aug-2020 13:05:22.952 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Vendor: Oracle Corporation 26-Aug-2020 13:05:22.952 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_BASE: /usr/local/tomcat 26-Aug-2020 13:05:22.952 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_HOME: /usr/local/tomcat 26-Aug-2020 13:05:22.965 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: --add-opens=java.base/java.lang=ALL-UNNAMED 26-Aug-2020 13:05:22.965 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: --add-opens=java.base/java.io=ALL-UNNAMED 26-Aug-2020 13:05:22.966 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: --add-opens=java.rmi/sun.rmi.transport=ALL-UNNAMED 26-Aug-2020 13:05:22.966 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.config.file=/usr/local/tomcat/conf/logging.properties 26-Aug-2020 13:05:22.966 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager 26-Aug-2020 13:05:22.966 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djdk.tls.ephemeralDHKeySize=2048 26-Aug-2020 13:05:22.966 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.protocol.handler.pkgs=org.apache.catalina.webresources 26-Aug-2020 13:05:22.967 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dorg.apache.catalina.security.SecurityListener.UMASK=0027 26-Aug-2020 13:05:22.967 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dignore.endorsed.dirs= 26-Aug-2020 13:05:22.967 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.base=/usr/local/tomcat 26-Aug-2020 13:05:22.967 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.home=/usr/local/tomcat 26-Aug-2020 13:05:22.967 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.io.tmpdir=/usr/local/tomcat/temp 26-Aug-2020 13:05:22.968 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent Loaded Apache Tomcat Native library [1.2.24] using APR version [1.6.5]. 26-Aug-2020 13:05:22.968 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true]. 26-Aug-2020 13:05:22.968 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent APR/OpenSSL configuration: useAprConnector [false], useOpenSSL [true] 26-Aug-2020 13:05:22.971 INFO [main] org.apache.catalina.core.AprLifecycleListener.initializeSSL OpenSSL successfully initialized [OpenSSL 1.1.1d 10 Sep 2019] 26-Aug-2020 13:05:23.469 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["http-nio-8080"] 26-Aug-2020 13:05:23.511 INFO [main] org.apache.catalina.startup.Catalina.load Server initialization in [799] milliseconds 26-Aug-2020 13:05:23.591 INFO [main] org.apache.catalina.core.StandardService.startInternal Starting service [Catalina] 26-Aug-2020 13:05:23.591 INFO [main] org.apache.catalina.core.StandardEngine.startInternal Starting Servlet engine: [Apache Tomcat/9.0.37] 26-Aug-2020 13:05:23.605 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-nio-8080"] 26-Aug-2020 13:05:23.625 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in [113] milliseconds
浏览器访问的是8888端口。
-
docker commit 案例演示
先运行起来,查看容器ID,再在其基础之上提交。
jeff@ThinkPad-T490:docker_study$ sudo docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES b364415177d7 tomcat "catalina.sh run" 3 seconds ago Up 3 seconds 0.0.0.0:8888->8080/tcp vigorous_germain jeff@ThinkPad-T490:docker_study$ sudo docker commit -a="zzyy" -m="tomcat without docs" b364415177d7 hao123/mytomcat:1.2 sha256:088002ba613a082088f0739deeaec792dd21307a7b43f944370d2ea7cc23d54d
再启动测试一下。
$ sudo docker images REPOSITORY TAG IMAGE ID CREATED SIZE hao123/mytomcat 1.2 088002ba613a 4 minutes ago 648MB centos latest 0d120b6ccaa8 2 weeks ago 215MB tomcat latest 2ae23eb477aa 2 weeks ago 647MB
$ sudo docker run -it -p 7788:8080 hao123/mytomcat:1.2
数据共享
一、拷贝文件
容器 --> 主机
$ sudo docker cp 73327b735156:/tmp/yum.log /root
二、文件夹挂载
-
容器 <--> 主机
文件夹共享了!
$ sudo docker run -it -v /mnt:/mnt centos
[root@050cf874a6a1 /]# ls bin etc lib lost+found mnt proc run srv tmp var dev home lib64 media opt root sbin sys usr
需要只读属性的话:
docker run -it -v /宿主机绝对路径目录:/容器内目录:ro 镜像名
-
查看挂载(共享)
jeff@ThinkPad-T490:mnt$ sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
050cf874a6a1 centos "/bin/bash" 18 minutes ago Up 18 minutes nervous_moser
jeff@ThinkPad-T490:mnt$ sudo docker inspect 050cf874a6a1
在log中能看到具体的挂载记录。
"HostConfig": { "Binds": [ "/mnt:/mnt" ],
End.