简体   繁体   中英

How link a Spring boot app at existing docker container for database?

I want to use my app from a docker container with anothers docker container, one for postgres and one for solr .

My docker compose is:

version: '3'
services:
    core:
        build: ./core
        ports:
            - "8081:8081"
        environment:
            - "SPRING_PROFILES_ACTIVE=production"
        links:
          - postgresdb
          - solrdb
    postgresdb:
        image: postgres:9.4
        container_name: postgres
        ports: 
            - "5432:5432"
        environment:
            - DB_DRIVER=org.postgresql.Driver
            - DB_URL=jdbc:postgresql://localhost:5432/db
            - DB_USERNAME=db
            - DB_PASSWORD=db
        networks:
            default:
    solrdb:
       image: solr:5.5
       container_name: solr
       ports:
            - "8983:8983"
       environment:
            - DB_URL=http://localhost:8984/solr
       networks:
            default:
networks:
    default:
        external:
            name: mynet

And already I have containers for solr and postgres created, just I want to use it. How I can do it?

You have already exposed the ports for solrdb and the postgresdb. So in your other container access these Dbs by the container names and the exposed port.

For example, solrDb should be accessed via solrdb:8983 and

the postgresdb should be accessed via postgresdb:5432

Edit : Make sure that both the containers are operating in the same network. You need to add this network field for all the containers.

postgresdb:
    image: postgres:9.4
    container_name: postgresdb
    ports: 
        - "5432:5432"
    environment:
        - DB_DRIVER=org.postgresql.Driver
        - DB_URL=jdbc:postgresql://localhost:5432/db
        - DB_USERNAME=db
        - DB_PASSWORD=db
    networks:
      default:

and in the end of all the docker file

networks:
 default:
  external:
    name: <your-network-name>

And make sure that your predefined network is running prior to the start of these containers. To create the network :

docker network create --driver overlay --scope global <your-network-name>

Note : Your container ('postgresdb') will be accessible by postgresdb:5432

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