Using Docker network host commands
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
- FROM - sets the base image on which we will create our custom image using the subsequent instructions.
- 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.
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.
Related Articles
Get the IP address of the Docker container from the host using docker inspect
Publish Date:2025/03/26 Views:103 Category:Docker
-
Docker containers are not just for isolation—they are often used to manage processes that still need to communicate directly with each other. However, to communicate, you usually need to know the IP address of each container, which you ca
Solution to incorrect access log time when deploying Nginx in Docker
Publish Date:2025/03/26 Views:165 Category:Docker
-
In the process of operating the website, I never took the logs too seriously. Although logging was turned on, I never analyzed the logs carefully. Today, when I looked at the logs on a whim, I found that the recorded time was 8 hours less t
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