Docker compose is one of the fundamental parts of the Docker solution that is mainly utilized to configure the services for multiple container applications. These Docker containers are hosted and managed on different networks, such as bridge networks, host networks, or user-created networks where these containers share their network namespace.
This write-up will describe how to utilize the host network for Docker compose.
How to Use Host Network For docker-compose?
In Docker, the “–net=<“network-mode”>” option is utilized to specify the network mode for a container in the “docker run” command. However, in docker-compose, users must configure the host network in the “docker-compose.yml” file by utilizing the “network_mode” key.
To use the host network in Docker compose, look at the listed steps.
Step 1: Generate Program File
First, create a simple HTML program file “index.html”, and paste the provided code into the file:
<h1>Hello! Welcome to Linuxhint Tutorial</h1>
Step 2: Create Dockerfile
Create the “Dockerfile” and copy the below-coded instructions into the file. Here, these instructions contain the following details:
- “FROM” specifies the base image for the container.
- “COPY” is copying the source file to the container path.
- “ENTRYPOINT” set the execution point or defaults for containers:
COPY index.html /usr/share/nginx/html/index.html
ENTRYPOINT ["nginx", "-g", "daemon off;"]
Step 3: Configure Service in Docker Compose
Next, create another file “docker-compose.yml”, that will configure the services to run the HTML program file on the host network. After that, copy the following configurations into the “docker-compose.yml” file:
These instructions includes:
- “services” key used to configure the service. For instance, we have configured the “web” service.
- “build” key specifies that the “web” service will utilize the Dockerfile instructions.
- “container_name” defines the name of the container in which the web service will manage and execute.
- “network_mode” key is specifically used to allocate the network to the container. To use the host network for the container, set the value of “network_mode” as “host”:
Step 4: Create and Start Container
Next, execute the “docker-compose up” command to create and run the “web” service in the Docker container. The “-d” option is used to run the container as a backend service or in the background:
Step 5: List Compose Containers
List down all containers and verify if the container is created and started or not:
Step 6: Verification
Verify if the container is executed on the host or not, inspect the container through “docker inspect <cont-name>” command:
Here, you can see our container is running on the “host” network:
Alternatively, for verification, you can visit the “localhost” and check if the service is running on the host or not:
The above output shows that we have successfully used the host network for Docker compose.
To use the host network for Docker compose, you can configure the host network in the “docker-compose.yml” file. For this purpose, first, create the “docker-compose.yml” file, configure your web service, and set the value of the “network_mode” key as “host”. This write-up has demonstrated how to utilize the host network for Docker compose.