How to install and use Docker on Ubuntu 20.04?
Last updated
Last updated
Docker allows users to package and run applications inside of containers. Both containers and virtual machines are isolated but, containers are more portable and resource friendly. Numerous containers can run at once on a single host due to the isolation.
In order to manage the containers, Docker uses a client-server architecture and the Docker daemon.
You will discover how to set up and utilize Docker on Ubuntu 20.04 in this article.
There are certain prerequisites that need to be met before you begin:
Ubuntu 20.04 installed, or an Ubuntu 20.04 server set up.
User account with admin privileges.
Working knowledge of the Linux command line.
For administering Docker or Kubernetes containers, Ubuntu is the best platform. This is because Ubuntu powers millions of machines worldwide, is swift, robust, and open-source, and runs the containers at scale.
This tutorial will cover docker installation using the official docker repository. (You may also use the default ubuntu repositories for installation but that doesn't ensure installation of the latest docker version)
Follow the steps mentioned below to install Docker on Ubuntu 20.04 :
1. Uninstall Older Versions
Uninstall all pre-existing older versions of Docker before proceeding with the installation of the latest version of Docker.
Use the following command to remove the older versions :
2. Updating the Package Repository
Use the following command to update the existing list of packages :
Enter the root user password when prompted to do so.
3. Installing Prerequisite Packages
Install some prerequisite packages that enable apt to use packages over HTTPS.
Use the following command to install the prerequisite packages :
4. Adding the GPG Key
The legitimacy of a software package is confirmed by a GPG key.
Use the following command to add the docker repository GPG key :
The output should display OK, verifying its legitimacy.
5. Adding the Docker Repository
Use the following command to add the Docker repository to apt sources :
This command will update our package database with the latest docker packages.
6. Establish Installation Source
Use the following command to establish that the Docker installation source is the Docker repository and not the Ubuntu repository :
You will see an output similar to this :
7. Install Docker
Use the following command to install Docker:
Wait for the installation to finish.
8. Check Docker Status
Use the following command to check Docker status:
The output will display that the Docker daemon is up and running.
Run the docker command in the terminal to access all Docker information, including syntax, options, and commands.
The foundation for creating Docker containers are Docker images. The images can be found on Docker Hub, a repository for Docker files. All users are able to host their images on Docker Hub thanks to the repository, which provides a large selection of images, including Linux distributions and apps.
The sections below demonstrate various methods of using Docker images.
Use the docker search command to search for images accessible on Docker Hub.
Specify the Image Name or Keyword you want to search for. For example, to search for all Node JS images, run :
The output of this command is a list of all images containing the nodejs keyword. If the official column has the OK parameter it indicates that the image was uploaded by the official company that created the project.
Choose the image you want, then use the pull option to download it. The syntax is :
For instance, pull the official python image by running :
Use the image to run a container once it has been downloaded. If you try to run a container from an image that has not been downloaded yet, docker will download the image first before running the container.
Browse Downloaded Images
Use the following command to see a list of previously downloaded images :
A Docker container is a virtual environment that is built from a Docker image. To automatically download an image and construct a container, use an image you have already downloaded or specify it's name in the docker run command.
For example download a test image and start up a container using the python image.
This will create a container from the mentioned image but it will run in non-interactive mode, utilize the -i and -t switches to get shell access to the container.
After running the container in interactive mode you can run the commands as usual, you do not need to use sudo as the shell has root access.
Use the exit command to exit the container.
Use the following command to view the list of all active containers :
To view the list of all active and inactive containers use the -a flag with the previous command.
Use the following command to start a stopped container :
You can either use the container ID or the container name to specify the container you wish to start.
For example :
Use the following command to stop a running container :
You can either use the container ID or the container name to specify the container you wish to stop.
For example :
Use the following command to remove any irrelevant containers using the following command :
You can use either the container ID or the conatiner name to specify the container you wish to remove.
For example :
Note: Use the --rm switch while creating a container to automatically remove it when stopped.
Volumes are the ideal method to store data generated and used by containers. Volumes are entirely managed by docker and stored on the host, they are not dependent on the container.
Use the following command to create a docker volume:
You can assign any unique name you want to the volume.
For example:
Use the following command to remove a docker volume:
Specify the name of the volume you wish to remove.
For example:
Use the following command to uninstall Docker Engine, CLI, containers and Docker compose packages:
Use the following command to remove all Docker images, containers, volumes, or custom configuration files:
This tutorial showed how to install docker on ubuntu 20.04 and use it to download images, create containers and create volumes.