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

  • remote control nginx configuration
  • npm package.json and docker (mounting it…)
  • Why does pecl install a different version for xdebug on another host when using same Dockerfile?
  • 'Graph' object has no attribute 'SerializeToString' on Windows Docker
  • Shared Docker devicemapper lvm thinpool in a multiboot setup
  • Cannot connect to Docker daemon in Azure Container Service
  • 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?

  • Docker link container
  • Problems accessing multiple docker containers remotely
  • Docker Remote API with Weave
  • Kubernetes rc sent args to container app
  • How do I use livereload.js when my server is hosted?
  • Ubuntu16 Docker-engine startup error: “no sockets found via socket activation: make sure the service was started by systemd”
  • 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.