Docker Machine with Bridged Network Adapter Example
Welcome readers! In this tutorial, we will see how the Docker Machine works with Bridged Network Adapter. We will discuss the Docker network and how to create a simple Bridge managed network.
Please note, due to security reasons I have blacked out some of the IP addresses of the Docker network configuration.
1. Introduction to Docker
In the present world, Docker is an important term,
- Often used in CI/CD platform that packages and runs the application with its dependencies inside a container
- Is a standard for Linux Containers
- A Container is a runtime that runs under any Linux kernel and provides a private machine-like space under Linux
1.1 Docker Terminology
- Image: Representation of Docker container i.e. a JAR or WAR file in Java
- Container: Runtime of Docker i.e. a deployed and running Docker image. For example, an executable Spring Boot jar
- Engine: The code that manages, creates and runs the Docker containers
- Hub: A public developers registry to distribute their code
- Repository: A collection of Docker related images i.e. different versions of the same application
1.2 Docker Command Basics
Here’s an example command.
1.3 Need for using Docker
- For environment replication, while the code runs locally on the machine
- For numerous deployment phases i.e. Dev/Test/QA
- For version control and distributing the application’s OS within a team
1.4 Setting up Docker
If someone needs to go through the Docker installation, please watch this video. To start with this tutorial, we are hoping that users at present have the Docker installation completed.
2. Docker Machine with Bridged Network Adapter Example
Network configuration is used to provide container isolation which allows web applications to work securely. Let us go ahead and explore the steps required to set up a bridged network in Docker.
2.1 Docker Network
Docker installation automatically created three networks i.e. bridge
, none
, and host
.
- bridge: All Docker installations represent the
docker0
network and the Docker are connected by default to this network. Any container which is running on this network communicates with one another via the IP addresses. Make note, Docker does not support automatic service discovery in this mode, so the developer must connect containers with the--link
option in thedocker run
command. This linking allows communication between the containers on thedocker0
network - none: This network lacks a network interface i.e. it has a local interface with no external network interface
- host: This network enables a container to attach to the host’s network
- bridge: All Docker installations represent the
To view the existing Docker networks, run
List Docker n/w’s
1 2 | # To view existing docker networks. docker network ls |
If everything goes well, the Docker network list will be shown as in Fig. 2.
To get further details developers can use the inspect
command.
Inspect Docker n/w
# To get details of a docker network. docker inspect <network_name>
If everything goes well, the Docker command will return the JSON object describing the network (i.e. including information which containers run on the network, options set, subnet, and the gateways) as shown in Fig. 3.
2.2 Creating a Bridge network
Bridge network (i.e. the docker0
network) is the simplest way to create your own network. So in this section, we will create our network and run a container on it. Developers need to use a network create
command.
Network creation
# To create a new network. docker network create --driver bridge <my_network_name>
If everything goes well, the command will create a new network as shown in Fig. 4.
Now developers can use the inspect
command to list the network properties. To run a container on this newly created network, we will use the following.
Running a container
# To run a container. docker run -d --net=<my_network_name> --name=<container_name> <any_name_given_to_container>
If everything goes well and the network is created successfully, we will start a container on this new network. We will start the PostgreSQL container on the network we created in Fig. 4. Once the container is successfully started using the docker run command, we will inspect the network to check if the container was successfully attached to the newly created network.
Any other Docker container we create on this network can immediately connect to any other container on the same network. The network isolates containers from other networks. Try out these commands in your development environment to practice and learn.
3. Conclusion
In this tutorial, we saw the following:
- Introduction to Docker and Docker networks
- Commands to create a bridge network and attach a simple container to it
That is all for this tutorial and I hope the tutorial will help you to create new bridge networks on Docker. As the diagram visibility is low readers can download the high-resolution diagrams from the Download section. Happy learning and do not forget to share!
4. Download the source code
You can download the full source code of this example here: Docker Machine with Bridged Network Adapter Example