6

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.

2
  • Can't you edit the docker-compose file of backend service and make it use primus-dev network?
    – Shashank V
    Jun 17, 2020 at 19:49
  • backend is defined in another docker-compose file, but it works if I duplicate the network definition in that. There are other issues to work out, but it seems like this is possible. Jun 17, 2020 at 20:02

2 Answers 2

6

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

1
  • Thanks! This sounds good. I ran into other issues and abandoned the setup for the moment, but will keep this is mind for future use. Jun 18, 2020 at 21:21
0

I got the below error attempting Suhas answer.

"services.netbox.networks must be a list"

Adding a dash fixed it for me, e.g.

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.