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 to debug python in IntelliJ idea using vagrant docker
  • Docker mount S3 container
  • How to tag a docker container?
  • Can't remove image due to error: “Error response from daemon: reference does not exist”
  • Run a shell command inside a running Docker container using Ansible
  • Docker: One image per user? Or one image for all users?
  • 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/

  • curl_slist_free_all() causes segfault on GKE w/ Debian 8.7
  • Dockerfile Dynamic Registry for Image
  • Get the same output of the nproc command in Python
  • Device mapping to container in docker lacks info
  • creating jhipster-spring boot project war file error
  • Docker run centos6 command fails
  • 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.