Installing Docker on Ubuntu in No Time: a Step-by-Step Guide

Avatar

By squashlabs, Last Updated: April 15, 2023

Installing Docker on Ubuntu in No Time: a Step-by-Step Guide

Introduction

Whether you’re a seasoned developer or a beginner eager to explore the world of containers, Docker is an indispensable tool in your arsenal. Docker simplifies the process of managing and deploying applications in isolated environments called containers.

This means no more ‘but it works on my machine’ scenarios – if it works in a Docker container, it’ll work anywhere Docker is installed. This blog post will walk you through the entire process of getting Docker up and running on your Ubuntu system. We’ll cover everything from understanding Docker editions to navigating the installation process, and even troubleshooting common issues. Let’s dive in!

Related Article: 6 Essential software testing tools to add to your arsenal (2023 updated)

Prerequisites

Before we begin the installation process, ensure that you have a version of Ubuntu installed on your system. Docker can be installed on Ubuntu 20.04 (and newer versions), Ubuntu 18.04, and Ubuntu 16.04. Also, make sure you have sudo privileges as you’ll need them to perform the installation steps.

Understanding Docker Editions

There are two editions of Docker – Docker CE (Community Edition) and Docker EE (Enterprise Edition). Docker CE is a free and open-source containerization platform. It’s perfect for individual developers and small teams looking to get started with Docker and experimenting with container-based apps. Docker EE, on the other hand, is a premium version designed for enterprise development and is used by teams who build and run business-critical applications in production.

In this guide, we’ll be installing Docker CE as it’s the most common choice for first-time Docker users.

Step-by-step Installation Process

Related Article: Build a Chat Web App with Flask, MongoDB, Reactjs & Docker

1. Setting up the Docker repository

  • First, open the terminal and update the apt package index using the command: sudo apt-get update
  • Next, install packages to allow apt to use a repository over HTTPS with: sudo apt-get install apt-transport-https ca-certificates curl software-properties-common

2.  Installing Docker Engine

  • Add Docker’s official GPG key with: curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
  • Set up the stable repository. To add the nightly or test repository, add the word nightly or test (or both) after the word stable in the commands below: sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable
  •  Update the apt package index again, and then install the latest version of Docker Engine and containerd with: sudo apt-get update and then sudo apt-get install docker-ce docker-ce-cli containerd.io
  • Verify that Docker Engine is installed correctly by running the hello-world image with: sudo docker run hello-world

Post-installation Steps

Related Article: Build a Movie Search App with GraphQL, Node & TypeScript

1. Running Docker as a non-root user

  • If you want to avoid typing sudo whenever you run the docker command, add your username to the docker group with: sudo usermod -aG docker ${USER}
  • To apply the new group membership, you can log out of the server and back in, or type: su - ${USER}

2. Starting, stopping, and restarting Docker services

  • You can use systemctl to start, stop, and restart Docker services with: sudo systemctl start docker, sudo systemctl stop docker, and sudo systemctl restart docker

Working with Docker

With Docker installed, you can now pull images from the Docker repository and run containers. For example, you can pull the Ubuntu image with: docker pull ubuntu and then run an Ubuntu container with: docker run -it ubuntu

Related Article: Comparing Kubernetes vs Docker

Troubleshooting Common Docker Installation Issues

If you run into issues during the Docker installation process, the Docker community is an excellent resource. The Docker documentation also includes a troubleshooting section that may help. Common issues usually involve permissions, Docker service status, or network settings.

Additional Resources

For mormation on Docker and to explore its capabilities further, you can visit the official Docker documentation. For community support, check out Docker’s community page. You can also explore Docker’s vast collection of public images in the Docker Hub.

In the previous section, we completed the Docker installation on Ubuntu and discussed some troubleshooting tips. Now let’s dive deeper into using Docker.

Running Docker Containers

Running a Docker container is straightforward. Once you’ve pulled an image, you can start a container from that image by using the docker run command. For instance, if you want to run a container from the Ubuntu image, you would use the following command:

docker run -it ubuntu

The –it flag is used to start the container in interactive mode, and you’ll be taken straight to the container’s command line.

Related Article: Copying a Directory to Another Using the Docker Add Command

Managing Docker Containers

Docker provides several commands to manage your containers:

  • docker ps: Lists all running containers.
  • docker ps -a: Lists all containers, both running and stopped.
  • docker stop <container_id>: Stops a running container.
  • docker rm <container_id>: Removes a container.

Remember to replace <container_id> with your actual container’s ID.

Docker Images

Docker images are the building blocks of Docker containers. An image is a lightweight, standalone, and executable software package that includes everything needed to run a piece of software, including the code, a runtime, libraries, environment variables, and config files.

To list the Docker images that you have on your system, use the docker images command.

Pulling Docker Images

You can pull Docker images from Docker Hub, which is a cloud-based registry service that allows you to link to code repositories, build your images, and test them, store manually pushed images, and link to Docker Cloud.

To pull an image, use the docker pull <image_name> command. For example, to pull the latest official Ubuntu image, you would use the docker pull ubuntu command.

Related Article: Docker CLI Tutorial and Advanced Commands

Removing Docker Images

If you want to remove an image from your system, you can use the docker rmi <image_id> command. Be careful with this command because if any containers (running or stopped) were created from the image, they need to be removed first.

Docker Networking

By default, a Docker container is part of a default network on your system. But Docker also allows you to create custom networks and attach containers to more than one network. You can use the docker network create command to create a custom network.

Conclusion

You’ve successfully navigated the process of installing Docker on your Ubuntu system. With Docker now at your disposal, you’ve unlocked a world of efficiency and consistency for your application development and deployment.

Remember, the key to mastering Docker, like any tool, is regular practice and continuous learning. Don’t hesitate to explore the numerous Docker images available and try deploying different applications in Docker containers. Should you encounter any challenges along the way, the Docker community is always ready to assist.

You May Also Like

How to Use Environment Variables in Docker Compose

Using environment variables in Docker Compose for software configuration is a crucial skill for developers. This article provides a step-by-step guide on defining and... read more

Copying a Directory to Another Using the Docker Add Command

Copying directories in Docker using the Add command is a process that can be done in just a few steps. This article provides a step-by-step guide to help you understand... read more

How to Use the Host Network in Docker Compose

Setting up and using the host network for Docker Compose can be a valuable tool for optimizing your Docker containers. In this tutorial, we will explore two options for... read more

How to Mount a Host Directory as a Volume in Docker Compose

Mounting a host directory as a volume in Docker Compose is a process that can greatly enhance your containerized applications. This article provides two methods for... read more

How To List Containers In Docker

Listing containers in Docker is a fundamental task for managing your Docker environment. This article provides a basic guide on how to list containers using two... read more

How to Run a Docker Instance from a Dockerfile

Running a Docker instance from a Dockerfile is a fundamental skill for software engineers. This article provides a step-by-step guide on creating a Dockerfile, building... read more