Docker Network
Multi-tier Web Application project with Docker Networks and Docker File
Docker Network is a feature within the Docker ecosystem that enables communication between Docker containers running on the same host or across different hosts.
It essentially provides a virtual network interface to Docker containers, allowing them to communicate with each other and with the external world.
- Bridge Networks: Default for Docker; enables communication between containers on the same host.
- Overlay Networks: Facilitates communication between containers on different Docker hosts in distributed environments.
- Macvlan Networks: Provides containers with unique MAC addresses, allowing direct access from the external network.
- Custom Networks: Allows users to create networks with specific configurations like subnetting and isolation policies.
- Service Discovery and Load Balancing: Integrates with mechanisms for dynamic communication and ensures high availability of containerized applications.
COMMANDS :
1. To create a Docker network:
docker network create [OPTIONS] NETWORK_NAME
Driver Options:
--driver
: Specify the driver to use for the network.- For example,
--driver bridge
(default),--driver overlay
,--driver macvlan
, etc.
IPAM Options (IP Address Management):
--subnet
: Specify the subnet for the network.--gateway
: Specify the gateway for the network.
Other Options:
--attachable
: Create an attachable network, which means other containers can be connected to it later.--internal
: Create an internal network, which means it's only accessible by other containers on the same network.
Here’s an example command to create a bridge network named my-bridge-network
:
docker network create --driver bridge my-bridge-network
And another example with additional options:
docker network create \
--driver overlay \
--subnet=10.0.9.0/24 \
--gateway=10.0.9.1 \
--attachable \
my-overlay-network
docker network create — subnet:
This command allows you to specify a custom subnet for the network. By default, Docker creates networks with the subnet 172.18.0.0/16.
docker network create --subnet=192.168.5.0/24 mynetwork
docker network create — ip-range:
Specifies a custom IP range for the network. By default, Docker assigns IP addresses from the subnet to containers automatically.
docker network create --subnet=192.168.5.0/24 --ip-range=192.168.5.0/25 mynetwork
docker network create — gateway:
Sets a custom gateway for the network. The gateway is the IP address of the bridge interface on the host within the network’s subnet.
docker network create --subnet=192.168.5.0/24 --gateway=192.168.5.1 mynetwork
docker network create — opt com.docker.network.bridge.name:
Specifies a custom bridge name for the network. This is useful when you want to control the naming of the bridge interface on the host.
docker network create --driver bridge --opt com.docker.network.bridge.name=my-bridge mynetwork
docker network create — internal:
Creates an internal network where containers can communicate with each other but not with the external network or the host.
docker network create --internal myinternalnetwork
docker network create — attachable:
Creates an attachable network that can be connected to running containers. By default, networks are not attachable.
docker network create --attachable myattachablenetwork
docker network create — label:
Adds metadata to the network in the form of key-value pairs. Labels can be used for filtering and organizing networks.
docker network create --label environment=production --label team=backend mynetwork
docker network connect — alias:
Specifies an alias for a container’s network interface on a specific network. This can be useful for DNS resolution within the Docker network.
docker network connect --alias db mynetwork mycontainer
2. Connects a container to a network:
docker network connect [OPTIONS] NETWORK CONTAINER
#OPTIONS are additional options you can use.
#NETWORK is the name or ID of the network.
#CONTAINER is the name or ID of the container you want to connect to the network.
Here are some commonly used options:
IP Options:
--ip
: Specify the IPv4 address for the container on the network.--ipv6
: Specify the IPv6 address for the container on the network.
Alias Options:
--alias
: Add network-scoped alias for the container.
Here’s an example command to connect a container named my-container
to a network named my-network
docker network connect my-network my-container
If you want to specify the IP address for the container on the network, you can do it like this:
docker network connect --ip 172.18.0.22 my-network my-container
And if you want to add an alias for the container on the network, you can do it like this:
docker network connect --alias my-alias my-network my-container
3. Disconnects a container from a network:
docker network disconnect [OPTIONS] NETWORK CONTAINER
#OPTIONS are additional options you can use.
#NETWORK is the name or ID of the network.
#CONTAINER is the name or ID of the container you want to disconnect from the network.
Here are some commonly used options:
- Force Option:
--force
: Force the disconnection even if the container is still running.
Here’s an example command to disconnect a container named my-container
from a network named my-network
:
docker network disconnect my-network my-container
If you want to force the disconnection, you can add the --force
option:
docker network disconnect --force my-network my-container
4. Docker network inspect:
Provides detailed information about a network, including its configuration and connected containers.
docker network inspect [OPTIONS] NETWORK [NETWORK...]
Here are some commonly used options:
- Format Option:
--format
: Pretty-print the information using a Go template.
Here’s an example command to inspect a network named my-network
:
docker network inspect my-network
#You can also use the --format option to customize the output format. For example:
docker network inspect --format='{{json .Containers}}' my-network
5. Lists all networks created on the Docker host:
docker network ls [OPTIONS]
6. Removes one or more networks from the Docker host:
docker network rm NETWORK [NETWORK...]
Removes all unused networks:
docker network prune [OPTIONS]
7. Creates an overlay network, which spans multiple Docker hosts, enabling communication between containers on different hosts.
docker network create --driver overlay
8. Creates a bridge network, which is the default network for Docker. Containers on a bridge network can communicate with each other if they are connected to the same network.
docker network create --driver bridge
9. Creates a MACvlan network, which assigns a unique MAC address to each container, making them appear as separate physical devices on the network.
docker network create --driver macvlan
Overview:
We will create a multi-tier web application consisting of a frontend web server, a backend API server, and a database server. Each component will run in its own Docker container, and we’ll use Docker networks to connect them together.
Components:
- Frontend Web Server (Nginx)
- Backend API Server (Node.js)
- Database Server (MySQL)
Steps:
- Setup the Project Structure: Create a project directory and navigate into it.
mkdir multi-tier-web-app
cd multi-tier-web-app
2. Create Dockerfiles: Create Dockerfiles for each component in their respective directories.
frontend/Dockerfile:
FROM nginx:latest
COPY nginx.conf /etc/nginx/nginx.conf
COPY dist /usr/share/nginx/html
backend/Dockerfile:
FROM node:latest
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD ["npm", "start"]
database/Dockerfile:
FROM mysql:latest
ENV MYSQL_ROOT_PASSWORD=root
ENV MYSQL_DATABASE=mydatabase
ENV MYSQL_USER=myuser
ENV MYSQL_PASSWORD=mypassword
3. Build Docker Images: Build Docker images for each component.
docker build -t frontend ./frontend
docker build -t backend ./backend
docker build -t database ./database
4. Create Docker Network: Create a Docker bridge network to connect the containers.
docker network create mynetwork
5. Run Containers: Run the containers and connect them to the created network.
Frontend:
docker run -d --name frontend --network mynetwork frontend
Backend
docker run -d --name backend --network mynetwork backend
Database:
docker run -d --name database --network mynetwork database
Access the Application:
- Once the containers are running, you can access the frontend web server at
http://localhost
. - The backend API server is available at
http://localhost:3000
. - The database server can be accessed using the configured credentials.
Notes:
We manually create and manage Docker networks using the
docker network
commands.Each container is connected to the same network, enabling communication between them.
This approach provides more granular control but requires additional setup compared to Docker Compose.
Feel free to customize and enhance this project as needed!
If there’s a specific topic you’re curious about, feel free to drop a personal note or comment. I’m here to help you explore whatever interests you!
Git Hub : github.com/nidhi-ashtikar