Tips

How to Remove Unused Docker Networks?


Context

On my server, I had fun deploying quite a lot of different projects with Docker.

Except I got into the bad habit of creating a different network for every container I add.

Problem

The problem is that there is a limited number of networks the server can create.
When you reach that limit and want to add a new container, you may get an error message like “network mon_projet_default could not be created”.

Solutions

Solution 1

You created some networks that you no longer use. Just delete them so you can create new ones. With this command:

docker network prune

Solution 2

You can put several containers on the same network.
For example, if you use a reverse proxy, you can put the applications you want to make reachable online on the same network as it by using “networks”.

In my case, I use the “Swag” reverse proxy, so all that’s left is to deploy GLPI on its network, for example:

version: "3.8"

services:
  glpi:
    image: diouxx/glpi
    container_name : glpi
    hostname: glpi
    ports:
      - "8080:80"
    restart: unless-stopped
    networks:
      - swag_default
      
networks:
  swag_default:
    external: true