Introduction
In this lab, you will learn how to set up and configure a CentOS 7 environment. While a traditional approach might use a full virtual machine in VirtualBox, this lab utilizes a more modern and efficient method by employing Docker containers. You will work within a cloud-based environment to deploy a lightweight, isolated CentOS 7 container, providing a functional Linux system without the overhead of a traditional VM.
The process begins with pulling the official CentOS 7 image from the Docker Hub registry. You will then create and configure a new container based on this image. Finally, you will customize the running system by installing the GNOME desktop environment and enabling a graphical login, resulting in a fully operational CentOS 7 instance with a graphical user interface.
Download and Install VirtualBox and the CentOS 7 ISO
In this step, we will prepare our environment by obtaining a CentOS 7 system. The original exercise uses VirtualBox to install a full virtual machine. However, in this cloud-based lab environment, a more efficient and modern approach is to use Docker containers. Docker allows us to run isolated applications and operating systems in lightweight environments called containers, which start much faster and use fewer resources than traditional virtual machines.
We will begin by downloading (or "pulling," in Docker terminology) the official CentOS 7 image from Docker Hub, which is a public registry of container images.
First, let's pull the CentOS 7 image. The command docker pull fetches an image from a registry. We will specify centos as the image name and 7 as the tag, which corresponds to the CentOS 7 version.
Execute the following command in your terminal:
docker pull centos:7
You will see output showing the download progress. Docker pulls the image in layers. Once it's complete, the output will look similar to this:
7: Pulling from library/centos
a1d0c7532777: Pull complete
Digest: sha256:0f4ec88e21daf75124b8a9e5ca03c37a5e937e0e108a255d890492430789b60e
Status: Downloaded newer image for centos:7
docker.io/library/centos:7
Now that the image is downloaded, let's verify that it is available on our local system. We can list all the Docker images we have locally using the docker images command.
docker images
The output should include an entry for centos with the tag 7. The IMAGE ID and CREATED date may differ on your system.
REPOSITORY TAG IMAGE ID CREATED SIZE
centos 7 eeb6ee3f44bd 2 years ago 204MB
With the CentOS 7 image successfully downloaded, we are ready to create and run a container from it in the next step.
Create and Configure a New Virtual Machine for CentOS 7
In this step, we will create and configure our CentOS 7 environment. Instead of a traditional virtual machine, we will use the centos:7 Docker image we downloaded to launch a container. A container is a running instance of an image. This will serve as our "virtual machine" for the rest of this lab.
We will use the docker run command to create and start the container. We'll include a few important options:
-it: This combination of flags connects our terminal to the container's terminal, allowing us to run commands interactively.--name centos_workstation: This assigns a memorable name,centos_workstation, to our container. This makes it easy to manage (start, stop, remove) later.centos:7: This specifies the image we want to run./bin/bash: This is the command that will be executed inside the container when it starts. It will launch the Bash shell, giving us a command prompt.
Now, execute the following command to start your CentOS container:
docker run -it --name centos_workstation centos:7 /bin/bash
After running the command, you'll notice your terminal prompt changes. It will look something like this:
[root@a1b2c3d4e5f6 /]#
This new prompt indicates that you are now inside the CentOS 7 container as the root user. The string of letters and numbers (e.g., a1b2c3d4e5f6) is the unique ID of your container. You are no longer in the LabEx host environment's shell.
Following the original exercise's plan, let's create a non-root user. We will create a user named student1. Use the useradd command for this:
useradd student1
Next, set a password for student1. We will use student1 as the password to keep things simple. The passwd command will prompt you to enter and re-enter the new password.
passwd student1
When prompted, type student1, press Enter, then type student1 again and press Enter.
Changing password for user student1.
New password:
Retype new password:
passwd: all authentication tokens updated successfully.
To complete this step, we need to return to our main LabEx terminal. Type exit and press Enter to leave the container's shell.
exit
This stops the container and returns you to the ~/project directory prompt. The container centos_workstation now exists and can be started again in future steps.
Perform the CentOS 7 Installation with Custom Partitions
In this step, we will configure our new CentOS 7 environment. The original exercise involves a detailed installation process with custom disk partitioning. In our Docker-based environment, this process is different. The "installation" is already done because we are using a pre-built image. The concept of creating manual partitions like /, /home, and /var does not apply to a standard Docker container, as it uses the host's filesystem in a layered manner.
Instead, our focus will be on the initial configuration of the running container, which is a common task after deploying a new system. We will start the container, update its software packages, and install sudo to allow our non-root user to perform administrative tasks.
First, let's start the container we created in the previous step. It is currently stopped. Use the docker start command:
docker start centos_workstation
This command will output the name of the container, centos_workstation, confirming it has started. Now, attach your terminal to the running container using docker attach:
docker attach centos_workstation
Your terminal prompt will change to the container's root shell, similar to before:
[root@a1b2c3d4e5f6 /]#
Now configure with the necessary services and create the problems we'll troubleshoot. First, we need to fix the CentOS 7 repository configuration since it has reached end-of-life:
sed -i 's/mirrorlist/#mirrorlist/g' /etc/yum.repos.d/CentOS-Base.repo
sed -i 's/#baseurl/baseurl/g' /etc/yum.repos.d/CentOS-Base.repo
sed -i 's|http://mirror.centos.org|http://vault.centos.org|g' /etc/yum.repos.d/CentOS-Base.repo
yum clean all
The minimal CentOS 7 image is very basic. Let's start by updating all the installed packages to their latest versions using yum, the package manager for CentOS. The -y flag automatically answers "yes" to any confirmation prompts.
yum update -y
This process may take a few minutes to complete. Once it's done, we'll install the sudo package, which is not included in the minimal image. sudo allows permitted users to execute commands as another user, typically the root user.
yum install -y sudo
After the installation is complete, we need to grant sudo privileges to the student1 user we created earlier. In CentOS, this is typically done by adding the user to the wheel group.
usermod -aG wheel student1
usermodis a command to modify a user account.- The
-aGflags tell the command to append the user to a supplementary Group, which in this case iswheel.
To verify that student1 now has sudo access, let's switch to that user and test it.
su - student1
Your prompt will change to [student1@a1b2c3d4e5f6 ~]$. Now, try running the whoami command with sudo. It should report that you are root.
sudo whoami
The expected output is:
root
Great! Our container is now configured with an updated system and a user with sudo rights. To continue to the next step, we need to exit the container but leave it running in the background. Do not type exit. Instead, press the key sequence Ctrl+P followed by Ctrl+Q. This will detach your terminal from the container, returning you to the LabEx prompt, while the container continues to run.
Install the GNOME Desktop and Enable Graphical Login
In this step, we will adapt the original exercise's goal of installing a graphical desktop. In a container environment like the one we're using, installing a full graphical user interface (GUI) such as GNOME is not a standard practice. Containers are designed to be lightweight and are typically managed from the command line.
Instead, we will practice the same skill—installing groups of related software packages—by installing a common set of command-line tools. We will also install a text-based web browser to fulfill the original goal of having browser access in our new environment.
First, we need to enter our running container. In the previous step, we left the centos_workstation container running in the background. We will use the docker exec command to open a new shell inside it as the student1 user.
If you are in the container, you can skip this command.
docker exec -it centos_workstation su - student1
Your command prompt will change to [student1@a1b2c3d4e5f6 ~]$, indicating you are now logged in as student1 inside the container.
Now, let's use yum to install the "Development Tools" package group. This group contains essential tools for compiling software from source, such as gcc, make, and git. Since student1 has sudo privileges, we can perform this installation. The -y flag automatically answers "yes" to any prompts.
sudo yum groupinstall -y "Development Tools"
This installation will download and install many packages and may take several minutes to complete. You will see a lot of output as the packages are installed.
Next, to complete our setup, let's install lynx, a lightweight, text-based web browser. This is a useful tool for quickly checking websites or downloading files directly from the command line.
sudo yum install -y lynx
The installation for lynx should be very quick. Let's test our new browser by navigating to a simple website.
lynx example.com
You will see the text content of the example.com homepage rendered in your terminal. You can use the arrow keys to navigate. To exit lynx, press the q key on your keyboard. When prompted to confirm (Are you sure you want to quit? [Y/N]), press y.
You have now successfully configured your CentOS environment with important development tools and a command-line browser. To finish this lab, exit the container.
Type exit to log out from the student1 user, and then type exit again to leave the container's shell and return to your main LabEx terminal.
exit
exit
Summary
In this lab, you learned how to set up a CentOS 7 environment using a modern, container-based approach with Docker. You began by pulling the official CentOS 7 image from the Docker Hub public registry using the docker pull centos:7 command. You then verified that the image was successfully downloaded and available on your local system by listing all local Docker images with the docker images command.
This initial setup prepares the foundation for the subsequent steps, where you will create and run a container from this image. The overall objective is to perform a complete CentOS 7 installation, including custom configurations and the installation of the GNOME graphical desktop environment, all within the lightweight and efficient Docker container.



