0
votes

I have created a docker file and while i am building and runing the container the container exits immediately.

I am using Ubuntu 18.04 as the base OS on which docker is installed.

My docker file contains the following

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>

Command Used to build the image is : sudo docker build -t myimage .

Command user to Run the Container is :

sudo docker run -it -p 80:80 myimage

Following output

sudo docker ps -a <br>

CONTAINER ID IMAGE COMMAND
CREATED STATUS PORTS
NAMES 6b1891ac8195 myimage "apache2ctl /usr/sbi…"
15 minutes ago Exited (1) 15 minutes ago
infallible_williamson
dc3cc1328508 5b5570dec3a9
"tail -f /dev/null /…" 17 minutes ago Exited (1) 17 minutes ago stoic_ganguly
a67f5cb9f080 53a1c1ddc4fc "tail -f /dev/null /…" 18 minutes ago Exited (1) 18 minutes ago
vibrant_grothendieck
07fa216c6c00 b5d19c3240f7
"apache2ctl /usr/sbi…" 31 minutes ago Exited (1) 31 minutes ago keen_blackburn
71d686cb0b8e 568c96482f1c
"apache2ctl /bin/sh …" 39 minutes ago Exited (1) 38 minutes ago happy_saha
11e0abe7c2ec d056b6f1d824 "apache2ctl /bin/sh …" 40 minutes ago Exited (1) 40 minutes ago
vibrant_kare
17ed24e8eef4 d056b6f1d824 "apache2ctl /bin/sh …" 3 hours ago Exited (1) 3 hours ago
gallant_dijkstra
b1c6d9bf2765 d056b6f1d824
"apache2ctl /bin/sh …" 3 hours ago Exited (1) 3 hours ago
elated_joliot

Please help.

1

1 Answers

0
votes

Few things to correct in your Dockerfile

Use specific versions for your base images

FROM ubuntu:18.04 add specific version to your FROM images, this helps to understand what version of base image(Ubuntu:18.04) this Docker image is built with.

Combine multiple RUN commands

Combine multiple RUN commands to single RUN command, if possible, helps to reduce number of docker layers which in turn reduces the size of the image.

Check Dockerfile best practices - https://docs.docker.com/develop/develop-images/dockerfile_best-practices/#run

Change this from

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

to

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

CMD correction

As you have already mentioned apache2ctl in your ENTRYPOINT no need to mention it again in your CMD. Your command is interpreted as apache2ctl /usr/sbin/apache2ctl -DFOREGROUND. Hence docker run command is failing.

So here is how your Dockerfile was modified:

$ 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"]

Now build Docker image as you have done.

$ 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

Run the container in detached mode, -d, as to run process in the background.

$ 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

Hit the URL with localhost and port 80

$ 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;

Also from the browser

enter image description here