JIYIK CN >

Current Location:Home > Learning > OPERATING SYSTEM > Docker >

Adding network mode in Docker Compose

Author:JIYIK Last Updated:2025/03/25 Views:

By default, a single network is created by Docker Compose in our application and each container is added there as a service. Every container on the network can be accessed and found by containers on the single network.

We can configure our network by defining network modes in Docker Compose. This article will discuss how to define and set up a network using network modes in Docker Compose.


Collecting network information in Docker

If we want to configure a network mode for a specific container or service, we need to gather additional information first. Running the command docker network lswill allow us to list the current Docker networks.

It should look similar to the output below.

NETWORK ID          NAME                         DRIVER
17cc61428fef        bridge                       bridge
098522f7fce0        sample_default               bridge
1ce3c472afc6        test_default                 bridge
8fd07e456e6c        host                         host
3b5787919641        none                         null

This command will come handy if we have to know the name or ID of our container, service or network through the previous command provided above.


Configuring network mode in Docker Compose

In Docker Compose version 3, we can use network mode in our YAML file by providing network_mode: parameter and its value. So, make sure you specify the Docker compose version we are using using version: parameter and value 3 or 3.0.

example:

version: "3"
services:
  app:
    network_mode: "host"

In the example above, we used the example host as our network mode, but we can specify containers or services instead. From the Docker official documentation, we can use the following multiple values ​​when defining the network mode of a service:

example:

network_mode: "bridge"
network_mode: "host"
network_mode: "none"
network_mode: "service:[service name]"
network_mode: "container:[container name/id]"

Since we gathered the information earlier, we can use the network name or ID when defining the network mode.

example:

network_mode: "container:sample_default"
network_mode: "container:1ce3c472afc6"

Configuring network mode in Docker Swarm

A collection of physical or virtual machines running Docker applications is called a Docker Swarm. The container orchestration solution called Docker Swarm allows users to control many containers distributed across different hosts.

Once a group of machines are clustered together, we can still run the Docker commands we are used to, but the machines in our cluster will now execute them.

Unfortunately, Docker Compose and Swarm do not play well together. Therefore, we need to define the network manually using the networks parameter instead of the network_mode parameter, and it should look like this.

example:

version: "3.0"
services:
    app:
        networks:
            - host

networks:
    host:
        name: [Your Network Mode Value]
        external: true

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Copy files from host to Docker container

Publish Date:2025/03/25 Views:126 Category:Docker

This article will discuss and demonstrate methods we can use to transfer files from the host to a running container in Docker. docker cp Copy the file from the host to the Docker container using docker cp The command is one of the simplest

Get the IP address of the Docker container

Publish Date:2025/03/25 Views:102 Category:Docker

This article demonstrates how to get the IP address of a Docker container. Connect to the Bridge network and get the IP address of the Docker container One of the big reasons why docker containers are so convenient is that we can easily con

Uninstalling Docker on macOS

Publish Date:2025/03/25 Views:95 Category:Docker

Recently, we have seen widespread adoption of Docker as the ultimate containerization platform. Because of this, setting up Docker on all platforms has been greatly simplified, including macOS and Windows. However, some users usually face p

Enter the Docker container's shell

Publish Date:2025/03/25 Views:98 Category:Docker

This article will demonstrate how to enter the Docker container shell using multiple methods. Use docker exec to enter the Docker container's shell We need to have a container up and running to use this command. We can check the status of t

Listing containers in Docker

Publish Date:2025/03/25 Views:122 Category:Docker

This article will discuss various commands for listing containers created in our system. This means we should create some containers beforehand for these commands to return values. List all running containers in Docker We will start by list

Mount the host directory into the Docker container

Publish Date:2025/03/25 Views:189 Category:Docker

Docker provides, among other features, tools to work with the host operating system and the container file system. One of these features is the ability to persist data in containers and share data between containers by mounting directories

Docker build command with multiple parameters

Publish Date:2025/03/25 Views:138 Category:Docker

docker build The command allows us to create a Docker image from a Dockerfile. It also allows us to build an image from a context that references a set of files located in a location specified by a PATH or URL. A Dockerfile, on the other ha

The difference between CMD and ENTRYPOINT in Docker

Publish Date:2025/03/25 Views:141 Category:Docker

Docker containers have become the standard when it comes to managing software and dependencies in different environments. When working with real-world applications, you need to create a docker file before building your application container

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial