Docker is a fantastic platform that enables developers to package applications into standardized units called containers to efficiently build, ship, and deploy the applications in any environment. In today’s world, DevOps professionals must seriously consider adding Docker to their skill set. Read on to learn how Docker helps streamline DevOps processes for better efficiency and effectiveness.
However, before learning about Docker, the first step is to install it. In this tutorial, you’ll learn how to install Docker on Ubuntu, how to use Docker on Ubuntu once installed, and more, including how you can upskill to become a certified DevOps professional.
Installing Docker on Ubuntu
Docker is an open-source containerization technology based on Linux that enable users to package and deploy applications in containers. Many containers can be executed simultaneously on a single host.
Ubuntu is the most preferred platform for managing Docker containers because it runs the containers at scale, besides being fast, secure, and open source.
The prerequisites for how to Install Docker on Ubuntu 20.04 include:
- One Ubuntu 20.04 server set up following the server setup guide, a sudo non-root user, and a firewall
- A user account on Docker Hub with administrator privileges
- Access to the terminal (Ctrl+Alt+T)
Steps for Installing Docker on Ubuntu
Chances are that the Docker installation package in the default Ubuntu repository may be an older version. Install Docker from the official Docker repository to ensure that you get the latest stable program version.
Option 1: Installing Docker from the Official Docker Repository
To access the official Docker repository, add a new package source to Ubuntu, add the GPG key from Docker to make sure the downloads are valid, and then Install Docker.
Follow the steps below:
Step #1 – Update the Package Repository
Execute the following command to update your system’s existing list of packages and ensure the latest required packages are installed.
sudo apt update
Enter your root password when prompted, and press Enter to continue with the update.
Step #2 – Install Prerequisite Packages
The ‘apt package manager’ requires some prerequisite packages on the system that let ‘apt’ use packages over HTTPS.
The following command will allow Ubuntu to access Docker repositories over HTTPS:
sudo apt install apt-transport-https ca-certificates curl software-properties-common
This command allows:
- ‘Apt’ to transfer files and data over HTTPS
- The system to check security certificates
- installation of ‘curl’ – a data transfer utility
- Addition of scripts for software management
Step #3 – Add GPG Key
A GPG key is used to verify the authenticity of any software package. Add the GPG key for the official Docker repository to your system by running the following command:
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add –
The output should state OK, which verifies the authenticity.
Step #4 – Add Docker Repository
The following command will add the Docker repository to ‘apt’ sources:
sudo add-apt-repository “deb [arch=amd64] https://download.docker.com/linux/ubuntu focal stable”
It will also update your package database with the latest Docker packages from the newly added repository.
Step #5 – Specify Installation Source
Run the apt-cache command to make sure the Docker installation source is the Docker repository, instead of the Ubuntu repository. This command queries the package cache about the Docker packages that have been previously added.
apt-cache policy docker-ce
The output will specify the version number for Docker in the added source repository.
Step #6 – Install Docker
Finally, install Docker by running:
sudo apt install docker-ce -y
Wait for the installation process to complete. Docker should now be installed.
Step #7 – Check Docker Status
Check that Docker is running by using the following command:
sudo systemctl status docker
The output should show that the Docker daemon is active and running.
Option 2: Installing Docker from the Default Ubuntu Repository
Although installing Docker from default repositories is easier, the Docker package may be outdated. In case you’re not concerned about having the latest Docker version, you can install Docker using the following steps:
Step #1 – Update the Repository
Update the local system package repository with the command:
sudo apt update
Enter the root password and wait for the update to be completed.
Step #2 – Install Docker
Install Docker using the following command:
sudo apt install docker.io -y
You’ll be prompted to choose between y/n. Choose the -y flag to answer ‘yes’.
Step #3 – Install Dependencies
Install all Docker dependency packages with the command:
sudo snap install docker
Step #4 – Test Installation
Check for correct installation by running the ‘status’ command:
sudo systemctl status docker
Checking the Docker version installed:
sudo Docker –version
How to Use Docker on Ubuntu?
Now that you’ve learned how to install Docker on Ubuntu, the next step will understand how to use Docker on Ubuntu. You can access all information about Docker, like syntax, options, and all available commands, by running the ‘docker’ command in the terminal.
*Note: Docker commands can only be run on Ubuntu by prefixing it with ‘sudo’.
Start using Docker by working with Docker images, creating containers, and managing Docker volumes.
Working with Docker Images
Docker images provide the base for building Docker containers. By default, Docker pulls these images from Docker Hub, a Docker repository. All users can host their images on Docker Hub, so there’s a vast array of images to choose from, including those hosted by applications and Linux distributions.
- To check if you can access and download Docker Hub images, use the command:
sudo Docker run hello-world
The output will indicate that you’ve pulled an image from Docker Hub successfully.
- Search for available Docker Hub images using the command:
sudo docker search [keyword]
Specify the keyword you wish to search for. To display all Ubuntu images, type:
sudo docker search ubuntu
- Pull a Docker Image by using the ‘pull’ command:
sudo Docker pull [image-name]
Once the image is downloaded, use it to create a container.
- To download the official Ubuntu image:
sudo Docker pull Ubuntu
- Check downloaded images by typing:
sudo docker images
The output shows all downloaded images on your system.
Working with Docker Containers
Docker Containers are isolated virtual environments created from Docker images. They allow developers to package an application along with its libraries and other dependencies and bundle it as a single unit.
- You can use any image you downloaded earlier or mention its name in the ‘docker run’ command to automatically download it and generate a container.
sudo Docker run hello-world
This command instructs Docker to create a container after downloading the image from Docker Hub. After creation, the “Hello from Docker” message is displayed, including an explanation of the container’s working, and then Docker terminates it.
- Run a Docker container by using the ‘run’ subcommand:
sudo docker run –name [container-name] [image-name]
- Name your container according to your choice by passing the ‘–name’ switch.
The container created is in non-interactive mode. Use a combination of -I and -t switches to gain interactive shell access to the container.
sudo Docker run -it Ubuntu
the command prompt would change, indicating that you’re now working within the container:
Here, the container ID is d9b100f2f636, which you can use to start, stop, or remove the container.
The ‘exit’ command lets you exit the container.
- To view active Docker containers, use:
sudo docker ps
- To view all containers including inactive ones:
sudo docker ps -a
- To view only the latest container:
sudo docker ps -l
- To start a stopped container:
sudo Docker start [container-ID | container-name]
- To stop a running container:
sudo docker stop [container-ID | container-name]
- To remove a Docker container:
sudo docker rm [container-ID | container-name]
Also Read: What is Azure DevOps? A Complete Guide
Working with Docker Volumes
Docker Volume is a file system that allows users to preserve data developed and used by Docker containers. Docker volumes are stored on the host, so they don’t depend on the containers. Instead, they can be used for easy data backup and sharing between multiple containers.
- Create a Docker Volume using:
sudo docker volume create [volume_name]
- Remove a Docker Volume using:
sudo docker volume rm [volume_name]
Several Docker networking commands help users to create and manage their networks inside a container.
Sharpen Your Docker Expertise for a Successful DevOps Career
Now that you’ve learned how to install Docker on Ubuntu and how to start using Docker to download images and create containers, you can begin your journey leveraging this containerization platform and benefit from its advantages.
Using Docker in DevOps is a great way to build applications by using unique interconnected components while ensuring the applications run seamlessly in any environment. Developers can enjoy considerable control over changes made during the application development lifecycle. This makes Docker an obvious choice for DevOps practitioners to scale up and speed up development and operations. In short, Docker makes it easier for DevOps professionals to create, ship, and deploy applications, and transition from on-premises to cloud or hybrid environments.
If you’re looking to boost your career in DevOps, completing this online DevOps bootcamp can help you bridge the gap between software development and operations while learning the latest tools and technologies DevOps experts and the companies they work for are using.
In this online bootcamp, you will learn the basics of containerization and microservices that will help you develop cloud-based and mobile applications, state-of-the-art architecture to support those, and gain expertise in agile DevOps practices. Get trained by global experts and acquire hands-on skills through real-world, industry-based course and capstone projects.
Apply now and become a DevOps professional in nine months!