How to Install Docker on CentOS
Docker is a powerful tool for building, deploying, and running applications within containers, providing an isolated environment that simplifies development and deployment. Installing Docker on CentOS allows you to take advantage of these capabilities. This guide will walk you through the process of installing Docker on a CentOS server.
1. Update the System
Before installing Docker, it’s a good idea to update your system to ensure all packages are current:
sudo yum update -y
2. Install Required Packages
Docker requires some dependencies to be installed first. Run the following command to add them:
sudo yum install -y yum-utils device-mapper-persistent-data lvm2
The yum-utils package provides the yum-config-manager utility, which we’ll use to set up the Docker repository.
3. Set Up the Docker Repository
Next, add the official Docker repository to your system:
sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
This repository provides the latest Docker packages for CentOS.
4. Install Docker
With the repository added, you can now install Docker:
sudo yum install -y docker-ce
This installs the Docker engine on your CentOS system.
5. Start and Enable Docker
Once Docker is installed, start the Docker service and enable it to run on startup:
sudo systemctl start docker
sudo systemctl enable docker
To check the status of the Docker service, you can run:
sudo systemctl status docker
6. Verify Docker Installation
To verify that Docker is working correctly, run the following command:
sudo docker run hello-world
This command downloads a test image from Docker’s repository and runs it. If everything is set up correctly, you’ll see a message that Docker is installed and working.
7. Managing Docker as a Non-Root User (Optional)
By default, Docker requires root privileges. To allow a non-root user to run Docker commands, add them to the docker group.
Step 1: Create the Docker Group
sudo groupadd docker
Step 2: Add Your User to the Docker Group
Replace your_username with the actual username you want to give Docker access to:
sudo usermod -aG docker your_username
After adding the user, log out and log back in for the changes to take effect.
8. Basic Docker Commands
Here are some essential Docker commands to get started:
- List Docker Containers:docker ps # Shows running containers docker ps -a # Shows all containers, including stopped ones
- Start and Stop Containers:
docker start container_id
docker stop container_id
- Remove Containers:
docker rm container_id
- List Docker Images:
docker images
- Pull a Docker Image:
docker pull image_name
9. Configuring Docker to Start on Boot
Docker should already be configured to start on boot. However, if you need to re-enable it:
sudo systemctl enable docker
10. Additional Docker Configuration (Optional)
- Setting Up Docker Compose: Docker Compose allows you to define and run multi-container applications. Install Docker Compose with:
sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose sudo chmod +x /usr/local/bin/docker-compose
- Configuring Docker Daemon: Customize Docker by editing the /etc/docker/daemon.json file. You can configure options like logging, storage drivers, and network settings.
Conclusion
Installing Docker on CentOS provides a flexible environment for containerized applications, simplifying development and deployment processes. With Docker up and running, you can now start exploring containerization for your projects on CentOS.