Docker – Creating Docker networks

File Management Commands in Linux

OK, you now know a lot about both the local and the remote network drivers, and you have seen how several of them are created for you when you install Docker and/or initialize swarm mode (or install a remote driver). But, what if you want to create your own networks using some of these drivers? It is really pretty simple. Let’s take a look. The built-in help for the network create¬†command looks like this:

# Docker network create command syntax
# Usage: docker network create [OPTIONS] NETWORK

Examining this, we see there are essentially two parts of this command we need to handle, the OPTIONS followed by the NETWORK name to make the network we wish to create. What are our options? Well, there are quite a lot, but let’s pick out a few to get you going quickly.

Probably the most important option is the --driver option. This is how we tell Docker which of the pluggable network drivers to use when creating this network. As you have seen, the choice of driver determines the network characteristics. The value you supply to the driver option will be like the ones shown in the DRIVER column of the output from the docker network ls command. Some of the possible values are bridge, overlay, and macvlan. Remember that you cannot create additional host or null networks as they are limited to one per Docker host. So far, what might this look like? Here is an example of creating a new overlay network, using mostly defaults for options:

# Create a new overlay network, with all default options
docker network create -d overlay defaults-over

That works just fine. You can run new services and attach them to your new network. But what else might we want to control in our network? Well, how about the IP space? Yep, and Docker provides options for controlling the IP settings for our networks. This is done using the --subnet, --gateway, and --ip-range optional parameters. So, let’s take a look at creating a new network using this options.¬†See Chapter 2, Learning Docker Commands, for how to install jq if you have not done so already:

# Create a new overlay network with specific IP settings
docker network create -d overlay \
--subnet= \
--ip-range= \
--gateway= \
# Initial validation
docker network inspect specifics-over --format '{{json .IPAM.Config}}' | jq

Executing the preceding code in my lab looks like this:

Looking over this example, we see that we created a new overlay network using specific IP parameters for the subnet, the IP range, and the gateway. Then, we validated that the network was created with the requested options. Next, we created a service using our new network. Then, we found the container ID for a container belonging to the service and used it to inspect the network settings for the container. We can see that the container was run using an IP address (in this case, from the IP range we configured our network with. Looks like we made it! 

As mentioned, there are many other options available when creating Docker networks, and I will leave it as an exercise for you to discover them with the docker network create --help command, and to try some of them out to see what they do.

Comments are closed.