compose: Unable to run service with network_mode "service:NAME"

Description

This ticket is a duplicate of https://github.com/docker/compose/issues/8766 given it was marked as “closed” but is not resolved.

Unable run to execute compose run <service> when a service is using network_mode: service:nginx

Steps to reproduce the issue:

This is a docker-compose.yml file which can replicate the issue.

version: '3'

services:
  nginx:
    image: nginx

  cli:
    image: alpine
    network_mode: service:nginx

Commands which replicate the issue.

$ compose up -d
$ compose run cli sh
Error response from daemon: network service:nginx not found

Describe the results you received:

Error response from daemon: network service:nginx not found

Describe the results you expected:

The service container to execute within the same network as the “nginx” service in this case.

This worked with Compose v1.

Additional information you deem important (e.g. issue happens only occasionally):

Output of docker compose version:

Docker Compose version v2.2.2

Output of docker info:

Client:
 Context:    default
 Debug Mode: false
 Plugins:
  app: Docker App (Docker Inc., v0.9.1-beta3)
  buildx: Docker Buildx (Docker Inc., v0.7.1-docker)
  compose: Docker Compose (Docker Inc., 2.0.0-beta.1)
  scan: Docker Scan (Docker Inc., v0.12.0)

Server:
 Containers: 129
  Running: 1
  Paused: 0
  Stopped: 128
 Images: 169
 Server Version: 20.10.12
 Storage Driver: overlay2
  Backing Filesystem: extfs
  Supports d_type: true
  Native Overlay Diff: true
  userxattr: false
 Logging Driver: json-file
 Cgroup Driver: systemd
 Cgroup Version: 2
 Plugins:
  Volume: local
  Network: bridge host ipvlan macvlan null overlay
  Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
 Swarm: inactive
 Runtimes: io.containerd.runc.v2 io.containerd.runtime.v1.linux runc
 Default Runtime: runc
 Init Binary: docker-init
 containerd version: 7b11cfaabd73bb80907dd23182b9347b4245eb5d
 runc version: v1.0.2-0-g52b36a2
 init version: de40ad0
 Security Options:
  apparmor
  seccomp
   Profile: default
  cgroupns
 Kernel Version: 5.15.5-76051505-generic
 Operating System: Pop!_OS 21.10
 OSType: linux
 Architecture: x86_64
 CPUs: 12
 Total Memory: 31.32GiB
 Name: batman
 ID: QAXJ:HDRA:DGM3:DR75:OBYA:VOV7:EEOE:K5IG:QD57:54I7:SVH6:5EQ3
 Docker Root Dir: /var/lib/docker
 Debug Mode: false
 Username: nickschuch
 Registry: https://index.docker.io/v1/
 Labels:
 Experimental: false
 Insecure Registries:
  127.0.0.0/8
 Live Restore Enabled: false

Additional environment details:

N/A

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 11
  • Comments: 18 (3 by maintainers)

Most upvoted comments

Hi @ndeloof - this has been an issue for almost a year - see #8766 - we’ve had this issue as well. I did some digging and I found out some additional info. Hope this helps.

This fails on v2 if the network (EDIT: IS) already setup, so just doing a docker compose run first will not trigger this issue. Even adding a depends_on doesn’t help because the container actually does get created either way, so that’s not the issue. This solely seems to be related to whether or not the network is created at the same time as the docker compose run.

I’ve tested this to be an issue on all docker-compose version v2, including the latest 2.11.1. It works on the latest v1 (1.29.2)

Test file:

version: '3'

services:
  nginx:
    image: nginx

  cli:
    image: alpine
    network_mode: service:nginx
idunno% /usr/local/bin/docker-compose run cli sh
Creating docker-compose-test_nginx_1 ... done
Creating docker-compose-test_cli_run ... done
/ # exit
idunno% /usr/local/bin/docker-compose run cli sh
Creating docker-compose-test_cli_run ... done
/ # exit
idunno% /usr/local/bin/docker-compose --version 
docker-compose version 1.29.2, build 5becea4c
Docker Compose version v2.11.1
WORKS: start directly with a run so it creates the network
idunno% docker compose run cli sh
[+] Running 2/2
 ⠿ Network docker-compose-test_default    Created                                                                                                                                                     0.1s
 ⠿ Container docker-compose-test-nginx-1  Created                                                                                                                                                     0.2s
[+] Running 1/1
 ⠿ Container docker-compose-test-nginx-1  Started                                                                                                                                                     0.7s
/ # exit

FAIL: trying to run with the network already setup
idunno% docker compose run cli sh
[+] Running 1/0
 ⠿ Container docker-compose-test-nginx-1  Running                                                                                                                                                     0.0s
Error response from daemon: network service:nginx not found

idunno% docker compose stop      
[+] Running 1/1
 ⠿ Container docker-compose-test-nginx-1  Stopped                                                                                                                                                     0.4s
idunno% docker compose rm -f -v
Going to remove docker-compose-test-nginx-1
[+] Running 1/0
 ⠿ Container docker-compose-test-nginx-1  Removed                                                                                                                                                     0.1s
idunno% docker compose down -v 
[+] Running 1/1
 ⠿ Network docker-compose-test_default  Removed                                                                                                                                                       0.3s

WORKS: trying again after doing a cleanup (remove container & network)
idunno% docker compose run cli sh 
[+] Running 2/2
 ⠿ Network docker-compose-test_default    Created                                                                                                                                                     0.0s
 ⠿ Container docker-compose-test-nginx-1  Created                                                                                                                                                     0.0s
[+] Running 1/1
 ⠿ Container docker-compose-test-nginx-1  Started                                                                                                                                                     0.4s
/ #

This issue has been automatically marked as not stale anymore due to the recent activity.

Unchecking [ ] Use Docker Compose v2 in macOS Docker Desktop preferences works around this when using docker-compose, but docker compose still fails.