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:
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:
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:
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:
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:
Then, check whether the container is running in privileged mode or not by utilizing the provided command along with container id or container name:
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.