16

I just started using docker and followed following tutorial: https://docs.docker.com/engine/admin/using_supervisord/

FROM ubuntu:14.04
RUN apt-get update && apt-get upgrade
RUN apt-get install -y openssh-server apache2 supervisor
RUN mkdir -p /var/lock/apache2 /var/run/apache2 /var/run/sshd /var/log/supervisor
COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf
EXPOSE 22 80
CMD ["/usr/bin/supervisord"]

and

[supervisord]
nodaemon=true

[program:sshd]
command=/usr/sbin/sshd -D

[program:apache2]
command=/bin/bash -c "source /etc/apache2/envvars && exec /usr/sbin/apache2 -DFOREGROUND"

Build and run:

sudo docker build -t <yourname>/supervisord .
sudo docker run -p 22 -p 80 -t -i <yourname>/supervisord

My question is, when docker runs on my server with IP http://88.xxx.x.xxx/, how can I access the apache localhost running inside the docker container from the browser on my computer? I would like to use a docker container as a web server.

2 Answers 2

23

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.

2
  • Is it possible to access Apache in such container by browsing to a named domain name, not the IP address? I have a container running that I can access by browsing to http://127.0.0.1/, but I would like to change it to, for example, http://website.localhost/. Jan 17, 2018 at 9:38
  • It somehow didn't work just like that. In addition, I added an Nginx server that proxied requests from website.localhostto 127.0.0.1, then it worked. Jan 17, 2018 at 12:23
12

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.