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 run sql script for postgres in docker
  • How to run multiple times a Docker container with different parameters in Kubernetes?
  • Ansible: no package available for docker-ce
  • Database Fails to Start - Host Directory as a Data Volume
  • Docker + Kubernetes build
  • Docker Shipyard - deploying a container to a specific node
  • 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?

  • Issues running cron in Docker on different hosts
  • debug docker deamon with gdb or cgdb
  • logstash on docker: set multiple pipeline
  • docker restart,exit command, reboot the host machine
  • Jackrabbit with PostgreSQL backend leaves repository in inconsistent state
  • Docker 1.0 CentOS 6.5 “unable to mount sys as readonly” when running image
  • 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.