How to Create a Docker Compose Network with Set IP Range

Learn how to create a custom Docker Compose network with a set IP range in just two easy steps!

How to Create a Docker Compose Network with Set IP Range
Photo by Taylor Vick / Unsplash

Step 1 | Create your network

Start by creating a network for your containers to attach to. It can be named anything and can be put for any IP range that you wish

sudo docker network create -d bridge <yourNetworkName> --subnet <yourIpRange>

# A more proper example would be
sudo docker network create -d bridge natwork --subnet 172.17.0.0/24
💡
While you can define the network and subnet per container it is not recommended to do so cause if you have several containers you want to run on a single network it may interfere as docker-composewill by default attempt to create its own network

Step 2 | Implement in your docker-compose file

It's rather easy to implement your newly created network,

Simply modify your docker-compose file with the following

version: "3"

services:
  example:
    image: example:example
    networks:
      - <yourNetworkName>
# Remember it's important you use the same network name in all the places it says
# <yourNetworkName>

networks:
  <yourNetworkName>:
     name: <yourNetworkName>
     external: true

To verify it worked you can run the following command

sudo docker network inspect <yourNetworkName>