Connect to a service in docker, run from an interactive shell

Suppose I have the following container:

docker run -i -t test/python3 /bin/bash

  • how to debug python in IntelliJ idea using vagrant docker
  • Postgres docker-image won't run and won't start [closed]
  • How do I get a Docker Gitlab CI runner to access Git on its parent host?
  • Why does SSH need a passphrase for a passphrase-less key?
  • log-driver=gelf does not work on CoreOS?
  • How to launch a new docker container from inside a docker container on openshift
  • and run the following in the shell:

    python3 -m http.server 8080

    How do I connect to this port from the host? Is this possible?

  • Start Filebeat using Supervisor
  • kubernetes on ubuntu 16.04
  • Modifying golang Docker Container using the running shell
  • RabbitMQ on Docker: Pika hangs on connection when given port, connection refused without port
  • Docker can't pull image from repository
  • Using ssh-agent with docker
  • One Solution collect form web for “Connect to a service in docker, run from an interactive shell”

    You can connect directly to the container using its private IP. To discover it, in the host terminal you can run docker inspect --format '{{ .NetworkSettings.IPAddress }}' <container_id>. The container_id can be obtained using docker ps once the container is running (also from the host). Once you have that ip you can access directly to it to the port you wish.

    Also you have the option to publish a container port to the host machine, thus you can access directly from you localmachine to localhost:8080, or from any other machine that has visibility with the host (to the adequate ip or hostname). To publish the port you have to use the option -p, as describer in the docker run prompt help:

    $ docker run --help
    -p, --publish=[]           Publish a container's port to the host
                                   format: ip:hostPort:containerPort | ip::containerPort | hostPort:containerPort | containerPort

    So in that case you could run docker run -i -t -p 8080:8080 test/python3 /bin/bash to, for example, publish the port 8080 of the container to the same number port of your local machine (you could choose any other not already in use in your host machine). Also may be you don’t need to run the container in a interactive way if you don’t need to perform any further action inside (or doing this using a script), and running directly the command you wish to execute: docker run -i -t -p 8080:8080 test/python3 python3 -m http.server 8080

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.