Share your love
Install and Use Docker on Ubuntu 20.04
In this article, we want to teach you to Install and Use Docker on Ubuntu 20.04. Docker is a great tool that can ease the creation, implementation, and performance processes with the containers. To see what Docker exactly is check our article about what is Docker and how it works. Now follow the steps below to complete this guide.
Complete Guide To Install and Use Docker on Ubuntu 20.04
In this article, you will install Docker itself on Ubuntu 20.04. You need some requirements to install the Docker.
Requirements
You need to log in as a non-root user with Sudo privileges to install Docker on Ubuntu 20.04. To do this, you can visit this article about the Initial server setup with Ubuntu 20.04.
Also in the article, you will need to create an account on DockerHub to create your own images and push them to Docker Hub.
Follow the steps below to start your Docker installation.
Step 1 – Install Required Packages for Docker
The Docker installation package is available in the official Ubuntu repository by default. But it may not be in the latest version. To get the latest version, update the existing packages on Ubuntu 20.04 with the following command:
sudo apt update
Here you need to install a few required packages that let apt use packages over HTTPS, run the following command:
sudo apt install apt-transport-https ca-certificates curl software-properties-common
Step 2 – Add Docker Repository To Ubuntu 20.04
Then, you should add the GPG key for the official Docker repository to your system with the following command:
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
In this step, you need to add the Docker repository to APT sources on Ubuntu 20.04:
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu focal stable"
Update the packages with the following command:
sudo apt update
You need to be sure about installing from the Docker repository instead of the default Ubuntu repository:
apt-cache policy docker-ce
In your output you will see something like this:
Output
docker-ce:
Installed: (none)
Candidate: 5:24.0.6-1~ubuntu.20.04~focal
Version table:
5:24.0.6-1~ubuntu.20.04~focal 500
...
Step 3 – Command for Installing Docker on Ubuntu 20.04
Now the docker-ce candidates to install. Install Docker on Ubuntu 20.04 with the following command:
sudo apt install docker-ce
When the installation is completed, check that is active and running on Ubuntu 20.04 with the following command:
sudo systemctl status docker
Your output should be similar to this:
Output
docker.service - Docker Application Container Engine
Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset>
Active: active (running) since Sun 2021-08-29 13:10:10 CEST; 26s ago
TriggeredBy: ● docker.socket
Docs: https://docs.docker.com
Main PID: 8435 (dockerd)
Tasks: 8
Memory: 28.4M
CGroup: /system.slice/docker.service
└─8435 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/cont>
Now you have installed Docker on Ubuntu 20.04. It will give you the Docker service, as well as the Docker command-line utility.
Note: As we mentioned above, you need sudo privileges to run docker commands. Or if you don’t want to use sudo, you can add a user to the docker group. In this way, the user can run commands without using sudo.
To add a user to the docker group, run the following command:
sudo usermod -aG docker ${USER}
You can log out and back into the server with the same user.
If you want to add a user that you aren’t logged in with, run the following command:
sudo usermod -aG docker username
For the rest of the article, we run commands as a user in the docker group.
Step 4 – Docker Command Syntax Options
At this point, you install Docker on Ubuntu 20.04. Now let’s know about the docker command.
The syntax of using the docker command is like this:
docker [option] [command] [arguments]
You can list all available subcommands by typing the following command:
docker
Your output should be like this:
Output
attach Attach local standard input, output, and error streams to a running container
build Build an image from a Dockerfile
commit Create a new image from a container's changes
cp Copy files/folders between a container and the local filesystem
create Create a new container
diff Inspect changes to files or directories on a container's filesystem
events Get real time events from the server
exec Run a command in a running container
export Export a container's filesystem as a tar archive
history Show the history of an image
images List images
import Import the contents from a tarball to create a filesystem image
info Display system-wide information
inspect Return low-level information on Docker objects
kill Kill one or more running containers
load Load an image from a tar archive or STDIN
login Log in to a Docker registry
logout Log out from a Docker registry
logs Fetch the logs of a container
pause Pause all processes within one or more containers
port List port mappings or a specific mapping for the container
ps List containers
pull Pull an image or a repository from a registry
push Push an image or a repository to a registry
rename Rename a container
restart Restart one or more containers
rm Remove one or more containers
rmi Remove one or more images
run Run a command in a new container
save Save one or more images to a tar archive (streamed to STDOUT by default)
search Search the Docker Hub for images
start Start one or more stopped containers
stats Display a live stream of container(s) resource usage statistics
stop Stop one or more running containers
tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
top Display the running processes of a container
unpause Unpause all processes within one or more containers
update Update configuration of one or more containers
version Show the Docker version information
wait Block until one or more containers stop, then print their exit codes
To get more information about the sub-commands, you can run the following command:
docker docker-subcommand --help
To view system-wide information about Docker, use:
docker info
When you get to know about the docker command on Ubuntu 20.04. Let’s see how to work with them.
Step 5 – Work with Docker images on Ubuntu 20.04
Docker containers are built from Docker images. By default, Docker pulls these images from Docker Hub. Anyone can host their Docker images on Docker Hub, so most applications and Linux distributions you’ll need will have images hosted there.
To check that you can access and download images from Docker Hub, run the following command:
docker run hello-world
Your output should seem like this:
Output
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
b8dfde127a29: Pull complete
Digest: sha256:7d91b69e04a9029b99f3585aaaccae2baa80bcf318f4a5d2165a9898cd2dc0a1
Status: Downloaded newer image for hello-world:latest
Hello from Docker!
This message shows that your installation appears to be working correctly.
Search for Docker Images
Now you can search for available images on Docker Hub, for example, run the following command to search Ubuntu images:
docker search ubuntu
In the output you will see something similar to this:
Output
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
ubuntu Ubuntu is a Debian-based Linux operating sys… 12691 [OK]
dorowu/ubuntu-desktop-lxde-vnc Docker image to provide HTML5 VNC interface … 561 [OK]
websphere-liberty WebSphere Liberty multi-architecture images … 280 [OK]
rastasheep/ubuntu-sshd Dockerized SSH service, built on top of offi… 255 [OK]
consol/ubuntu-xfce-vnc Ubuntu container with "headless" VNC session… 241 [OK]
ubuntu-upstart DEPRECATED, as is Upstart (find other proces… 113 [OK]
1and1internet/ubuntu-16-nginx-php-phpmyadmin-mysql-5 ubuntu-16-nginx-php-phpmyadmin-mysql-5 50 [OK]
Download Docker Images
Here, when you choose the image that you want to use, you can run the following command to download the image:
docker pull ubuntu
You will see the following output:
Output
Using default tag: latest
latest: Pulling from library/ubuntu
16ec32c2132b: Pull complete
Digest: sha256:82becede498899ec668628e7cb0ad87b6e1c371cb8a1e597d83a47fac21d6af3
Status: Downloaded newer image for ubuntu:latest
docker.io/library/ubuntu:latest
After you download the image, you can run a container with that image with the “run” subcommand. If an image wasn’t downloaded the “run” subcommand downloaded it first then ran the container using it.
List Downloaded Docker Images
To see the images you have downloaded run the following command:
docker images
You will see something like this:
Output
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu latest 1318b700e415 4 weeks ago 72.8MB
hello-world latest d1165f221234 5 months ago 13.3kB
At this point, you learn how to work with Docker images on Ubuntu 20.04. now let’s see how to run Docker containers.
Step 6 – Run Docker containers with the images
Here you can run a container using the latest image of Ubuntu with the following command:
docker run -it ubuntu
Note: -it switches give you interactive shell access into the container.
Your command prompt should change to this form:
root@450142aac378:/#
Important Note: Remember the container id in the command prompt. In this example, the container ID is 450142aac378.
Now you can run any commands that you want. You don’t need to use sudo for your commands because you run commands as a root inside the container.
root@450142aac378:/# apt update
Here you can install any application. Here we install Node.js with the following command:
root@450142aac378:/# apt install nodejs
Note Any changes you make inside the container only apply to that container.
You can exit from the container by the following command:
root@450142aac378:/# exit
When you finished running Docker containers on Ubuntu 20.04 let’s see how to manage them.
Step 7 – Manage Docker containers from CLI
You can see active and running containers with the following command:
docker ps
To list all containers both active and inactive. Use the following command;
docker ps -a
In your output you will see:
Output
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
450142aac378 ubuntu "bash" 3 minutes ago Exited (127) 11 seconds ago objective_galois
9b2ee1d994ce hello-world "/hello" 6 minutes ago Exited (0) 6 minutes ago compassionate_dirac
You can see the containers you created with:
docker ps -l
Output
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
450142aac378 ubuntu "bash" 3 minutes ago Exited (127) 47 seconds ago objective_galois
Also, you can start an existing container with a container ID. For example:
docker start 450142aac378
Then, you can use the docker ps
command to see its status. Your output should be similar to this:
Output
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
450142aac378 ubuntu "bash" 5 minutes ago Up 27 seconds objective_galois
To stop a running container, you can run the following command with the container ID or name:
docker stop objective_galois
If you want to remove a container you can run the following command with the name of that container. For example, to remove the hello-world, you can run the following command:
docker rm compassionate_dirac
At this point, you learn how to manage Docker containers on Ubuntu 20.04.
Let’s see how Containers can be turned into images that you can use to build new containers.
Step 8 – Commit Changes in a Container to a Docker Image
In this part, you learn how to save the state of a container as a new Docker image.
After installing Node.js inside the Ubuntu container, you now have a container running off an image, but the container is different from the image you used to create it. But you might want to reuse this Node.js container as the basis for new images later.
Here you can commit the changes to a new Docker image with the following command:
docker commit -m "What you did to the image" -a "Author Name" container_id repository/new_image_name
For example for the user olivia, with the container ID of 450142aac378 :
docker commit -m "added Node.js" -a "olivia" 450142aac378 olivia/ubuntu-nodejs
Then, you can see your available images with the following command:
docker images
In your output you will see:
Output
REPOSITORY TAG IMAGE ID CREATED SIZE
olivia/ubuntu-nodejs latest 5d98f09fcc3d 14 seconds ago 72.8MB
ubuntu latest 1318b700e415 4 weeks ago 72.8MB
hello-world latest d1165f221234 5 months ago 13.3kB
Step 9 – Push Docker images to a Docker repository
After you create a new image from an existing image you may want to share it with a few of your friends, the whole world on Docker Hub, or other Docker registries that you have access to. To push an image to Docker Hub or any other Docker registry, you must have an account there.
To have an account on Docker Hub you need to register at Docker Hub.
If you want to log in to the Docker hub you will be asked for authentication :
docker login -u docker-registry-username
If you enter the correct password, authentication should succeed. Then, you may push your own image using the following command:
docker push docker-registry-username/docker-image-name
For example:
docker push olivia/ubuntu-nodejs
It will take a little time to complete. After you push an image to a registry, it should be listed on your account’s dashboard.
Note: If a push attempt results in an error of this sort, log in, then repeat the push attempt.
Conclusion
In this article, you have learned to install Docker, work with images and containers, and push a modified image to Docker Hub on Ubuntu 20.04. Hope you enjoy it. If you need any help, please comment for us.