Docker

How to Run Containers in Privileged Mode

Docker containers are the primary part of the Docker environment, allowing developers to construct and deploy programs in a virtualized run-time environment. These containers can be operated in privileged mode. More specifically, a privileged mode is a powerful feature of the Docker platform that allows developers to run containers with root capabilities and allow containers to access full host privileges.

This write-up will teach you how to run containers in privileged mode.

How to Run a Privileged Docker Container?

When the users execute the Docker container in privileged mode, it has complete root access and bypasses any checks.

Follow the provided steps to start the Docker containers in privileged mode to grant host privileges.

Step 1: Make Dockerfile

First, launch the Visual Studio code editor, make a Dockerfile and paste the provided code into the file:

FROM golang:1.8 AS builder

WORKDIR /go/src/app

COPY main.go .

RUN go build -o webserver .

CMD ["./webserver"]

Step 2: Create Program File

Create a “main.go” file to specify a simple Golang program. For this purpose, paste the given code in the file:

Package main

import (
"fmt"
"log"
"net/http"
)

funchandler (w http.ResponseWriter, r *http.Request) {
        fmt.Fprintf(w, "Hello! Welcome to LinuxHint Tutorial")
}
funcmain () {
        http.HandleFunc("/", handler)
        log.Fatal(http.ListenAndServe("0.0.0.0:8080", nil))
}

Step 3: Build Docker Image

Then, use the provided command to create the new Docker image from Dockerfile. Here, the “-t” flag is used to specify the Docker image name:

$ docker build -t golang:latest .

Step 4: Run Docker Container in Privileged Mode

Next, execute the container in privileged mode by utilizing the provided command along with the “–privileged” flag. Here, the “-p” is used to specify the port number, and the “-d” option is used to run the container in the background:

$ docker run --privileged -d -p 8080:8080 golang

Then, move to the browser and visit the “localhost:8080” URL to check if the container is running or not:

Step 5: Check Container is Running in Privileged Mode

To check if the container is running in privileged mode or not, first list down all Docker containers with the help of the “docker ps” command:

$ docker ps -a

Then, check whether the container is running in privileged mode or not by utilizing the provided command along with container id or container name:

$ docker inspect --format='{{.HostConfig.Privileged}}' b46571b87efd

The displayed “true” value indicates that the container is in privileged mode:

We have taught you how to run Docker containers in privileged mode.

Conclusion

To run the Docker container in privileged mode, first, create a Docker image with the help of Dockerfile. Then, running the image in privileged mode to operate the container will host privileges. For this purpose, utilize the “docker run –privileged” command. This write-up has explained how to process the Docker container in privileged mode.

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.