2

I have the following Dockerfile:

FROM ubuntu:latest
MAINTAINER Valter Silva
RUN apt-get update
RUN apt-get install -y apache2
ADD index.html /var/www/html/
CMD /usr/sbin/apache2ctl -D FOREGROUND
EXPOSE 22
EXPOSE 80

This is how I build my image:

docker build --tags mob:latest .

Next, this is how I bring my container up:

docker run -d --name website mob

Then I check my container's ip via the following command:

docker network inspect bridge

I would like to execute a curl on this ip to ensure that my website is up and running:

curl 172.17.0.2

But for some reason, my command keeps hanging. I'm not sure why this is happening. I'm running my dockeron a MacOS system.

  • What is unclear in docker documentation: docs.docker.com/engine/reference/builder/#expose ? Quoting: "The EXPOSE instruction does not actually publish the port. It functions as a type of documentation between the person who builds the image and the person who runs the container, about which ports are intended to be published. To actually publish the port when running the container, use the -p flag on docker run to publish and map one or more ports, or the -P flag to publish all exposed ports and map them to high-order ports." ... – Tensibai Jan 31 '18 at 13:28
  • The documentation is clear. I just didn't know this information that you brought @Tensibai, thus my post. On the pluralsight course I did, the professor didn't use the -p option. – Valter Silva Jan 31 '18 at 14:47
  • that just the official documentation, if you don't know what a statement in a file does, read its doc first (even if it works, understand what you're writing more than 'That's the way to do it from my courses') and you'll save hours of headache trying to understand what you did wrong. – Tensibai Jan 31 '18 at 14:51
  • Sure man, sure @Tensibai. Thanks so much for the support and incredible help! – Valter Silva Jan 31 '18 at 16:05
5

You can get to know if your site is up and running in the following way,

Map both the ports of inside container to the host using -p option and try to curl localhost:port

Scenario:-

Run your container with the following command,

docker run -d --name website -p 80:80 -p 22:22 mob

Explanation:- -p host_port:container's_port

Now, curl localhost:80 or localhost:22

If your service is running properly, you will have got the response

P.S:- Make sure nothing is running in 80 and 20 on host before running the docker run command

1

Your command (curl 172.17.0.2 ) is hanging not due to expose port .It is clear
for security reasons, in Dockerfile EXPOSE command is not working, you need to manually need to add -p host_port:container_port in docker run command & it helps connect your container outside your machine.

If service is not running on 80 port , then you get message like this shell> curl 172.17.0.2 curl: (7) Failed to connect to 172.17.0.2 port 80: Connection refused

curl command is hanging due to non interactive & non TTY shell .

Run your docker run command with -it option :

docker run -d -it -p 80:80 --name website mob

1

It depends on what you want to archive. If your container is intended to be accessed from outside, you need port mapping: -p $HOSTPORT:$CONTAINERPORT So you can use -p 80:80 as an argument for docker run to expose port 80.

If this is not the case (e.g. backend server that is accessed by a reverse proxy) you could simply use docker exec to execute your curl command in the container. This also has the advantage that no external IP is needed, simply use localhost. It's also an better option to expose port 22 for ssh.

docker exec -it $CONTAINERNAME bash

where bash can be replaced by any login shell for interactive mode. You can also directly pass a command here like this:

docker exec -it $CONTAINERNAME curl http://localhost

The execute command has the benefit that it can be used in already running containers. So for example you could check if a service in the container is gone after a period of time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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