HiveBrain v1.2.0
Get Started
← Back to all entries
snippetdockerMinor

How to connect to a docker container?

Submitted by: @import:stackexchange-devops··
0
Viewed 0 times
dockerconnectcontainerhow

Problem

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.

Solution

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

Code Snippets

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

Context

StackExchange DevOps Q#3229, answer score: 8

Revisions (0)

No revisions yet.