How to connect an image using a custom bridge?

I start with a Spring Boot application image, which depends on the PostgresSql database. Thus, the Spring boot container does not start if there is no database for it.

The database works, but since the --link option --link now deprecated in docker. How can I connect these two containers when I cannot start SpringBoot and then run the docker network connect my-net postgres .

I created both images with separate Dockerfiles , maybe this is possible using the docker-compose approach?

+5
source share
2 answers

I recommend using a docker socket file to determine your services. Using the depends_on file, you can use the depends_on option:

 version: '3' services: web: build: context: . dockerfile: dockerfile-web depends_on: - postgres-db postgres-db: build: context: . dockerfile: dockerfile-postgres-db 

You can also define networks in the docker layout file to connect the container:

 version: '3' services: web: build: context: . dockerfile: dockerfile-web depends_on: - postgres-db networks: - backend-net postgres-db: build: context: . dockerfile: dockerfile-postgres-db networks: - backend-net networks: backend-net: driver: bridge 
+4
source

Start by using a custom bridge network. This way you do not need to use --link , which is considered deprecated, and the containers will be able to communicate using their names. This is what the bridge network does not provide by default.

From: Docker Docs

Differences between custom bridges and the default bridge

  • Custom bridges provide automatic DNS resolution between containers.

Secondly, you can do this “manually” by running docker commands in the appropriate order / depending on your case, but, as Sebastian showed in the docker-compose file, it’s much easier to handle this.

+3
source

Source: https://habr.com/ru/post/1275779/


All Articles