Docker tutorials 7: installing Apache2 on docker (on a Mac) – part 2: using a Dockerfile

We can now apply what we learned in Tutorial 6 by using the Dockerfile techniques we discussed in Tutorial 3. We had the following Dockerfile:

FROM ubuntu:latest
MAINTAINER myname <>
RUN apt-get update
RUN apt-get -y upgrade
CMD /bin/bash

We now need to extend it by adding the port declaration, the Apache installation, file system mapping and the right command at the end. Our file becomes:

FROM ubuntu:latest
MAINTAINER myname <>
RUN apt-get update
RUN apt-get -y upgrade
RUN apt-get -y install apache2
ADD mySharedDirectory /var/www/html

First we install apache2, then we EXPOSE port 80, then we map mySharedDirectory on the host onto /var/www/html on the container.

What do we write in the CMD?
If we declared /bin/bash as before, after running our machine we’d need to manually start apache2ctl. Probably is not what we want.

We might try to CMD /usr/sbin/apache2ctl, but it would not work: the command returns immediately, and our container would stop!

We can try to fix this behavior by declaring

CMD /usr/sbin/apache2ctl -D FOREGROUND

because in this way the command would not return immediately. However, we could not do anything else on the container (like e.g. examining the log files from a shell while the container is running).

A possible solution is to declare CMD bash -c ‘apache2ctl start ; bash’which means that we open a shell, in that we launch in a subshell apache2ctl and another shell. In this way, the web server starts but we still have a shell allowing us to perform operations on the machine.

So this is our preferred option:

CMD bash -c 'apache2ctl start ; bash'

Of course, having written the Dockerfile, we need now to create the image and then run it, bit that’s what we also did in Tutorial 3:

docker build -t myname:myLinux2 --rm=true .
docker run -it myname:myLinux2

Tagged with: ,
Posted in Docker

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: