Overview of Docker Containerization
Docker functions as an open-source platform designed for containerization, facilitating the build, test, and deployment processes. Applications are packaged into movable containers that include all necessary dependencies, ensuring consistent execution across different environments. This technology is integral to modern software development lifecycles, including Continuous Integration and Continuous Delivery (CI/CD) pipelines.
The following guide details the procedure for setting up the Docker Engine on an Ubuntu 20.04 system. While Docker is available in the default Ubuntu repositories, those versions may be outdated. To ensure access to the latest features and security patches, this method utilizes the official Docker repository.
Installation Procedure
Setting up Docker on Ubuntu involves configuring the package manager to trust the official Docker repository, importing security keys, and installing the engine components.
Begin by refreshing the local package database and installing prerequisites required for managing HTTPS sources:
sudo apt update
sudo apt install -y apt-transport-https ca-certificates curl gnupg-agent software-properties-common
Next, retrieve the GPG key from the Docker official server. Instead of piping directly, download the key to a local file first for verification:
curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o docker.key
sudo apt-key add docker.key
Configure the APT source list manually by creating a new list file. This approach offers more control than using the add-apt-repository helper:
echo "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list
With the repository configured, update the package index again to recognize the new Docker packages.
Installing the Latest Version
To deploy the most recent stable release of the Docker Engine, execute the following installation command:
sudo apt update
sudo apt install -y docker-ce docker-ce-cli containerd.io
Installing a Specific Version
If your environment requires a specific version, first query the repository to see available releases:
apt list -a docker-ce
The output will display available versions in the second column. Select the desired version string and append it to the package name during installation:
sudo apt install -y docker-ce=<VERSION_STRING> docker-ce-cli=<VERSION_STRING> containerd.io
Upon completion, the Docker daemon starts automatically. Verify the service status using systemd:
sudo systemctl status docker
A successful installation shows the service state as active (running).
Standard system upgrades will update Docker when new versions are released. To prevent automatic upgrades and lock the current version, mark the packages on hold:
sudo apt-mark hold docker-ce docker-ce-cli containerd.io
Configuring Non-Root Access
By default, Docker commands require root privileges or sudo access. To allow a standard user to interact with the Docker daemon without elevated permissions, add the user to the docker group created during installation:
sudo usermod -aG docker $(whoami)
For the group membership changes to take effect, log out of the current session and log back in.
Verification Steps
Confirm the installation by running a test container. This commmand pulls the hello-world image if it is not present locally, executes it, and prints a confirmation message:
docker run --rm hello-world
The --rm flag ensures the container is removed automatically after execution. Successful output indicates the Docker Engine is communicating correctly with the registry.
Optional: Registry Mirror Configuration
In regions with limited connetcivity to Docker Hub, configuring a registry mirror can improve pull speeds. Create or edit the daemon configuration file:
sudo nano /etc/docker/daemon.json
Insert the mirror URL into the configuration structure:
{
"registry-mirrors": [
"http://hub-mirror.c.163.com"
]
}
Apply the changes by restarting the Docker service:
sudo systemctl restart docker
Removing Docker
To completely remove Docker from the system, first stop all active containers and prune existing objects such as images, volumes, and networks:
docker stop $(docker ps -aq)
docker system prune --volumes --all
Finally, purge the Docker packages and remove unused dependencies:
sudo apt purge docker-ce
sudo apt autoremove