What's Actually Happening
You're trying to connect a container to a Docker network or start a Docker Compose project, but Docker reports that the network doesn't exist. This typically happens when networks are deleted, Docker Compose projects are improperly cleaned up, or there's a network naming mismatch.
The Error You'll See
```bash $ docker run --network my-network my-app Error: No such network: my-network
$ docker network connect my-network my-container Error: No such network: my-network
$ docker-compose up
ERROR: Network myapp_default declared as external, but could not be found. Please create the network manually using docker network create myapp_default and try again.
$ docker-compose --project-name myproject up ERROR: for myservice network myproject_default not found ```
Why This Happens
- 1.Network was never created: Referenced network doesn't exist
- 2.Network deleted: Previous cleanup removed the network
- 3.Docker Compose project mismatch: Using wrong project name or external network reference
- 4.Docker daemon restart: Networks can be lost after daemon restart (rare)
- 5.Network naming issues: Using wrong network name or scope
- 6.Swarm mode conflicts: Overlay networks not available on all nodes
Step 1: List Available Networks
First, check what networks exist:
```bash # List all networks docker network ls
# Output example: NETWORK ID NAME DRIVER SCOPE abc123def456 bridge bridge local def789ghi012 host host local ghi345jkl678 none null local mno901pqr234 my-network bridge local
# Filter by driver docker network ls --filter driver=bridge
# Show detailed network info docker network inspect my-network ```
Step 2: Create the Missing Network
If the network doesn't exist, create it:
```bash # Create a basic bridge network docker network create my-network
# Create with specific driver docker network create --driver bridge my-network
# Create with custom subnet docker network create \ --driver bridge \ --subnet=172.20.0.0/16 \ --gateway=172.20.0.1 \ my-network
# Create an overlay network (for Swarm) docker network create --driver overlay my-overlay-network
# Create attachable overlay network docker network create --driver overlay --attachable my-overlay-network ```
Step 3: Fix Docker Compose Network Issues
For Docker Compose projects, ensure proper network configuration:
Issue: External network not found ```yaml # docker-compose.yml (wrong - network doesn't exist) version: '3.8' services: app: image: my-app networks: - my-network
networks: my-network: external: true # Error if network doesn't exist ```
Fix 1: Create the network first
``bash
docker network create my-network
docker-compose up
Fix 2: Use managed network instead ```yaml # docker-compose.yml (correct - creates network automatically) version: '3.8' services: app: image: my-app networks: - my-network
networks: my-network: driver: bridge ```
Fix 3: Use external network with name ```yaml # docker-compose.yml version: '3.8' services: app: image: my-app networks: - my-network
networks: my-network: external: true name: actual-network-name # Specify actual network name ```
Step 4: Fix Project Name Mismatch
Docker Compose creates networks with project name prefix:
```bash # Docker Compose creates: projectname_networkname # If project name is "myapp", network becomes: myapp_default
# Check actual network names docker network ls | grep myapp
# Specify project name explicitly docker-compose -p myproject up
# Or use COMPOSE_PROJECT_NAME COMPOSE_PROJECT_NAME=myproject docker-compose up ```
Configure default project name in .env:
``bash
# .env file
COMPOSE_PROJECT_NAME=myapp
# docker-compose.yml
version: '3.8'
name: myapp # Explicit project name
services:
app:
image: my-appStep 5: Handle Multi-Project Networks
When sharing networks between projects:
```yaml # Project A: docker-compose.yml version: '3.8' services: app: image: my-app networks: - shared-network
networks: shared-network: driver: bridge name: shared-network # Explicit name without project prefix ```
```yaml # Project B: docker-compose.yml version: '3.8' services: app: image: my-app networks: - shared-network
networks: shared-network: external: true name: shared-network # Reference the same network ```
Step 6: Fix Swarm Overlay Network Issues
For Swarm mode overlay networks:
```bash # Check if Swarm is active docker info | grep Swarm
# If not, initialize Swarm docker swarm init
# Create overlay network on manager node docker network create --driver overlay my-overlay
# Make it attachable for standalone containers docker network create --driver overlay --attachable my-overlay
# Verify network is available on all nodes docker service create --name test --network my-overlay nginx
# Check network scope docker network inspect my-overlay | grep Scope ```
Step 7: Clean Up and Recreate Networks
Sometimes networks are in a corrupted state:
```bash # Remove unused networks docker network prune
# Remove specific network docker network rm my-network
# Disconnect all containers from network first docker network inspect my-network -f '{{range .Containers}}{{.Name}} {{end}}' | \ xargs -I {} docker network disconnect my-network {}
# Then remove docker network rm my-network
# Recreate docker network create my-network ```
Step 8: Debug Network Connectivity
Verify network is working correctly:
```bash # Connect container to network docker network connect my-network my-container
# Test connectivity between containers docker run --network my-network --name test1 -d alpine sleep 300 docker run --network my-network --name test2 -d alpine sleep 300
# Ping between containers docker exec test1 ping -c 3 test2
# Check DNS resolution docker exec test1 nslookup test2
# Check network details docker network inspect my-network
# View container network interfaces docker exec test1 ip addr ```
Verify the Fix
Confirm the network exists and containers can connect:
```bash # Create test network docker network create test-network
# Verify network exists docker network ls | grep test-network
# Run container on network docker run -d --name test-container --network test-network nginx
# Inspect network shows container docker network inspect test-network
# Check container is connected docker inspect test-container -f '{{range $k, $v := .NetworkSettings.Networks}}{{$k}}{{end}}'
# Cleanup test docker rm -f test-container docker network rm test-network ```
You should now be able to create networks and connect containers without "network not found" errors.