简体   繁体   中英

Docker Compose with Multiple Networks

I have a docker-compose file that specifies two containers which are to be created in two separate networks:

  1. A mysql database -- this contains the adventureworks data (and belongs to adventureworks-db-nw network).
  2. A java based webapp -- this talks to the mysql database to perform CRUD operations (and belongs to adventureworks-app-nw network).
version: '3'
services:
    adventureworks-db:
        image: mysql
        container_name: adventureworks-db
        networks: 
            - adventureworks-db-nw
        ports:
            - 3306:3306
    adventureworks-app:
        container_name: adventureworks-app
        networks: 
            - adventureworks-app-nw
        ports:
            - 8080:8080

networks: 
    adventureworks-db-nw:
        driver: bridge
    adventureworks-app-nw:
        driver: bridge

For brevity, I have omitted certain parts of the docker-compose.yml file that will perhaps not be relevant to the question I have.

How can I get the java webapp in adventureworks-app container, that belongs to the adventureworks-app-nw to talk to the mysql database in adventureworks-db container, that belongs to the adventureworks-db-nw network?

Please note that I want to maintain the separation of these two networks as two separate custom (user-created) docker networks.

If you want the java app to communicate with the db container via it's service name, then you would have to add the "adventureworks-db-nw" network to the app service.

If you don't want to do that then since you are exposing the ports I guess you should be able to connect by using the host ip/hostname and the exposed port.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM