Aidan Ewen Aidan Ewen - 1 month ago 8
Apache Configuration Question

How do have my docker image run apache as a service

I've built a docker image to run apache. I've built it from a ubuntu base image so that I could set the apache and php versions I wanted. I've added a

CMD
to the end of my docker file to run apache in the foreground.

FROM ubuntu:16.04

RUN apt-get update && \
apt-get install -y --allow-unauthenticated software-properties-common && \
add-apt-repository ppa:ondrej/apache2 && \
apt-get update && \

....

WORKDIR /

CMD ["/usr/sbin/apachectl","-DFOREGROUND"]


I'm trying to run the container using docker-compose (along with some other services). However the container exits immediately. The same happens if run the image with the
docker
command.

docker run my_image
docker ps


Interestingly if I run it with
docker run -it my_image
, then the container starts and I'm taken into a bash shell. (I believe the default CMD on the ubuntu base image I'm using is bash).

Am I missing something? How do I write my Dockerfile so that apache runs as a service on the image?

Answer

If you are getting a bash shell when running your image, it sounds like your CMD ["/usr/sbin/apachectl","-DFOREGROUND"] isn't taking effect.

Double check that your docker build command succeeded, and that you are running the built image and not some other image.

Here's an example Dockerfile, the relevant docker build output (specifically the CMD step), and an example of it running:

$ cat Dockerfile
FROM ubuntu:16.04
RUN apt-get -y update && apt-get -y install apache2 && rm -rf /var/lib/apt/lists/*

CMD apachectl -DFOREGROUND
$ docker build -t myapache .
Sending build context to Docker daemon 2.048 kB
Step 1 : FROM ubuntu:16.04
 ---> 2fa927b5cdd3
Step 2 : RUN apt-get -y update && apt-get -y install apache2 && rm -rf /var/lib/apt/lists/*
 ---> Using cache
 ---> f766fd84056d
Step 3 : CMD apachectl -DFOREGROUND
 ---> Running in 2814d231e50e
 ---> 774b579e43ce
Removing intermediate container 2814d231e50e
Successfully built 774b579e43ce
$ r run -d --name myapache -p 80:80 myapache
4718994f1669306c64ac6846f4e0f87aa15a63a888ca7d5c8ba711f39379db57
$ 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: 2014-03-19
    See: https://launchpad.net/bugs/1288690
...