zoukankan      html  css  js  c++  java
  • Docker

    问题现象:

    创意一个 docker 镜像. 并且构建成功. 运行容器 ,并查看 容器时, 它显示空的列表. 

    [root@localhost learn2]# docker build -t tomcatenv:1.0 .
    Sending build context to Docker daemon    368MB
    Step 1/11 : FROM                centos:8.2.2004
     ---> 831691599b88
    Step 2/11 : MAINTAINER  firestar "firestar@sina.com"
     ---> Running in 73ecb1e53aa6
    Removing intermediate container 73ecb1e53aa6
     ---> 9d2f2e36afa9
    Step 3/11 : WORKDIR     /usr
     ---> Running in adfa300e780f
    Removing intermediate container adfa300e780f
     ---> 07682d3368c4
    Step 4/11 : RUN                 mkdir jre
     ---> Running in 2d8784737898
    Removing intermediate container 2d8784737898
     ---> 216606ed25c6
    Step 5/11 : RUN                 mkdir tomcat
     ---> Running in 6c20b2b99c88
    Removing intermediate container 6c20b2b99c88
     ---> 7965cc996421
    Step 6/11 : ADD         jre1.8 /usr/jre
     ---> 81018d3c2adf
    Step 7/11 : ADD         mytomcat/apache-tomcat-9.0.45 /usr/tomcat
     ---> 4a74901fbd7a
    Step 8/11 : ENV JAVA_HOME /usr/jre
     ---> Running in d411abda3d0c
    Removing intermediate container d411abda3d0c
     ---> 1db5345ff153
    Step 9/11 : ENV PATH /sbin:$JAVA_HOME/bin:$PATH
     ---> Running in abb48987dd6e
    Removing intermediate container abb48987dd6e
     ---> 9e6cba2eba4e
    Step 10/11 : EXPOSE 8080
     ---> Running in d7ca23ef2cd0
    Removing intermediate container d7ca23ef2cd0
     ---> 2e1bd64f9f65
    Step 11/11 : ENTRYPOINT ["/usr/tomcat/bin/catalina.sh","run"]
     ---> Running in 619a3244d526
    Removing intermediate container 619a3244d526
     ---> 5d333a043514
    Successfully built 5d333a043514
    Successfully tagged tomcatenv:1.0

    centos 启动一个容器添加了-d 参数,但是docker ps 或者docker ps -a查看却已经退出了

    [root@localhost learn2]# docker run -d --name mytomcat tomcatenv:1.0
    88cc7b75e6e54ee361482ca9e957dafedb0a6e4efdc4b6290211a85d5ecaeb6c
    [root@localhost learn2]# docker ps
    CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

    退出原因

    1、docker容器运行必须有一个前台进程, 如果没有前台进程执行,容器认为空闲,就会自行退出
    2、容器运行的命令如果不是那些一直挂起的命令( 运行top,tail、循环等),就是会自动退出
    3、这个是 docker 的机制问题

    解决方案

    方案1:

    网上有很多介绍,就是起一个死循环进程,让他不停的循环下去,前台永远有进程执行,那么容器就不会退出了,以centos为例

    shell>docker run -d centos /bin/sh -c "while true; do echo hello world; sleep 1; done"

    缺点: 命令太冗长了,还占用一个终端

    方案2:

    shell>docker run -dit centos /bin/bash

    添加-it 参数交互运行
    添加-d 参数后台运行
    这样就能启动一个一直停留在后台运行的Centos了。

    shell>docker ps

    容器运行起来了

    进入容器的方法:

    使用exec,不要使用attach命令
    attach命令就是使用现有终端,如果你要退出容器操作,那么bash结束,容器也就退出了
    shell>docker exec -it <container_id> /bin/bash //新建一个bash

     参考:

    [1]  docker启动容器之后马上又自动关闭了

    [2]  docker ps shows empty list

    [3]  Why docker container exits immediately

    [4]  Run container but exited immediately

    [5]  https://webkul.com/blog/docker-container-will-automatically-stop-run/

    [6]  https://www.tutorialworks.com/why-containers-stop/

  • 相关阅读:
    [CF1198D] Rectangle Painting 1
    [CF696B] Puzzles
    [CF540D] Bad Luck Island
    [P1654] OSU!
    [P6154] 游走
    [CF1265E] Beautiful Mirrors
    [CF920F] SUM and REPLACE
    [CF453B] Little Pony and Harmony Chest
    [CF808D] Array Division
    [CF1155D] Beautiful Array
  • 原文地址:https://www.cnblogs.com/firestar277/p/14682871.html
Copyright © 2011-2022 走看看