eduartua eduartua - 3 months ago 48
Apache Configuration Question

Running a Docker container that accept traffic from the host

I have the following config:

Dockerfile

FROM centos
MAINTAINER Eduar Tua <eduartua@gmail.com>

RUN yum -y update && yum clean all
RUN yum -y install httpd && yum clean all
RUN echo "Apache works" >> /var/www/html/index.html

EXPOSE 80

ADD run-apache.sh /run-apache.sh
RUN chmod -v +x /run-apache.sh

CMD ["/run-apache.sh"]


The run-apache.sh script:

#!/bin/bash

rm -rf /run/httpd/* /tmp/httpd*

exec /usr/sbin/apachectl -D FOREGROUND


Then I build the image with:

sudo docker build --rm -t platzi/httpd .


then

sudo docker run -d -p 80:80 platzi/httpd


After that when I try to run the container accepting connections from the host in the 80 port I get this:

67ed31b50133adc7c745308058af3a6586a34ca9ac53299d721449dfa4996657
FATA[0002] Error response from daemon: Cannot start container 67ed31b50133adc7c745308058af3a6586a34ca9ac53299d721449dfa4996657: Error starting userland proxy: listen tcp 0.0.0.0:80: bind: address already in use


Any help?

Answer

The error seems pretty clear:

FATA[0002] Error response from daemon: Cannot start container 67ed31b50133adc7c745308058af3a6586a34ca9ac53299d721449dfa4996657: Error starting userland proxy: listen tcp 0.0.0.0:80: bind: address already in use

It says, "address already in use". This means that something on your system -- probably a web server like Apache -- is already listening on port 80. You will either need to:

  • stop the web server,
  • select a different host port in the -p argument to docker run or
  • just drop the -p argument.

Because Docker can't set up the requested port forwarding, it does not start the container.

Options (a) and (b) will both allow the container to bind to port 80 on your host. This is only necessary if you want to access the container from somewhere other than your host.

Option (c) is useful if you only want to access the container from the docker host but do not want to otherwise expose the container on your local network. In this case, you would use the container ip address as assigned by docker, which you can get by running docker inspect and perusing the output, or just running:

docker inspect --format '{{ .NetworkSettings.IPAddress }}' container_id
Comments