Kaustubh Trivedi
Kaustubh Trivedi

Reputation: 408

Access port of one container from another container

I have a postgres database in one container, and a java application in another container. Postgres database is accessible from port 1310 in localhost, but the java container is not able to access it. I tried this command:

docker run modelpolisher_java java -jar ModelPolisher-noDB-1.7.jar --host=biggdb --port=5432 --user=postgres --passwd=postgres --dbname=bigg

But it gives error java.net.UnknownHostException: biggdb.

Here is my docker-compose.yml file:

version: '3'
services:
  biggdb:
    container_name: modelpolisher_biggdb
    build: ./docker/bigg_docker
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
      - POSTGRES_DB=bigg
    ports:
      - "1310:5432"
  java:
    container_name: modelpolisher_java
    build: ./docker/java_docker
    stdin_open: true
    tty: true

Dockerfile for biggdb:

FROM postgres:11.4

RUN apt update &&\
apt install wget -y &&\
# Create directory '/bigg_database_dump/' and download bigg_database dump as 'database.dump'
wget -P /bigg_database_dump/ https://modelpolisher.s3.ap-south-1.amazonaws.com/bigg_database.dump &&\
rm -rf /var/lib/apt/lists/*

COPY ./scripts/restore_biggdb.sh /docker-entrypoint-initdb.d/restore_biggdb.sh

EXPOSE 1310:5432

Can somebody please tell what changes I need to do in the docker-compose.yml, or in the command, to make java container access ports of biggdb (postgres) container?

Upvotes: 0

Views: 1202

Answers (2)

Kaustubh Trivedi
Kaustubh Trivedi

Reputation: 408

As you use docker-compose to bring up the two containers, they already share a common network. To be able to access that you should use docker-compose run and not docker run. Also, pass the service name (java) and not the container name (modelpolisher_java) in docker-compose run command.

So just use the following command to run your jar:

docker-compose run java java -jar ModelPolisher-noDB-1.7.jar --host=biggdb --port=5432 --user=postgres --passwd=postgres --dbname=bigg

Upvotes: 0

David Maze
David Maze

Reputation: 158705

The two containers have to be on the same Docker-internal network to be able to talk to each other. Docker Compose automatically creates a network for you and attaches containers to that network. If you're docker run a container alongside that, you need to find that network's name.

Run

docker network ls

This will list the Docker-internal networks you have. One of them will be named something like bigg_default, where the first part is (probably) your current directory name. Then when you actually run the container, you can attach to that network with

docker run --net bigg_default ...

Consider setting a command: in your docker-compose.yml file to pass these arguments when you docker-compose up. If the --host option is your code and doesn't come from a framework, passing settings like this via environment variables can be a little easier to manage than command-line arguments.

Upvotes: 1

Related Questions