Docker

–Privileged In Docker Compose With Code Examples

Docker compose is a multi-container managing tool frequently used to configure the services for multi-container projects and applications. These containers are mostly configured through the “docker-compose.yml” file. Docker containers can be executed in privileged mode through the “–privileged” option in the “docker run” command. However, in Docker compose, the “–privileged” option is not used. Users can run the compose container in privileged mode using the “privileged” key.

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:

version:"alpine"
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:

> docker-compose up -d

Step 3: Inspect Container

To verify if the container is executing with host privileges or not, check out the provided command:

> docker inspect --format='{{.HostConfig.Privileged}}' web-container

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.

About the author

Rafia Zafar

I am graduated in computer science. I am a junior technical author here and passionate about Programming and learning new technologies. I have worked in JAVA, HTML 5, CSS3, Bootstrap, and PHP.