Aidan Ewen Aidan Ewen - 1 year ago 91
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

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 && \



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 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 Source

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/*

$ 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
$ http://localhost:80/

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
    Modified from the Debian original for Ubuntu
    Last updated: 2014-03-19
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download