Answers: <p>It depends on what you want to archive. If your container is intended to be accessed from outside, you need port mapping: <code>-p $HOSTPORT:$CONTAINERPORT</code> So you can use <code>-p 80:80</code> as an argument for <code>docker run</code> to expose port 80. </p>
  
  <p>If this is not the case (e.g. backend server that is accessed by a reverse proxy) you could simply use <code>docker exec</code> to execute your <code>curl</code> command in the container. This also has the advantage that no external IP is needed, simply use <code>localhost</code>. It's also an better option to expose port 22 for ssh.</p>
  
  <pre><code>docker exec -it $CONTAINERNAME bash
  </code></pre>
  
  <p>where <code>bash</code> can be replaced by any login shell for interactive mode. You can also directly pass a command here like this: </p>
  
  <pre><code>docker exec -it $CONTAINERNAME curl http://localhost
  </code></pre>
  
  <p>The <code>execute</code> 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. </p>
  
Answers: CC BY-SA 3.0
Answers: 2018-02-23T13:21:45.543Z
Answers: 2018-02-23T13:21:45.543Z
Answers: Daniel
Answers: <p>Your command (curl 172.17.0.2 ) is hanging not due to expose port .It is clear<br>
  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 &amp; it helps connect your container outside your machine.</p>
  
  <p>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</p>
  
  <p>curl command is  hanging  due to  non interactive  &amp; non TTY shell . </p>
  
  <p>Run your docker run command with -it option : </p>
  
  <p>docker run -d -it -p 80:80 --name website mob</p>
  
Answers: CC BY-SA 3.0
Answers: 2018-02-03T18:12:59.497Z
Answers: 2018-02-03T18:12:59.497Z
Answers: Dheeraj Kumar
Answers: <p>You can get to know if your site is up and running in the following way,</p>
  
  <p>Map both the ports of inside container to the host using <strong>-p</strong> option and try to <code>curl localhost:port</code></p>
  
  <p><em>Scenario:-</em></p>
  
  <p>Run your container with the following command,</p>
  
  <pre><code>docker run -d --name website -p 80:80 -p 22:22 mob
  </code></pre>
  
  <p>Explanation:-  <code>-p host_port:container's_port</code></p>
  
  <p>Now, <code>curl localhost:80</code> or <code>localhost:22</code></p>
  
  <p>If your service is running properly, you will have got the response</p>
  
  <p>P.S:- Make sure nothing is running in 80 and 20 on host before running the <strong>docker run</strong> command</p>
  
Answers: CC BY-SA 3.0
Answers: 2018-01-31T12:02:33.943Z
Answers: 2018-01-31T15:24:15.957Z
Answers: 2018-01-31T15:24:15.957Z
Answers: Dave
Body: <p>I have the following <code>Dockerfile</code>:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>This is how I build my <code>image</code>:</p>
  
  <pre><code>docker build --tags mob:latest .
  </code></pre>
  
  <p>Next, this is how I bring my container up:</p>
  
  <pre><code>docker run -d --name website mob
  </code></pre>
  
  <p>Then I check my container's ip via the following command:</p>
  
  <pre><code>docker network inspect bridge
  </code></pre>
  
  <p>I would like to execute a <code>curl</code> on this ip to ensure that my website is up and running:</p>
  
  <pre><code>curl 172.17.0.2
  </code></pre>
  
  <p>But for some reason, my command keeps hanging. I'm not sure why this is happening. I'm running my <code>docker</code>on a MacOS system.</p>
  
Comments: CC BY-SA 3.0
Comments: 2018-01-31T13:28:13.777Z
Comments: What is unclear in docker documentation: https://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." ...
Comments: Tensibai
Comments: CC BY-SA 3.0
Comments: 2018-01-31T14:47:33.527Z
Comments: 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.
Comments: Valter Silva
Comments: CC BY-SA 3.0
Comments: 2018-01-31T14:51:46.080Z
Comments: 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.
Comments: Tensibai
Comments: CC BY-SA 3.0
Comments: 2018-01-31T16:05:10.220Z
Comments: Sure man, sure @Tensibai. Thanks so much for the support and incredible help!
Comments: Valter Silva
Tags: <docker><dockerfile><docker-machine>
Title: How to connect to a docker container?
UserDisplayName: Valter Silva
ThreadText: # How to connect to a docker container?
  
  (Asked by: username_0 on 2018-01-31)
  
  I have the following `Dockerfile`:
  
  ```
  FROM ubuntu:latest
  MAINTAINER username_0 username_0
  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 `docker`on a MacOS system.
  
  **Tags:** docker, dockerfile, docker-machine
  
  > **Comments:**
  > **username_1**: What is unclear in docker documentation: https://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." ...  
  > **username_0**: The documentation is *clear*. I just didn't know this information that you brought @username_1, thus my post. On the pluralsight course I did, the professor didn't use the `-p` option.  
  > **username_1**: 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.  
  > **username_0**: Sure man, sure @username_1. Thanks so much for the support and incredible help!  
  
  ---
  
  ## 3 answers
  
  ### Answer by username_4 on 2018-01-31. Score: 8 | Accepted answer
  
  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
  
  
  ---
  
  ### Answer by username_2 on 2018-02-23. Score: 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.
  
  
  ---
  
  ### Answer by username_3 on 2018-02-03. Score: 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
  
  
  ---
  
  