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

Docker deploys nginx php application

Publish Date:2025/03/26 Views:131 Category:Docker

I'm learning docker recently. I'm learning by building an nginx+php development environment example. Here I record the build process. First, give a docker-compose.yml deployment configuration file version: '3' services: nginx: container_nam

How to use Docker to image a Node.js web application

Publish Date:2025/03/26 Views:107 Category:Docker

Docker is a containerization platform that simplifies the packaging and execution of applications. Containers run as independent processes with their own file systems, but share the kernel of their host machine. Docker has attracted much at

Start a Bash terminal in a new Docker container

Publish Date:2025/03/26 Views:97 Category:Docker

Docker containers are a standard unit for packaging all the dependencies of an application, allowing us to easily run them in any environment. Containers have become very popular recently, and most developers now rely heavily on containers

Passing environment variables to containers in Docker

Publish Date:2025/03/26 Views:125 Category:Docker

This article will introduce how to pass environment variables to containers in Docker. Passing environment variables to containers in Docker using the -e and tags -env We will first see how to create environment variables and pass them to t

Install Docker using Homebrew

Publish Date:2025/03/26 Views:202 Category:Docker

There is no doubt that Docker containers have revolutionized the way we develop and deploy applications. They provide developers with the ability to package applications and dependencies in an isolated environment. Recently, we've seen wide

Enforce clean build of images in Docker

Publish Date:2025/03/26 Views:88 Category:Docker

This article discusses and demonstrates how to enforce clean builds of images in Docker. Building images in Docker We will use a simple Flask application to demonstrate this concept. my-app Create a app.py simple application named in the ho

Running a Docker instance from a Dockerfile

Publish Date:2025/03/26 Views:140 Category:Docker

Docker containers have undoubtedly become the standard unit for managing software and dependencies in different environments. When using real applications, you must create a docker file before building the container image of the application

Scan to Read All Tech Tutorials

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

Recommended

Tags

Scan the Code
Easier Access Tutorial