Docker is a platform that uses containerization technology to enable the deployment, management, and scaling of applications in lightweight, portable containers. Containers bundle an application with all its dependencies, allowing it to run consistently across different environments. This guide will introduce you to Docker on Linux, focusing on Debian, Ubuntu, and Red Hat distributions.
Installing Docker
Docker can be easily installed on various Linux distributions. Below are the installation steps for Debian, Ubuntu, and Red Hat.
Installing Docker on Debian and Ubuntu
To install Docker on Debian or Ubuntu, follow these steps:
sudo apt update
sudo apt install apt-transport-https ca-certificates curl software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
echo "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt update
sudo apt install docker-ce
Installing Docker on Red Hat
To install Docker on Red Hat, follow these steps:
sudo yum install -y yum-utils
sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
sudo yum install docker-ce docker-ce-cli containerd.io
Starting and Enabling Docker
After installing Docker, you need to start the Docker service and enable it to start on boot. Use the following commands:
sudo systemctl start docker
sudo systemctl enable docker
Running Your First Docker Container
Once Docker is installed and running, you can run your first Docker container. Let’s start with a simple example using the hello-world image:
sudo docker run hello-world
This command downloads the hello-world image from Docker Hub and runs it in a container. If everything is set up correctly, you should see a “Hello from Docker!” message.
Managing Docker Containers
Docker provides various commands to manage containers. Here are some of the most commonly used commands:
Listing Containers
To list all running containers, use:
sudo docker ps
To list all containers, including stopped ones, use:
sudo docker ps -a
Stopping and Removing Containers
To stop a running container, use:
sudo docker stop container_id
To remove a stopped container, use:
sudo docker rm container_id
Conclusion
Docker is a powerful tool for developing, shipping, and running applications inside containers. This guide provided an introduction to Docker on Debian, Ubuntu, and Red Hat, covering installation, starting Docker, running a container, and basic container management. With Docker, you can ensure your applications run consistently in any environment.