简体   繁体   中英

docker-compose run with specified network name

I have a docker-compose file with three services (Solr, PostgreSQL and pgAdmin), all sharing a Docker network.

version: '2'
services:
  solr:
    image: solr:7.7.2
    ports:
      - '8983:8983'
    networks:
      primus-dev:
        ipv4_address: 10.105.1.101
    volumes:
      - data:/opt/solr/server/solr/mycores
    entrypoint:
      - docker-entrypoint.sh
      - solr-precreate
      - primus
      - /opt/solr/server/solr/configsets/sample_techproducts_configs
    environment:
      - SOLR_HEAP=2048m
    logging:
      options:
        max-size: 5m

  db:
    image: "postgres:11.5"
    container_name: "primus_postgres"
    ports:
      - "5432:5432"
    networks:
      primus-dev:
        ipv4_address: 10.105.1.102
    volumes:
      - primus_dbdata:/var/lib/postgres/data
    environment:
      - POSTGRES_DB=primus75
      - POSTGRES_USER=primus
      - POSTGRES_PASSWORD=primstav

  pgadm4:
    image: "dpage/pgadmin4"
    networks:
      primus-dev:
        ipv4_address: 10.105.1.103
    ports:
      - "3050:80"
    volumes:
      - /home/nils/docker-home:/var/docker-home
    environment:
      - PGADMIN_DEFAULT_EMAIL=nils.weinander@kulturit.se
      - PGADMIN_DEFAULT_PASSWORD=dev

networks:
  primus-dev:
    driver: bridge
    ipam:
      config:
        - subnet: 10.105.1.0/24

volumes:
  data:
  primus_dbdata:

This works just fine after docker-compose up (at least pgAdmin can talk to PostgreSQL).

But, then I have a script (actuall a make target, but that's not the point here), which builds, runs and deletes a container with docker-compose run :

docker-compose run -e HOME=/app -e PYTHONPATH=/app/server -u 0 --rm backend \
    bash -c 'cd /app/server && python tools/reindex_mp.py -s -n'

This does not work as the reindex_mp.py cannot reach Solr on 10.105.1.101, as the one shot container is not on the same Docker network. So, is there a way to tell docker-compose to use a named network with docker-compose run ? docker run has an option --network but that is not available for docker-compose.

You can create a docker network outside your docker-compose and use that network while running services in docker-compose.

docker network create my-custom-created-network

now inside your docker-compose, use this network like this:

services:
  serv1:
    image: img
    networks:
      my-custom-created-network

networks:
  my-custom-created-network:
    external: true

The network creation example creates a bridge network. To access containers across hosts, use an overlay network.

You can also use the network created inside docker-compose and connect containers to that network.

Docker creates a default network for docker-compose and services which do not have any network configuration specified, will be using default network created by docker for that compose file.

you can find the network name by executing this command:

docker network ls

Use the network appropriate name while starting a container, like this

docker run [options] --network <network-name> <image-name>

Note: Containers in a same network are accessible using container names, you can leverage this instead of using ips

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