How to use docker container as apache server? How to use docker container as apache server? docker docker

How to use docker container as apache server?


You will have to use port forwarding to be able to access your docker container from the outside world.

From the Docker docs:

By default Docker containers can make connections to the outside world, but the outside world cannot connect to containers.

But if you want containers to accept incoming connections, you will need to provide special options when invoking docker run.

So, what does this mean? You will have to specify a port on your host machine (typically port 80) and forward all connections on that port to the docker container. Since you are running Apache in your docker container you probably want to forward the connection to port 80 on the docker container as well.

This is best done via the -p option for the docker run command.

sudo docker run -p 80:80 -t -i <yourname>/supervisord

The part of the command that says -p 80:80 means that you forward port 80 from the host to port 80 on the container.

When this is set up correctly you can use a browser to surf onto http://88.x.x.x and the connection will be forwarded to the container as intended.

The Docker docs describes the -p option thoroughly. There are a few ways of specifying the flag:

# Maps the provided host_port to the container_port but only # binds to the specific external interface-p IP:host_port:container_port# Maps the provided host_port to the container_port for all # external interfaces (all IP:s)-p host_port:container_port

Edit: When this question was originally posted there was no official docker container for the Apache web server. Now, an existing version exists.

The simplest way to get Apache up and running is to use the official Docker container. You can start it by using the following command:

$ docker run -p 80:80 -dit --name my-app -v "$PWD":/usr/local/apache2/htdocs/ httpd:2.4

This way you simply mount a folder on your file system so that it is available in the docker container and your host port is forwarded to the container port as described above.


There is an official image for apache. The image documentation contains instructions in how you can use this official images as a base for a custom image.

To see how it's done take a peek at the Dockerfile used by the official image:

https://github.com/docker-library/httpd/blob/master/2.4/Dockerfile

Example

Ensure files are accessible to root

sudo chown -R root:root /path/to/html_files

Host these files using official docker image

docker run -d -p 80:80 --name apache -v /path/to/html_files:/usr/local/apache2/htdocs/ httpd:2.4

Files are accessible on port 80.