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.

  • Unable to use sed to customize buffer size in mariadb Dockerfile
  • Connecting to an H2 Database in a Docker Container
  • Django on Docker - relation “django_session” does not exist at character 109
  • Develop guide with docker images
  • Docker how to commit images with name on repository and tag
  • Permissions inside docker
  • 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

  • Docker can't load config file, but container works fine
  • Docker machine timeout - how to fix without destroying the machine?
  • Using netcat to pipe unix socket to tcp socket
  • Problems with database setup on cookiecutter-django
  • Gracefully Stopping Docker Containers
  • How to create a superuser in Django with Linux shell script?
  • 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.