容器生命周期管理
1.run
创建一个新的容器并运行一个命令
语法格式
docker run [OPTIONS] IMAGE [COMMAND][ARG...]
备注:
其实跟docker create一样的语法格式,只不过可以运行容器甚至进入容器内部
COMMAND: 需要在容器内执行的命令
docker run执行流程
首先在指定镜像上创建一个可写的容器层
然后使用指定的命令(command)启动它
docker run 相当于执行力两个API /containers/create 、 ../start
options说明
OPtion | 作用 |
---|---|
-i | 以交互模式运行,通常与-t同时使用 |
-t | 启动容器后,为容器分配一个命令行,通常与-i同时使用 |
-v | 目录映射,容器目录挂载到宿主机目录,格式<host目录><容器目录> |
-d | 守护进程,后台运行该容器 |
-p | 指定端口映射,格式: 主机(宿主)端口:容器端口 |
-P | 随机端口映射,容器内部端口随机映射到主机的端口 |
-u | 以什么用户身份创建容器 |
--name "nginx-lb" | 容器名字 |
-m, --memory bytes | 设置容器使用内存最大值 |
-h, --hostname string | 指定容器的host name |
--dns 8.8.8.8 | 指定容器 dns 服务器 |
-e username="ritchie" | 设置环境变量 |
--restart | Docker 重启后,容器是否自动重启 |
--privileged | 容器内是否使用真正的 root 权限 |
-p的三种写法
docker run -p ip:主机端口:容器端口
docker run -p 主机端口:容器端口(常用)
docker run -p 容器端口
实例1:
使用镜像 tomcat:7 容器命名为tomcat7
[root@localhost Fancy_Illusion]# docker run --name tomcat7 tomcat:7
Unable to find image 'tomcat:7' locally
7: Pulling from library/tomcat
0bc3020d05f1: Pull complete
a110e5871660: Pull complete
83d3c0fa203a: Pull complete
a8fd09c11b02: Pull complete
96ebf1506065: Pull complete
26b72ffca293: Pull complete
0bffa2ea17aa: Pull complete
d880cebcc7a6: Pull complete
d19ab8039b36: Pull complete
5057492dc495: Pull complete
Digest: sha256:f7d37d248d0eacec1e5995736234c9c22155626fcb27ea3ead13b9db24e698f7
Status: Downloaded newer image for tomcat:7
Sep 13, 2022 6:58:49 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server version name: Apache Tomcat/7.0.109
Sep 13, 2022 6:58:49 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server built: Apr 22 2021 18:43:38 UTC
Sep 13, 2022 6:58:49 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Server version number: 7.0.109.0
Sep 13, 2022 6:58:49 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Name: Linux
Sep 13, 2022 6:58:49 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: OS Version: 3.10.0-1160.el7.x86_64
Sep 13, 2022 6:58:49 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Architecture: amd64
Sep 13, 2022 6:58:49 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Java Home: /usr/local/openjdk-8/jre
Sep 13, 2022 6:58:49 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Version: 1.8.0_292-b10
Sep 13, 2022 6:58:49 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: JVM Vendor: Oracle Corporation
Sep 13, 2022 6:58:49 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_BASE: /usr/local/tomcat
Sep 13, 2022 6:58:49 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: CATALINA_HOME: /usr/local/tomcat
Sep 13, 2022 6:58:49 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Djava.util.logging.config.file=/usr/local/tomcat/conf/logging.properties
Sep 13, 2022 6:58:49 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
Sep 13, 2022 6:58:49 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Djdk.tls.ephemeralDHKeySize=2048
Sep 13, 2022 6:58:49 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dorg.apache.catalina.security.SecurityListener.UMASK=0027
Sep 13, 2022 6:58:49 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dignore.endorsed.dirs=
Sep 13, 2022 6:58:49 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.base=/usr/local/tomcat
Sep 13, 2022 6:58:49 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Dcatalina.home=/usr/local/tomcat
Sep 13, 2022 6:58:49 AM org.apache.catalina.startup.VersionLoggerListener log
INFO: Command line argument: -Djava.io.tmpdir=/usr/local/tomcat/temp
Sep 13, 2022 6:58:49 AM org.apache.catalina.core.AprLifecycleListener lifecycleEvent
INFO: Loaded Apache Tomcat Native library [1.2.28] using APR version [1.6.5].
Sep 13, 2022 6:58:49 AM org.apache.catalina.core.AprLifecycleListener lifecycleEvent
INFO: APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true].
Sep 13, 2022 6:58:49 AM org.apache.catalina.core.AprLifecycleListener initializeSSL
INFO: OpenSSL successfully initialized [OpenSSL 1.1.1d 10 Sep 2019]
Sep 13, 2022 6:58:49 AM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-apr-8080"]
Sep 13, 2022 6:58:49 AM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 667 ms
Sep 13, 2022 6:58:49 AM org.apache.catalina.core.StandardService startInternal
INFO: Starting service [Catalina]
Sep 13, 2022 6:58:49 AM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.109
Sep 13, 2022 6:58:49 AM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-apr-8080"]
Sep 13, 2022 6:58:49 AM org.apache.catalina.startup.Catalina start
INFO: Server startup in 65 ms
这样运行,会直接将容器运行日志打印出来,所以一般都需要加 -d,否则无法继续敲其他命令
[root@localhost Fancy_Illusion]# docker run -d --name tomcat8 tomcat:8
Unable to find image 'tomcat:8' locally
8: Pulling from library/tomcat
0e29546d541c: Pull complete
9b829c73b52b: Pull complete
cb5b7ae36172: Pull complete
6494e4811622: Pull complete
668f6fcc5fa5: Pull complete
dc120c3e0290: Pull complete
8f7c0eebb7b1: Pull complete
77b694f83996: Pull complete
b7c26350ecc2: Pull complete
7365b3b02e1b: Pull complete
Digest: sha256:421c2a2c73f3e339c787beaacde0f7bbc30bba957ec653d41a77d08144c6a028
Status: Downloaded newer image for tomcat:8
dadbab601c2d02a6cd04abf186675ff9c85fc3f8b0989b0391dbd2786dfb367e
[root@localhost Fancy_Illusion]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
dadbab601c2d tomcat:8 "catalina.sh run" About a minute ago Up About a minute 8080/tcp tomcat8
其实打印的在日志就是 catalina.sh run 命令之后的日志
实例2:
使用镜像tomcat:8
以后台模式启动一个容器
容器名字定义为tomcat8
[root@localhost Fancy_Illusion]# docker run -d --name tomcat8 tomcat:8
[root@localhost Fancy_Illusion]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
dadbab601c2d tomcat:8 "catalina.sh run" About a minute ago Up About a minute 8080/tcp tomcat8
创建容器之后还能执行其他命令,这样方便很多
2.start/stop/restart
docker start:启动一个或多个已经被停止的容器
docker stop:停止一个或多个运行中的容器
docker restart:重启一个或多个容器
语法格式
docker start [OPTIONS] CONTAINER [CONTAINER...]
docker stop [OPTIONS] CONTAINER [CONTAINER...]
docker restart [OPTIONS] CONTAINER [CONTAINER...]
stop/restart 命令的 options
option | 作用 |
---|---|
-t,--time | 杀死容器之前等待停止的秒数(默认为10) |
根据容器 ID 启动、关闭、重启
[root@localhost Fancy_Illusion]# docker start afb882148e5e
afb882148e5e
[root@localhost Fancy_Illusion]# docker stop afb882148e5e
afb882148e5e
[root@localhost Fancy_Illusion]# docker restart afb882148e5e
afb882148e5e
根据容器名字启动、关闭、重启
[root@localhost Fancy_Illusion]# docker start jenkins
jenkins
[root@localhost Fancy_Illusion]# docker stop jenkins
jenkins
[root@localhost Fancy_Illusion]# docker restart jenkins
jenkins
启动所有容器
[root@localhost Fancy_Illusion]# docker start $(docker ps -a -q)
关闭所有容器
[root@localhost Fancy_Illusion]# docker stop $(docker ps -a -q)