Installation
Start by updating your existing list of packages:
sudo apt update
sudo apt upgrade
Next, install a few packages that let apt
to use packages over HTTPS:
sudo apt install apt-transport-https ca-certificates curl software-properties-common
Adding the GPG key for the official Docker repository to your system:
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
Add the Docker repository to APT sources:
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu focal stable"
This will also update our package database with the Docker packages from the newly added repo.
Make sure you are about to install from the Docker repo instead of the default Ubuntu repo:
apt-cache policy docker-ce
Output:
Installing Docker:
sudo apt install docker-ce
When it's completed, verify the Docket status:
sudo systemctl status docker
Output:
To check Docker version:
docker version
Now Docker is installed in your system. You can start making a container by downloading a Docker Image from the Docker Registry.
Basic Usage Of Docker
Now I will observe standard Docker commands (e.g., how to download a Docker image, build a container, and access the container).
To create a new container, you should start by choosing a base image with the OS.
To check your Ubuntu images:
docker search ubuntu
Output example:
Now it's time to download the base image to our server, use the command:
docker pull ubuntu
Output:
Now you can see all downloaded images by using the command:
docker images
Output:
The Ubuntu image was downloaded from DockerHub/Docker Registry. The next step is to create a container from that image.
To create the container, you can use either' docker create' or' docker run':
docker create ubuntu:22.04
Output:
It has created a container, but it hasn't started; to start it:
docker run -i -t ubuntu:22.04 /bin/bash
This command will create and run a container based on the Ubuntu 22.04 image and run a command /bin/bash inside the container; you will be automatically inside the container after running the command.
The container will stop when you leave it with the command exit. If you want to have a container running in the background, you just need to add the -d option to the command:
docker run -i -t -d ubuntu:22.04 /bin/sh -c "while true; do echo hello world; sleep 1; done"
To check the container running in the background:
docker ps
If you want to see the logs result from that bash command, you can use the command:
docker logs NAMES/ContainerID
How can I access the shell of the container that runs in the background mode? This command will connect you to the shell of the container:
docker exec -i -t ContainerID /bin/bash
If the hostname and the container ID are equal, this means that you are inside the container shell. When you type 'exit' on that shell, you will leave the shell, but the container will still be running.
Another command that you will use often is:
docker stop ContainerID
This will stop the container without deleting it so that you can start it again with the command:
docker start ContainerID
If you like to remove the container, stop it first and then remove it with the command:
docker rm ContainerID
This is a short introduction to installing and using Docker on the Ubuntu operating system.
Conclusion
Docker is an open-source container virtualization platform that helps developers deploy their applications and system administrators manage applications in a secure virtual container environment.