How To Install and Use Docker on CentOS 7
Enjoying this content? Subscribe to the Channel!
Containerize Your Work: The Ultimate Guide to Installing and Using Docker on CentOS 7
Welcome back to Darren’s Tech Tutorials!
Docker has revolutionized the way we develop, deploy, and manage applications. By using containers, you can ensure your software runs exactly the same way, everywhere, regardless of the underlying operating system. If you’re running CentOS 7 and ready to unlock the power of containerization, this is the guide for you!
In this comprehensive tutorial, we will walk you through the essential steps to install Docker on your CentOS 7 system and run your very first container—an instance of Ubuntu!
Prerequisites
Before we dive into the Docker installation, you must have a functioning CentOS 7 environment.
If you haven’t yet installed CentOS 7, you can follow along with our installation guide here:
Once your system is up and running, let’s get started!
Step 1: Installing Docker on CentOS 7
The installation process is straightforward using the standard yum package manager.
1. Install the Docker Package
Open your terminal and use the following command to download and install the latest stable version of Docker:
sudo yum install docker
2. Start the Docker Service
After installation completes, the Docker service needs to be initiated.
sudo systemctl start docker
3. Verify the Service Status
It’s always a good idea to check that the service started successfully. The output should show the service as ‘active (running)’.
sudo systemctl status docker
4. Enable Docker to Start at Boot
To ensure Docker automatically restarts every time your CentOS 7 machine reboots, we need to enable the service:
sudo systemctl enable docker
Step 2: Verifying the Docker Installation
Now that Docker is installed and running, let’s quickly confirm that everything is working correctly and see which version we are running.
1. View Docker System Information
This command provides a wealth of information about your Docker installation, including the number of images and containers currently running, storage driver details, and operating system information.
sudo docker info
2. Check the Docker Version
To confirm the client and server versions you have installed, run:
sudo docker version
If both commands execute without error, congratulations—Docker is successfully installed!
Step 3: Finding and Pulling Your First Container
Docker works by using pre-built images, which are essentially templates for containers. To demonstrate the magic of containers, we are going to use an image of Ubuntu, a completely different Linux distribution, running right inside your CentOS host!
1. Search for the Ubuntu Image
We can search the Docker Hub registry (the official repository for Docker images) to find the official Ubuntu image:
sudo docker search ubuntu
This will display a list of images matching the term “ubuntu.” Look for the official, highly-rated image.
2. Download the Ubuntu Image (Pulling)
To download the official Ubuntu image to your local machine, use the docker pull command. This image is now locally stored and ready to be used instantly.
sudo docker pull ubuntu
Step 4: Running Your First Docker Container
With the Ubuntu image downloaded, we can now demonstrate two powerful ways to run a container: executing a single non-interactive command, and starting a fully interactive shell session.
1. Running a Non-Interactive Command
This is a great test run. We will use the Ubuntu container to execute one simple command (cat /etc/issue) and then the container will automatically stop. This command asks the Ubuntu container to print its OS version information.
sudo docker run ubuntu cat /etc/issue
You should see output confirming that the container is, in fact, running Ubuntu, even though your host OS is CentOS 7!
2. Running an Interactive Shell Session
To truly step inside the container, we need to start an interactive session. We achieve this using the -it flags:
-i: Keeps STDIN open, allowing you to pass commands to the container.-t: Allocates a pseudo-TTY, giving you an actual terminal interface.
Run this command to drop into a bash shell inside your Ubuntu container:
sudo docker run -it ubuntu bash
Your command prompt will change, indicating that you are now operating inside the Ubuntu container. You can now run Ubuntu-specific commands (like apt update or ls -l /) without affecting your CentOS host machine!
To exit the container and return to your CentOS terminal, simply type exit.
Summary and Next Steps
You’ve done it! You successfully installed Docker on CentOS 7, verified the service, pulled an image, and launched both non-interactive and interactive Ubuntu containers.
This is just the beginning of your containerization journey. Docker allows you to isolate environments, test applications safely, and streamline deployments like never before.
If you found this guide helpful, please be sure to like this blog post and subscribe to Darren’s Tech Tutorials on YouTube for more clear, accessible technology guides.
Happy containerizing!