Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use docker container as apache server?

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.

like image 635
UpCat Avatar asked Jan 04 '15 17:01

UpCat


1 Answers

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.

like image 125
wassgren Avatar answered Oct 17 '22 06:10

wassgren