How to connect to a docker container?

22,469

Solution 1

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

Solution 2

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.

Solution 3

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

Share:
22,469
Valter Silva
Author by

Valter Silva

Updated on September 18, 2022

Comments

  • Valter Silva
    Valter Silva over 1 year

    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.