What is the docker-compose equivalent of the docker argument --net=container:ReportWeb in the docker run command

Viewed 1053

I have two containers and they connect ok when I run the following docker run commands.

docker run --security-opt "credentialspec=file://net_app.json" -d -ti --name=ReportWeb -p 8081:80 --net=nat --ip 172.30.116.101 --restart=always net.com/reportweb:1.0.0

And then

docker run --security-opt "credentialspec=file://net_app.json" -d -ti --name=Engine --net=container:ReportWeb --restart=always net.com/report:1.0.0

However can not get the same effect in docker-compose

docker-compose file:

version: "3"
services:
  reportweb:
    build:
      context: .
      dockerfile: ReportWeb.Dockerfile
    image: net.com/reportweb:1.0.0
    tty: true
    stdin_open: true
    restart: on-failure
    ports:
      - "8081:80"
    security_opt:
      - credentialspec=file://net_app.json
    networks:
      app_net:
        ipv4_address: 172.16.238.101
  cyengine:
    build:
      context: .
      dockerfile: Report.Dockerfile
    image: net.com/report:1.0.0
    links:
      - reportweb
    tty: true
    stdin_open: true
    restart: on-failure
    depends_on:
      - reportweb
    security_opt:
      - credentialspec=file://net_app.json
    networks:
      - app_net
networks:
  app_net:
    ipam:
      driver: windows
      config:
        - subnet: 172.16.238.0/24

2 Answers

the docker doc specifies as:

version: "3.8"
services:
  web:
    networks:
      hostnet: {}

networks:
  hostnet:
    external: true
    name: host

but there a wonderful converter that you can use here: https://www.composerize.com/

and you input:

docker run --security-opt "credentialspec=file://net_app.json" -d -ti --name=Engine --net=container:ReportWeb --restart=always net.com/report:1.0.0

will turned into

version: '3.3'
services:
    report:
        container_name: Engine
        network_mode: 'container:ReportWeb'
        restart: always
        image: 'net.com/report:1.0.0'

enter image description here

Building on @ΦXocę 웃 Пepeúpa ツ's answer:

I had slightly different problem: I was already starting with a container for for Redis Enterprise described in a docker-compose file, but my Google Cloud Run minikube runtime could not connect.

Using this answer I was able to add this to my existing redis service in my docker-compose 3.7 file, instead of crafting a new docker run command:

services:
  redis:
    networks:
      - cloud-run-dev-internal
networks:
  cloud-run-dev-internal:
    external: true
    name: cloud-run-dev-internal
Related