Connect from a Docker container to port of another docker container

I have two docker containers running on a linux host.

One with a DATABASE and one with an APPLICATION.

  • How can I access internet in docker build?
  • Ubuntu 16.10 : get recent docker & docker compose version?
  • Passing Tomcat parameters to Docker
  • supervisor curl command failing with exit 56 error
  • Using Docker to load Memory Image?
  • How to scale application with multiple exposed ports and multiple volume mounted by using docker swarm?
  • How can I connect from container APPLICATION to container DATABASE port 5432?

    For example:
    I start DATABASE container and bind port 5432 to hosts localhost with “-p5432:5432. But how can I acces this port from APPLICATION? I cannot bind port 5432 to host localhost because this port is now already used.

    Docker version 1.12.6


    I used now

    sudo docker network create --subnet= --gateway=   --ip-range= my-network
    sudo docker run --name postgres  --network=my-network --ip= -d postgres
    sudo docker run --name application --network=my-network --ip= -d application

    and configured app in APPLICATION container to look for ip

  • Bamboo Mesos Marathon
  • NodeJS + Forever + Docker configuration doesn't work
  • Tag a docker container?
  • Running a script from a mongodb docker-container
  • How to create automatic docker image after successful build of Jenkins?
  • Redis Server docker container connect to another docker container can only work with bind all
  • 2 Solutions collect form web for “Connect from a Docker container to port of another docker container”

    Alternatively you can use docker networks, easy to achieve with docker-compose. In my example you can point from your app container to postgres-container as this: postgres-container:5432


    version: "3"
        image: postgres
        command: sh -c 'sleep 10; echo \\l | PGPASSWORD=mysecretpassword psql -U postgres -h postgres-container'
        image: postgres
          - POSTGRES_PASSWORD=mysecretpassword


    docker-compose up


    Attaching to postgresql_app_1, postgresql_postgres-container_1
    postgres-container_1  | server started
    app_1                 |                                  List of databases
    app_1                 |    Name    |  Owner   | Encoding |  Collate   |   Ctype    |   Access privileges
    app_1                 | -----------+----------+----------+------------+------------+-----------------------
    app_1                 |  postgres  | postgres | UTF8     | en_US.utf8 | en_US.utf8 |
    app_1                 |  template0 | postgres | UTF8     | en_US.utf8 | en_US.utf8 | =c/postgres          +
    app_1                 |            |          |          |            |            | postgres=CTc/postgres
    app_1                 |  template1 | postgres | UTF8     | en_US.utf8 | en_US.utf8 | =c/postgres          +
    app_1                 |            |          |          |            |            | postgres=CTc/postgres
    app_1                 | (3 rows)
    app_1                 |
    1. You can use docker link to link to running containers … like below and use alias name as ip address. You can do same using docker-compose as well.

      $ docker run -d -P –name application –link db-image:db application-image

    use ‘db’ as host name in your application.

    1. You can also give your host ip to application container as db end point. It will work.
    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.