Search code examples
dockernginxdocker-composedocker-containerjwilder-nginx-proxy

Service "nginx-proxy" uses an undefined network "nginx-proxy"


I am trying to run a WordPress site inside of a docker container on Ubuntu VPS using Nginx-Proxy.

I created the following docker-compose.yml file


version: '3.4'
services:

  nginx-proxy:
    image: jwilder/nginx-proxy
    container_name: nginx-proxy
    ports:
      - 80:80
      - 443:443
    restart: always
    networks:
      - nginx-proxy
    volumes:
      - /var/run/docker.sock:/tmp/docker.sock:ro
      - /etc/nginx/vhost.d:/etc/nginx/vhost.d:ro
      - /etc/certificates:/etc/nginx/certs

  wordpress:
    image: wordpress
    container_name: wordpress
    restart: always
    ports:
      - 8080:80
    environment:
      - VIRTUAL_HOST=wordpress.domain.com
      - VIRTUAL_PORT=5500
      - WORDPRESS_DB_HOST=db
      - WORDPRESS_DB_USER=db_username
      - WORDPRESS_DB_PASSWORD=db_password
      - WORDPRESS_DB_NAME=db_name
    depends_on:
      - nginx-proxy
      - db
    networks:
      - nginx-proxy
    volumes:
      - wordpress:/var/www/html
    ports:
      - 5500:5500
    expose:
      - 5500
  db:
    image: mysql:latest
    container_name: db
    restart: always
    environment:
       MYSQL_DATABASE: db_name
       MYSQL_USER: db_username
       MYSQL_PASSWORD: db_password
       MySQL_RANDOM_ROOT_PASSWORD: '1'
    depends_on:
      - nginx-proxy
    networks:
      - nginx-proxy
    volumes:
      - db:/var/lib/mysql
    ports:
      - 5600:5600
    expose:
      - 5600
volumes:
   wordpress:
   db:

Every time I run docker-compose up I get the following error

Service "nginx-proxy" uses an undefined network "nginx-proxy"

I created a network using the following command

docker network create nginx-proxy

Here is the output of docker network ls

enter image description here

Why do I get that error? How can I fix it?


Solution

  • Anything you name in a per-service networks: block needs to be declared in a top-level networks: block.

    version: '3.4'
    services:
      nginx-proxy:
        networks:
          - nginx-proxy # <-- matches below
    volumes: { ... }
    networks:
      nginx-proxy:      # <-- matches above
        # may be empty, but this block is required
    

    If you don't declare any networks: at all, Compose creates a network named default and attaches containers to it. For almost all uses this is what you need. So it may be simpler to just delete the networks: blocks entirely.

    version: '3.4'
    services:
      nginx-proxy:
        image: jwilder/nginx-proxy
        # No networks:; just use automatic [default]
    

    (You similarly do not need to manually provide a container_name:, or to expose: ports at the Compose level.)