How to Install and Use Docker on Ubuntu 22.04
Accelerate your application development with Docker on Ubuntu Server. Docker is an open-source platform for building containerized applications and microservices. With Docker, every developer will have exactly the same environment. Also, you can easily share your application image with developers via the Docker Registry.
In this guide, we’ll take you through the installation of Docker on the Ubuntu system, and the basic usage of Docker for managing images, containers, volumes, and networking. With this, you can get started with ease.
Prerequisites
You will need the following to proceed:
- An Ubuntu server 22.04 or later.
See also: 15 Steps to Install Ubuntu Server 22.04 LTS - A non-root user with sudo administrator privileges.
Installing Docker on Ubuntu
To get the latest version of Docker, you must install it from the official Docker repository. Complete the following steps to install Docker from the official Docker repository. Then, check if Docker is running and check the Docker version.
1. First, run the apt command below to install the package ca-certificates
, curl
, and gnupg
to your system.
sudo apt install ca-certificates curl gnupg -y
2. Now execute the following command to add the GPG key of the Docker repository. The GPG key will be stored at /etc/apt/keyrings/docker.gpg
.
# Creating directory /etc/apt/keyrings
sudo install -m 0755 -d /etc/apt/keyrings
## Adding Docker GPG Key
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
# Change permission of Docker GPG key
sudo chmod a+r /etc/apt/keyrings/docker.gpg
3. Then, run the command below to add the Docker repository to your Ubuntu system. In this case, you will add the Docker repository stable version.
echo \
"deb [arch="$(dpkg --print-architecture)" signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
"$(. /etc/os-release && echo "$VERSION_CODENAME")" stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
4. Once you’ve added the Docker repository, refresh your Ubuntu package index via apt update
. Then, install Docker packages using the apt install
command below.
# updating repository
sudo apt update
# Installing Docker CE
sudo apt install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
Type y
to confirm and proceed with the installation.
5. After that, execute the following systemctl
command to ensure that Docker is running and enabled.
# checking if docker is enabled
sudo systemctl is-enabled docker
# checking docker status
sudo systemctl status docker
If the docker
service is running, you should get the output active (running)
. The output enabled
indicates that Docker will start automatically upon the system boot.
6. Lastly, run the docker
command below to check the Docker version on your system.
docker version
The following output confirms that Docker 24.0.7 is installed.
Running Docker Without Sudo on Ubuntu
By default, only the root user can run containers with Docker. To allow non-root users to run containers with Docker, you must add your user to the docker
group. This way you can run Docker containers without sudo.
1. First, execute the following command to add your user to the docker
group.
sudo usermod -aG docker username
2. Then, log in to your user via the su
command below.
su - username
3. Lastly, execute the following docker run
command to run a new container hello-world
.
docker run hello-world
If everything goes well, you should see the Hello World message from Docker.
Managing Docker Service on Ubuntu
The following steps are dedicated to managing the Docker service via the systemctl
utility. This includes how to start and stop Docker in your Ubuntu machine, resting Docker service when needed, and checking the status of Docker service.
1. To start the docker service, use the systemctl start
command below.
sudo systemctl start docker
2. Next, if you want to stop the docker service, use the following systemctl stop
command.
sudo systemctl stop docker
3. When you make changes to Docker, you must restart the docker
service to apply the changes. Execute the systemctl restart
command below to restart it.
sudo systemctl restart docker
4. Lastly, to check the docker service status, use the systemctl status
command below. If Docker is running, you should get an output such as active (running)
, otherwise, you can get an output such as failed
.
sudo systemctl status docker
Working with Docker Images
At this point, Docker is running on your Ubuntu machine. Now you’ll learn how to manage Docker images using the docker utility. This includes searching images, downloading images, checking information about an image, and then removing Docker images from your Docker host.
1. First, find available Docker images with the name apache2
in the Docker Image Registry by executing the docker search
command below.
docker search apache2
2. Next, download the Docker image via the docker pull
command below. In this case, you will download the image ubuntu/apache2
.
docker pull ubuntu/apache2
The download process should look like this:
3. Once you’ve downloaded the image, run the following command to verify the list of downloaded images on your system.
docker images
If you followed this tutorial so far, you should two Docker images, ubuntu/apache2
and hello-world
with the tag latest
.
4. After that, check the detailed information about the image ubuntu/apache2:latest
using the docker inspect
command with option --type=image
like the following.
docker inspect --type=image ubuntu/apache2:latest
The output should be formatted as json like the following:
5. Furthermore, you can also check the information of the specific part from the Docker image by adding the parameter --format="{{.Section}}"
. For example, execute the command below to check the Config.Cmd
and Size
sections from the Docker image ubuntu/apache2:latest
.
# checking the default CMD command
docker inspect --type=image --format="{{.Config.Cmd}}" ubuntu/apache2:latest
# checking size of image in bytes
docker inspect --type=image --format="{{.Size}}" ubuntu/apache2:latest
6. Lastly, run the docker rmi
command below to delete the Docker image hello-world
from your Ubuntu system.
sudo rmi hello-world
Managing Container
After downloading the Docker image, you’ll learn how to run the container, check if the container running, and inspect the container to get information about it, then you will also start and stop the container.
1. First, execute the docker run
command below. After the command is executed, you should see the container ID.
docker run -d --name webserver1 -e TZ=UTC -p 8080:80 ubuntu/apache2:latest
Detailed options used in the command:
- -d: Run the container in the background and imprint the container ID.
- –name webserver1: give the new container name
webserver1
. - -e TZ=UTC: Set up environment variable
TZ
for the time zone within the container toUTC
. - -p 8080:80: Expose the port
8080
on the Docker host machine, which is equivalent to port80
on the container.
2. Now run the docker ps
command below to verify the list of running containers.
docker ps
If everything goes well, you should see the container webserver1
with the status running
and exposed port 8080
on the Docker host machine.
3. Then, run the docker inspect --type=container
command below to get detailed information about the container webserver1
. The output will be in json format, which contains detailed information about the webserver1
container.
docker inspect --type=container webserver1
4. Next, you can also specify the output of specific information via the --format="{{json .Section}}"
parameter. For example, run the command below to check the status
and pid
(process id) of the webserver1
container.
# checking the state/status and PID of webserver1
docker inspect --type=container --format="{{json .State.Status}}" webserver1
docker inspect --type=container --format="{{json .State.Pid}}" webserver1
5. Furthermore, run the following command to get the internal IP address from the section .NetworkSettings.IPAddress
and the exposed port from the section .NetworkSettings.Ports
on the webserver1
container.
# checking the IP address and ports of webserver1
docker inspect --type=container --format="{{json .NetworkSettings.IPAddress}}" webserver1
docker inspect --type=container --format="{{json .NetworkSettings.Ports}}" webserver1
6. Next, stop the container webserver1
using the docker stop
command below. Then verify again the list containers via the docker ps -a
command. The -a
parameter will show you containers that are running and exited.
# stopping container webserver1
docker stop webserver1
docker ps -a
You should see the webserver1
container is exited.
7. Then, if you want to start the container webserver1
again, simply run the docker start
command below.
docker start webserver1
8. Lastly, run the docker rm
command below to remove the container webserver1
. The parameter -f
is used to force the removal of the container.
docker rm webserver1 -f
Managing Volumes
Volumes are where you can store data of your application persistently in Docker. In the following step, you’ll learn how to create, manage, and use Docker volume on your container.
1. First, create a new directory ~/local-data
and the custom index.html
page via vim
editor.
mkdir -p ~/local-data
vim ~/local-data/index.html
Insert the following HTML script into the file.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Welcome to Apache2 Docker</title>
</head>
<body>
<h2>Hello from webserver2 container - Powered by Apache2</h2>
</body>
</html>
When finished, save and exit the file.
2. Then, execute the docker run
command below to create a new container webserver2
.
docker run -d --name webserver2 -e TZ=UTC -v ~/local-data:/var/www/html -p 8082:80 ubuntu/apache2:latest
-v ~/local-data:/var/www/html: The -v
option is used to mount the volume of the local directory ~/local-data
to the target directory /var/www/html
on the container.
3. Next, verify the list running container using the docker ps
command below. You should see the webserver2
container with the status UP
and exposed port 8082
on the Docker host machine.
docker ps
4. After that, run the docker inspect
command below to check the Binds
and Mounts
within the webserver2
container.
# checking bind volume of webserver2
docker inspect --format="{{json .HostConfig.Binds}}" webserver2
# checking mounts of volume in webserver2
docker inspect --format="{{json .Mounts}}" webserver2
You should get an output that the local directory ~/local-data
is mounted to the target directory /var/www/html
on the container.
5. Lastly, launch your web browser and visit the server IP address followed by port 8082
, i.e: http://192.168.5.30:8082/. If everything goes well, you should see the custom index.html page that you’ve created on the ~/local-data
directory.
Managing Networks
The Docker container is running in an isolated environment and uses its networking configuration. each container has an internal IP address, which is provided by the Docker network. Complete the following section to learn how to create, manage, and use the Docker network.
1. To start, execute the docker network create
command below to create a new network on Docker. In this case, you will create a new network mynet
with the default configuration for both driver and subnet.
docker network create mynet
2. After you’ve created mynet
network, execute the docker network ls
command below to verify the list of networks on Docker. You should see network mynet
is created.
docker network ls
3. Also, you can check the specific configuration of the mynet
network via the docker inspect --type=network
command below.
# checking default driver of Docker network mynet
docker inspect --type=network --format="{{json .Driver}}" mynet
# checking IP address configuration of mynet network
docker inspect --type=network --format="{{json .IPAM.Config}}" mynet
You should see the mynet network is using the default driver bridge
and subnet 172.18.0.0/16
.
4. Next, use the docker run
command below with the parameter --network
to attach a specific network for your container. In this case, you will create a new container webserver3
with the network mynet
.
docker run -d --name webserver3 --network mynet -e TZ=UTC -p 8083:80 ubuntu/apache2:latest
5. Once the webserver3
container is running, install the jq
package via the apt install
command below. The jq
package is a json parser program that makes json output colorful and pretty.
sudo apt install jq -y
6. Lastly, run the docker inspect --type=container
command below to check the network configuration for the webserver3
container.
docker inspect --type=container --format="{{json .NetworkSettings.Networks}}" webserver3 | jq
The following output confirm that the webserver3
container uses mynet
as the default networking configuration with IP address 172.18.0.2
.
Checking Container Logs for Troubleshooting
In the following step, you will learn how to check the logs of your containers via the docker client.
1. To check the logs of the container, run the docker logs
command below. This will show you all logs since the container is created to present.
docker logs webserver3
2. Next, add the option --follow
to show container logs in streaming mode.
docker logs --follow webserver3
3. Then, you can also check the last N lines of container logs via the --tail
parameter like this. In this case, you will check logs for the last 10 lines.
docker logs --tail 10 webserver3
4. Furthermore, you can also show logs of containers since timestamps via the --since
parameter.
docker logs --since 20m webserver3
5. Lastly, use the --until
parameter to sho logs before timestamps.
docker logs --until 10m webserver3
Conclusion
To wrap up, you’ve completed the installation of Docker on the Ubuntu Server. You’ve also learned how to manage images, containers, volumes, and networking in Docker. You can now start containerizing your application via Docker.
Further reading, go over the containerizing of language-specific applications, such as Node.js, Python, Golang, and Java.