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.

  • Docker composer copy files
  • Docker volume mount doesn't exist
  • Import host group into Docker container
  • multiple app nodes how to expose jmx in kubernetes?
  • Configuring riak-ruby to work with docker cluster
  • Vagrant start docker image on existing machine
  • 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

    update

    I used now

    sudo docker network create --subnet=172.20.0.0/16 --gateway=172.20.0.100   --ip-range=172.20.1.0/24 my-network
    sudo docker run --name postgres  --network=my-network --ip=172.20.0.10 -d postgres
    sudo docker run --name application --network=my-network --ip=172.20.0.15 -d application
    

    and configured app in APPLICATION container to look for ip 172.20.0.10

    https://docs.docker.com/engine/userguide/networking/work-with-networks/

  • Issue returning https url when returning response from a docker container
  • How to get Docker host IP on Travis CI?
  • What would cause an abort signal to be sent to a Docker container?
  • Native Docker on Windows
  • Cross container communication with Docker
  • Restarting machines in a docker swarm on AWS
  • 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

    docker-compose.yml:

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

    Command:

    docker-compose up
    

    Output:

    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.