Edited 3 days ago by ExtremeHow Editorial Team
DockerContainersVirtualizationApplication DeploymentDevOpsMicroservicesOrchestrationSystem AdministrationInstallationManagement
This content is available in 7 different language
Docker is a platform that allows developers to easily build, run, and manage applications in containers, which are lightweight, standalone execution environments that make shared use of an operating system (OS) while maintaining isolation. Docker has become quite popular because it helps streamline workflows, making deployment and scaling easier.
In this guide, we will learn how to install and use Docker on a Linux system. The article will cover installation steps, basic commands, and some useful example use-cases for Docker on Linux.
Before we dive into the installation process, let's look at why Docker is a great tool:
Before you start installing Docker, you must have the following:
sudo
privileges to install software packages.The first step is to make sure your system is up-to-date. Use the following command to update your package list and install available updates:
sudo apt update
sudo apt upgrade
This ensures that all existing packages are up to date and compatible with the Docker installation.
Docker requires some packages to work properly. Run these commands to install these packages:
sudo apt install apt-transport-https ca-certificates curl software-properties-common
Explanation: These packages help Docker to access repositories over HTTPS, manage certificates, and add new repositories.
The next step is to add the Docker repository to your APT sources. This repository contains the latest version of Docker.
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
Explanation: The first command adds Docker's official GPG key, ensuring that packages are correctly signed and valid. The second adds the Docker repository to the system's list of APT sources.
Now, update the package list again to include the Docker repository, and install Docker:
sudo apt update
sudo apt install docker-ce
Explanation: Here, docker-ce
means "Docker Community Edition", a free and open-source version suitable for Linux distributions.
After the installation is complete, you must start the Docker service and enable it to start on boot:
sudo systemctl start docker
sudo systemctl enable docker
This ensures that the Docker daemon is active and will start automatically when the system is rebooted.
To verify if Docker is correctly installed and running, execute the following command:
sudo docker --version
You will see an output displaying the Docker version installed on your system.
To run an Nginx web server using Docker:
sudo docker run --name my-nginx -p 8080:80 -d nginx
Explanation: This command performs the following tasks:
run
: Launches a new container.--name my-nginx
: Assigns the name “my-nginx” to the container for easy identification.-p 8080:80
: Maps port 80 (the default Nginx port) in the container to port 8080 of the host machine.-d
: Runs the container in detached mode, allowing it to run in the background.nginx
: Tells Docker which image to use (if it doesn't exist locally it will be pulled from Docker Hub).You can now visit http://localhost:8080 on your browser to see the running Nginx server.
To view all currently running Docker containers, use:
sudo docker ps
The output will show the container ID, name, status, and other important information.
If you want to stop a running container, use:
sudo docker stop my-nginx
Replace my-nginx
with the name or ID of the container you want to stop.
To remove a stopped container, use:
sudo docker rm my-nginx
This command deletes the specified container. Make sure the container is closed before deleting it.
To download a Docker image from Docker Hub (a repository for Docker images), use:
sudo docker pull ubuntu
Explanation: This command pulls the official Ubuntu image from Docker Hub so you can use it to build containers.
If you want to remove unused Docker images from your system, use:
sudo docker rmi ubuntu
This command will remove the specified Docker image.
Docker serves many functions in software development and deployment. Some example scenarios include:
Developers can use Docker to create customizable development environments that match their production environments. This prevents the common problem of "it works on my machine", ensuring consistency for all developers and systems. Here's an example:
FROM node:14
WORKDIR /app
COPY . .
RUN npm install
CMD ["node", "index.js"]
In this Dockerfile example, you can create a Node.js application container. It specifies a base image (node:14
) to use, sets the working directory, copies local files to the container, installs required packages, and runs the Node.js application.
Docker's ability to isolate applications is perfect for microservices, where each service is contained and deployed independently. Using Docker Compose, you can configure and run multiple interconnected Docker containers with a single command.
version: '3'
services:
web:
image: nginx
ports:
- "8080:80"
db:
image: postgres
This docker-compose.yml
file defines two services: web
(running Nginx) and db
(running PostgreSQL). The Nginx service maps port 80 from inside the container to port 8080 on the host.
Docker is often used in CI/CD pipelines to automate testing and deployment processes by creating containers on demand to test software. This allows developers to experience more reliable and faster testing cycles.
Docker helps developers and system administrators to deploy applications smoothly by maintaining the entire application, as well as its dependencies, inside a container. This makes it easy to replicate the same environment at different locations without any additional configuration. It is useful for quickly deploying web applications, data analysis pipelines, and more.
This guide provides detailed information on installing and using Docker on Linux systems. As a powerful and flexible container technology, Docker has many advantages for developing, deploying, and managing applications. The included commands and examples provide a foundation for starting to use Docker efficiently.
Docker remains one of the most influential tools in virtual environments, playing a vital role in modern DevOps practices and cloud computing infrastructures. Docker adoption is steadily growing due to its portable, lightweight, and consistent nature across various stages of software development and production environments.
If you find anything wrong with the article content, you can