【问题标题】:Docker Apache Container exist immediately (Build from Docker File) on Ubuntu 18.04Docker Apache Container 在 Ubuntu 18.04 上立即存在(从 Docker 文件构建)
【发布时间】:2023-03-10 19:09:01
【问题描述】:

我创建了一个 docker 文件,当我构建和运行容器时,容器立即退出。

我使用 Ubuntu 18.04 作为安装 docker 的基础操作系统。

我的 docker 文件包含以下内容

FROM ubuntu <br>
RUN apt-get update <br>
RUN apt-get install apache2 -y <br>
RUN apt-get install apache2-utils -y <br>
RUN apt-get clean <br>
RUN rm -rf /var/lib/apt/lists/* <br>
EXPOSE 80 <br>
ENTRYPOINT ["apache2ctl"] <br>
CMD ["/usr/sbin/apache2ctl", "-DFOREGROUND"] <br>

用于构建图像的命令是: sudo docker build -t myimage.

运行容器的命令用户是:

sudo docker run -it -p 80:80 myimage

跟随输出

sudo docker ps -a <br>

容器 ID 图像命令
创建状态端口
名称 6b1891ac8195 myimage "apache2ctl /usr/sbi..."
15 分钟前退出 (1) 15 分钟前
infallible_williamson
dc3cc1328508 5b5570dec3a9
“tail -f /dev/null /…” 17 分钟前 退出 (1) 17 分钟前 stoic_ganguly
a67f5cb9f080 53a1c1ddc4fc "tail -f /dev/null /..." 18 分钟前退出 (1) 18 分钟前
充满活力的_grothendieck
07fa216c6c00 b5d19c3240f7
“apache2ctl /usr/sbi…” 31 分钟前 退出 (1) 31 分钟前 敏锐布莱克本
71d686cb0b8e 568c96482f1c
“apache2ctl /bin/sh ...” 39 分钟前 退出 (1) 38 分钟前 happy_saha
11e0abe7c2ec d056b6f1d824 "apache2ctl /bin/sh ..." 40 分钟前退出 (1) 40 分钟前
充满活力的_kare
17ed24e8eef4 d056b6f1d824 "apache2ctl /bin/sh ..." 3 小时前 退出 (1) 3 小时前
Gallant_dijkstra
b1c6d9bf2765 d056b6f1d824
"apache2ctl /bin/sh ..." 3 小时前 退出 (1) 3 小时前
兴高采烈

请帮忙。

【问题讨论】:

    标签: ubuntu dockerfile containers


    【解决方案1】:

    您的 Dockerfile 中需要纠正的地方很少

    为您的基础图像使用特定版本

    FROM ubuntu:18.04 将特定版本添加到您的FROM 映像中,这有助于了解此 Docker 映像是使用什么版本的基础映像(Ubuntu:18.04)构建的 .

    组合多个RUN 命令

    将多个 RUN 命令合并到单个 RUN 命令,如果可能的话,有助于减少 docker 层的数量,从而减小图像的大小。

    检查 Dockerfile 最佳实践 - https://docs.docker.com/develop/develop-images/dockerfile_best-practices/#run

    改变这个

    RUN apt-get update 
    RUN apt-get install apache2 -y 
    RUN apt-get install apache2-utils -y 
    

    RUN apt-get update && \
        apt-get install -y apache2 apache2-utils && \
        apt-get clean && \
        rm -rf /var/lib/apt/lists/*
    

    CMD 修正

    正如您在ENTRYPOINT 中已经提到的apache2ctl 无需在您的CMD 中再次提及。您的命令被解释为 apache2ctl /usr/sbin/apache2ctl -DFOREGROUND。因此 docker run 命令失败。

    以下是您的Dockerfile 的修改方式:

    $ cat Dockerfile 
    FROM ubuntu:18.04
    RUN apt-get update && \
        apt-get install -y apache2 apache2-utils && \
        apt-get clean && \
        rm -rf /var/lib/apt/lists/*
    EXPOSE 80
    ENTRYPOINT ["apache2ctl"]
    CMD ["-DFOREGROUND"]
    
    

    现在构建 Docker 映像,就像你所做的那样。

    $ docker build -t myimage:1.0 .
    Sending build context to Docker daemon   38.3MB
    Step 1/5 : FROM ubuntu:18.04
     ---> cf0f3ca922e0
    Step 2/5 : RUN apt-get update &&     apt-get install -y apache2 apache2-utils &&     apt-get clean &&     rm -rf /var/lib/apt/lists/*
     ---> Using cache
     ---> 2f47b3a5e735
    Step 3/5 : EXPOSE 80
     ---> Using cache
     ---> 5b395b19a0dc
    Step 4/5 : ENTRYPOINT ["apache2ctl"]
     ---> Using cache
     ---> be1afc8d76c3
    Step 5/5 : CMD ["-DFOREGROUND"]
     ---> Running in 3d0d78c566e2
    Removing intermediate container 3d0d78c566e2
     ---> 00167996f7f3
    Successfully built 00167996f7f3
    Successfully tagged myimage:1.0
    

    以分离模式运行容器-d,用于在后台运行进程。

    $ docker run -d -p 80:80 myimage:1.0
    eccb77059f9ce8628e7a47d64adb9d20a3ca6cb3cecce06935b46eb13652b992
    
    
    $ docker ps | grep eccb77
    eccb77059f9c        myimage:1.0       "apache2ctl -DFOREGR…"   14 seconds ago      Up 12 seconds       0.0.0.0:80->80/tcp                     charming_payne
    
    $ docker ps | grep eccb77
    eccb77059f9c        myimage:1.0       "apache2ctl -DFOREGR…"   27 seconds ago      Up 25 seconds       0.0.0.0:80->80/tcp                     charming_payne
    
    $ docker ps | grep eccb77
    eccb77059f9c        myimage:1.0        "apache2ctl -DFOREGR…"   4 minutes ago       Up 4 minutes        0.0.0.0:80->80/tcp                     charming_payne
    

    使用 localhost 和端口 80 访问 URL

    $ curl http://localhost:80
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
      <!--
        Modified from the Debian original for Ubuntu
        Last updated: 2016-11-16
        See: https://launchpad.net/bugs/1288690
      -->
      <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <title>Apache2 Ubuntu Default Page: It works</title>
        <style type="text/css" media="screen">
      * {
        margin: 0px 0px 0px 0px;
        padding: 0px 0px 0px 0px;
    

    也来自浏览器

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-02
      • 1970-01-01
      • 1970-01-01
      • 2018-01-24
      • 1970-01-01
      • 2021-10-09
      • 2020-10-22
      • 2016-04-04
      相关资源
      最近更新 更多