Are you looking to run Ubuntu 20.04 within a Docker container on your CentOS 7 host machine without granting the container root privileges? This article will guide you through the necessary steps and key concepts to achieve this. Let's dive in!
Why Run Ubuntu 20.04 in a Non-Privileged Docker Container on CentOS 7?
As the CentOS 7 Linux distribution ages, some users may want to run the latest software versions inside their stable production server. With Docker, it is possible to create lightweight and isolated containers running different operating systems on top of your CentOS 7 server without modifying the base system or requiring privileged container access.
Prerequisites
To follow this guide, you should have:
- A machine running CentOS 7 minimal installation with Docker installed.
- Basic understanding of Docker and command-line navigation.
Pulling the Ubuntu 20.04 Docker Base Image
You can pull the Ubuntu 20.04 base image using the following command:
$ docker pull ubuntu:20.04
Selecting a Non-Root User
For security reasons, avoid using the root user when running a Docker container. Let's create a regular user with a home directory inside the Ubuntu 20.04 Docker container.
$ docker run -it --entrypoint "/bin/bash" ubuntu:20.04 -c "useradd --create-home user && usermod --shell /bin/bash user"
Commiting Changes to a New Ubuntu 20.04 Non-Root User Docker Image
Having created a new non-root user in the container, you can now commit these changes and create a new Docker image based on Ubuntu 20.04.
$ docker commit ([container_id](https://docs.docker.com/engine/reference/commandline/ps/#view-container-ids)]) myubuntu:20.04
Running a Non-Privileged Docker Container on CentOS 7
To run a non-privileged Docker container on CentOS 7, you can start it using the newly created image.
$ docker run --user user:user -it --rm myubuntu:20.04 /bin/bash
This article walked you through running Ubuntu 20.04 within a non-privileged Docker container on CentOS 7 using the following steps:
- Pulling the Ubuntu 20.04 base image.
- Creating a new regular user inside the Docker container.
- Committing changes and creating a new Ubuntu 20.04 non-root user Docker image.
- Running a non-privileged Docker container using the new image.