Containers are a solution to the problem of how to get software to run reliably when moved from one computing environment to another. How does it work?, well it bundles an application’s code together with the related configuration files and libraries, and with the dependencies required for the app to run. This allows us to deploy applications seamlessly across environments.
Builder: Its a tool used to build, in the case of Docker is a DockerFile which is a text document that contains all the commands a user could call on the command line to assemble an image.
Engine: It is an application used to run a container. For Docker, this refers to the docker command and acts like a client and the dockerd daemon which acts as a server. It is called the Docker Engine.
Orchestration: If we need to deploy many containers a technology used to manage many containers is needed. This process includes provisioning, deployment, scaling (up and down), networking, load balancing and more. A good example would be Kubernetes.
In this tutorial we will be focusing on the Docker Engine.
- How to install Docker from the regular Ubuntu repository.
- How to enable Docker to start automatically at system boot.
- How to install Docker images and run them locally.
- How to configure Docker.
- How to search and run images from docker hub.
- Ubuntu 18.04(LTS) , 20.04 (LTS), 21.04, or 21.10.
- User with administrator privileges.
- Basic Command-line knowledge.
Note: For This procedure we will use Ubuntu Focal 20.04 (LTS).
If you don’t have Ubuntu installed you can follow this How to Install Ubuntu Tutorial.
Or you can also install Ubuntu on a virtual machine with this tutorial.
Let’s remove some older installations of docker, just in case!. This way we make sure to have a clean installation. If you are sure you don’t have docker installed go the the installation process.
sudo apt-get remove docker docker-engine docker.io containerd runc
The apt-get command should remove packages related to docker if they were installed.
if passages were removed we need to make sure that the contents in the directory
/var/lib/docker/ is not preserved. To do this follow these steps:
To uninstall the Docker Engine, CLI, and Containerd packages:
sudo apt-get purge docker-ce docker-ce-cli containerd.io
To delete all images, containers, and volumes:
sudo rm -rf /var/lib/docker
sudo rm -rf /var/lib/containerd
In this tutorial will be installing the docker engine using the Ubuntu repositories
sudo apt update
sudo apt-get install \
This command will update the apt package index, this way we can use apt to use the repository over https.
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
Note: GPG is a public key cryptography implementation. This allows for the
secure transmission of information between parties and can be used to verify
that the origin of a message is genuine.
Use the following command to set up the stable repository
"deb [arch=$(dpkg --print-architecture) 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
We can verify the repository has been added
ls -al /etc/apt/sources.list.d | grep docker
sudo apt-get install docker-ce docker-ce-cli containerd.io
If you have multiple Docker repositories enabled, installing or updating without
specifying a version in the
always installs the highest possible version, which may not be appropriate for
your stability needs.
1 Verify the available version in the repository.
apt-cache madison docker-ce
Lets say we want to install the version available 5:20.10.5~3-0~ubuntu-focal
2 Run the installation command.
sudo apt-get install docker-ce=5:20.10.5~3-0~ubuntu-focal docker-ce-cli=5:20.10.5~3-0~ubuntu-focal containerd.io
Now that we have installed the docker engine version that we need we can test the docker engine by downloading and running an docker image:
sudo docker run hello-world
At this point we can work normally with the docker engine. However, it is recommended to verify and configure certain aspects to work more comfortably.
sudo systemctl list-unit-files --type=service | grep docker.service
If the second column “STATE” is enabled means that the docker service will start at boot.
If the STATE column is disabled we can enable it by running this command.
sudo systemctl enable docker.service
The Docker daemon always runs as the root user. This means that a regular user needs to use sudo to access this service.
If you don’t want to preface the docker command with sudo, follow these steps:
- Create a docker group. (In case it doesn’t exist)
sudo groupadd docker
- Add your user to the docker group.
sudo usermod -aG docker $USER
- Activate the changes to groups.
- Verify that you can run docker commands without sudo.
docker run hello-world
Here is the sequence:
The docker group grants privileges equivalent to the root user.
This can make your system vulnerable to docker daemon attacks.
It is possible to allow Docker to accept requests from remote hosts by configuring it to listen on an IP address and port as well as the UNIX socket.
The recent versions of Ubuntu uses systemd, this means that we can configure docker to accept remote connections with the docker.service unit-file.
Lets see How this works!!!
sudo systemctl edit docker.service
Lets say we want to listen for connections from any remote host on port 2376
ExecStart=/usr/bin/dockerd -H fd:// -H tcp://0.0.0.0:2376
sudo systemctl daemon-reload
sudo systemctl restart docker.service
sudo netstat -lntp | grep dockerd
here is the sequence:
If we need to use ipv6 with docker you can follow these steps. We can choose to use either IPv4 or IPv6 (or both) with any container, service, or network.
###1. Edit the daemon.json file in /etc/docker/ or create it if it doesn’t exist.
In the daemon.json file:
- In the file set the ipv6 key to true
- The fixed-cidr-v6 to your IPv6 subnet.
- Save the file.
systemctl reload docker
sudo nano /etc/docker/daemon.json
"dns": ["188.8.131.52", "184.108.40.206"]
Make sure to include at least one DNS server which can resolve
public ip addresses, so that you can connect to Docker Hub and so that your
containers can resolve internet domain names.
Save and close the file.
sudo service docker restart
docker pull hello-world
You made it!
If you have been watching closely, you are now fully equipped to install Docker Engine.
Now you know how to:
- Perform a clean install of Docker Engine.
- Perform additional configurations to customize the installation