This blog will demonstrate how to use the “–privileged” in Docker compose to execute compose containers in privileged mode.
How to Use “–Privileged” in Docker Compose?
A powerful feature of the Docker platform called privileged mode that enables programmers to run containers with root credentials and gives them access to all host privileges. However, there is no “–privileged” option available for the “docker-compose” command. You can use the “privileged” key in the compose file.
For a better understanding, look at the listed steps.
Step 1: Generate “docker-compose.yml” File
First, make a “docker-compose.yml” file and paste the following instructions into the file:
services:
web:
build: .
container_name: web-container
privileged: true
ports:
- "8080:8080"
golang:
image:"golang:alpine"
In the above code block:
- The two services are configured one is “web” and the other is “golang”.
- The “web” service reads the instructions from Dockerfile.
- “container_name” key is used to specify the container name that will execute the “web” service.
- “privileged” key is set as “true” to run the “web” service container with host privileges.
- “ports” specify the exposing port for the container.
- “image” key defines the base image for the “golang” service:
Step 2: Create and Start Container
Next, run the “docker-compose up” command to create and start the container. The “-d” flag executes the containers in detached mode or the background:
Step 3: Inspect Container
To verify if the container is executing with host privileges or not, check out the provided command:
The output “true” indicates that the “web-container” is executing in privileged mode:
To view the web service output, navigate to the specified localhost port. Here, you can see we have successfully started the web service in privileged mode:
We have elaborated on how to use “–privileged” in Docker compose with a code example.
Conclusion
The “–privileged” option is used in the “docker run” command to process the container in privileged mode. However, in Docker compose command, the “privileged” key is utilized to run the container and its service with host/root privileges. For this purpose, set the “privileged” key as “true” in the “docker-compose.yml” file. This write-up has demonstrated the method to run the compose container in privileged mode with a code example.