JIYIK CN >

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

Using Docker network host commands

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

Docker containers work by leveraging network drivers that are created during the installation of Docker. The default drivers available to us include bridge and host networking.

When we create containers without specifying a network, they are added to the bridge network. If we want to add a network to the host network or a custom network, we use the --network command.

In this article, we will learn how to add a container to the host network using the --network command. We will also learn how to add a container to the default network if we do not specify a network using this command.


Create Nginx project

Open WebStorm IDEA and select File > New > Project . In the window that opens, select Empty Project and change the project name from untitled to docker-network-host or any preferred name.

Finally, press the button labeled Create to generate the project.

Create a file called index.html in the current folder and copy and paste the following code into the file.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>Welcome to docker networking !</h1>
</body>
</html>

In this file, we have created a simple web page that will display a title when we visit the application running in the container. This will help verify that our container is running the Nginx application.


Defining an image

Create a file called Dockerfile and copy and paste the following instructions into it. Note that we typically use files with this name to create custom images from existing images.

FROM nginx:alpine
ADD . /usr/share/nginx/html
  1. FROM - sets the base image on which we will create our custom image using the subsequent instructions.
  2. ADD - Copies files and folders in the current folder to the image file system destination /usr/share/nginx/html.

Building an Image

Build our custom image using the Dockerfile defined above. Open a new terminal window using the keyboard shortcut ALT+F12 and build the image using this command.

~/WebstormProjects/docker-network-host$ docker build --tag docker-network-host:latest .

This command executes the Dockerfile in sequence to create a tagged image. We can view each running instruction in the terminal window as follows.

=> [1/2] FROM docker.io/library/nginx:alpine@sha256:082f8c10bd47b6acc8ef15ae61ae45dd8fde0e9f389a8b5cb23c37408642bf5d               0.1s
 => => resolve docker.io/library/nginx:alpine@sha256:082f8c10bd47b6acc8ef15ae61ae45dd8fde0e9f389a8b5cb23c37408642bf5d               0.1s
 => CACHED [2/2] ADD . /usr/share/nginx/html

Run the container in the default network driver

To run our application's container in the default network, execute the following command in a terminal window. Note that this container will be added to the bridge network.

~/WebstormProjects/docker-network-host$ docker run --name docker-network-bridge-test -d -p 8000:80 docker-network-host:latest

This command runs a container named docker-network-bridge-test that listens on port 80 in the container and exposes port 8000 on the host. To verify that this container has been added to the bridge network, execute the following command to check the bridge network.

~/WebstormProjects/docker-network-host$ docker network inspect bridge

Output:

[
    {
        "Name": "bridge",
        "Id": "45773c7633cf28baa742ceca9c054a8dd6b4ea609dd9944d7ae12bdb57e86bcd",
        "Created": "2022-10-06T07:53:45.935633743Z",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": null,
            "Config": [
                {
                    "Subnet": "172.17.0.0/16",
                    "Gateway": "172.17.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "5e452da9b047ce94ff23b4841d1cb3358f34b037ef566c2f12a9eb57012a5f85": {
                "Name": "docker-network-bridge-test",
                "EndpointID": "d3491eb0c518d91be71b170e8ca0a077e07781bffbe20f3e1a1fd415eef9c288",
                "MacAddress": "02:42:ac:11:00:02",
                "IPv4Address": "172.17.0.2/16",
                "IPv6Address": ""
            }
        },
        "Options": {
            "com.docker.network.bridge.default_bridge": "true",
            "com.docker.network.bridge.enable_icc": "true",
            "com.docker.network.bridge.enable_ip_masquerade": "true",
            "com.docker.network.bridge.host_binding_ipv4": "0.0.0.0",
            "com.docker.network.bridge.name": "docker0",
            "com.docker.network.driver.mtu": "1500"
        },
        "Labels": {}
    }
]

In the returned JSON, the Containers property has a container named docker-network-bridge-test with a MacAddress and an IPv4Address assigned. This is the same container that was created at the beginning of this section.

To serve this application on a browser, make a request to localhost:8000 (http://localhost:8000/) and make sure it returns a header containing the text Welcome to docker networking !.


Running containers in the host network

The term host network means that the container runs on our host's network, as the host network cannot containerize the container network. To run a container in the host network, execute the following command in the terminal window.

~/WebstormProjects/docker-network-host$ docker run --rm -d --network host --name docker-network-host-test docker-network-host:latest

This command runs a container named docker-network-host-test in the host network using the --network option with a value of host . To verify that this container was added to the host network, execute the following command in a terminal window.

~/WebstormProjects/docker-network-host$ docker network inspect host

Output:

[
    {
        "Name": "host",
        "Id": "4ce9d3806cd88f9d9ea446272f4338f7b1f5e7098d4d0bc2a11090c1759d1880",
        "Created": "2022-09-19T07:51:50.247290701Z",
        "Scope": "local",
        "Driver": "host",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": null,
            "Config": []
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "22d15c7eee861ec7fdfd22936c30dfe1d17b2dde52ada93256f3e30943a3ed80": {
                "Name": "docker-network-host-test",
                "EndpointID": "7bd01907c8b86a881b86ae9e9c8ad8c1d3aa9f0583adebb7d41ae15bd565fabe",
                "MacAddress": "",
                "IPv4Address": "",
                "IPv6Address": ""
            }
        },
        "Options": {},
        "Labels": {}
    }
]

In the returned JSON, the Containers property has a container called docker-network-host-test, note that in this case the container has no MacAddress or IPv4Adress assigned because it is running on the host network. This is the same container that was created at the beginning of this section.

To serve this application on a browser, make a request to localhost:80 (http://localhost:80/) and make sure it returns a header containing the text Welcome to docker networking!. However, make sure that port 80 on the host network is open to accept requests.


Summarize

In this article, we learned how to run containers on the default bridge network driver and the host network driver. Note that we used the --network command to add containers on a network other than the default network.

To ensure that your application runs on the new network, make sure that the port you are using is open for connections and that another process is not using that port.

Previous:Clear Docker container logs

Next: None

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

Clear Docker container logs

Publish Date:2025/03/24 Views:192 Category:Docker

Logs are information recorded by the application when a specific event or state occurs. They help us monitor the application and take necessary actions. For example, when we deploy an application to a production environment, logs can help u

Docker daemon log location

Publish Date:2025/03/24 Views:65 Category:Docker

The Docker daemon provides essential information about the general state of your microservices architecture. Unfortunately, container-centric logging techniques allow you to collect relevant data from your services but provide little inform

Difference between COPY and ADD commands in Dockerfile

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

A Dockerfile is a text document that contains all the commands used to build a Docker image. Recently, we have seen Docker being widely used as the default tool for managing configurations and automating deployments. Advanced features such

The --rm flag in Docker

Publish Date:2025/03/24 Views:151 Category:Docker

Typically, when most developers start using docker, after going through all the processes of pulling images, building them, and running containers, removing a container defeats the purpose of doing so. However, for experienced developers, t

Setting environment variables in Docker

Publish Date:2025/03/24 Views:188 Category:Docker

Environment variables are used to add additional configuration or metadata to aid in the development of an application, and can exist in different forms. For example, when developing a Java application, we usually set an environment variabl

在 Linux 中托管 Docker Internal

Publish Date:2023/04/18 Views:221 Category:Docker

Docker 允许开发人员通过将应用程序包装在称为容器的标准化单元中来高效地构建、测试和部署应用程序。 在使用 Docker 容器时,您可能会遇到需要将容器与主机连接的场景。

在 Docker 中设置工作目录

Publish Date:2023/04/18 Views:234 Category:Docker

在 Docker 中,我们可以通过编辑 Dockerfile 并添加密钥 WORKDIR 来设置我们的工作目录。本文将讨论在 Docker 中更改我们当前和默认的工作目录。

在 Docker 容器中公开多个端口

Publish Date:2023/04/18 Views:466 Category:Docker

Docker 容器使用端口来实现万维网上不同设备之间的通信。 在本篇文章中,我们将学习如何使用 Nginx 应用程序在 Docker 容器中公开多个端口。

将用户添加到 Docker 容器

Publish Date:2023/04/18 Views:206 Category:Docker

在本文中,我们将学习如何通过实现返回产品数组的 Express 应用程序将用户添加到 Docker 容器。

Scan to Read All Tech Tutorials

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

Recommended

Tags

Scan the Code
Easier Access Tutorial