1. Home
Docker

Understanding Docker: A Comprehensive Overview

Discover comprehensive Docker tutorials designed for all skill levels. Learn containerization, deployment, and best practices to enhance your development workflow.

  • 11
  • 4
right-top-arrow
3

Install Docker on Ubuntu: A Complete Guide

Updated on 29/08/2024527 Views

Introduction

Docker has positively impacted the way we develop and deploy software. Since its inception in 2013, Docker has become the go-to platform for containerization, with millions of developers using it to build, share, and run applications consistently across various environments. In fact, Docker is downloaded 13 billion times per month from the Docker Hub.

Docker streamlines and automates the process of deploying software within small, portable containers. These containers are isolated environments that contain everything an application needs to run, from the code and runtime to system tools and libraries. This isolation is similar to virtual machines, but containers are much more efficient in terms of resource usage and startup time.

Thanks to Docker’s client-server architecture and the powerful Docker daemon, managing these containers is both straightforward and efficient. Docker is popularly used with Ubuntu because Docker works exceptionally well with this popular Linux distribution and is straightforward to install.

In this article, you will learn how to install Docker on an Ubuntu system. I will guide you through each step of the installation process, I will also show you how to run and manage containers. We will also touch on Docker images.

Overview

Docker is a significant tool that simplifies the management of application processes by running them in isolated containers, which are similar to virtual machines but are more lightweight and efficient, relying on the host operating system for many resources. This makes them highly portable and resource-friendly, ideal for modern software development.

Prerequisites for Docker Installation on Ubuntu

To install Docker on Ubuntu in this tutorial, you will need the following:

1. Ubuntu 22.04 LTS Server Setup

First, you will need an Ubuntu 22.04 LTS server. It is important to perform some initial setup steps to enhance the security and usability of your server. Here is a quick rundown:

  • Login as Root on Your Ubuntu Server: Use the command ssh root@your_server_ip to log in as the root user.
  • Create a New User: Add a new user for daily tasks. For example, adduser yourusername.
  • Grant Administrative Privileges: Add your new user to the sudo group with usermod -aG sudo yourusername. This allows the user to run commands with superuser privileges by prefixing them with sudo.
  • Set up a Basic Firewall: Ensure your server is protected by configuring a basic firewall.

2. Account on Docker Hub

If you plan to create your own Docker images and drive them to the Docker Hub, you will need an account. Having a Docker Hub account allows you to store and share your Docker images easily. Setting up an account is straightforward:

  • Go to the Docker Hub website.
  • Sign Up and fill out your details.
  • Confirm your email address and log in.

Docker Installation on Ubuntu

There are two main ways to install Docker on Ubuntu: using the official Docker repository or using the default Ubuntu repositories. Let's go through both methods, starting with the preferred approach of using the official Docker repository.

1. Installing Docker from the Official Repository

  1. Update the Package Repository

sudo apt update

  1. Install Prerequisite Packages

Install packages that are necessary to enable apt to access repositories over HTTPS:

sudo apt install -y curl ca-certificates apt-transport-https software-properties-common

  1. Add Docker’s Official GPG Key

Add Docker's GPG key to ensure the packages are authentic:

sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 9DC858229FC7DD38854AE2D88D81803C0EBFCD88

  1. Manually add the Docker repository to the sources list

sudo sh -c 'echo "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" > /etc/apt/sources.list.d/docker.list'

  1. Update the Package Index Again

Refresh the package index to include the Docker packages:

sudo apt update

  1. Install Docker on Ubuntu

Install Docker CE (Community Edition):

sudo apt install docker-ce –y

  1. Verify the Installation

Check that you have installed Docker and it is running:

sudo systemctl status docker

You should see the output indicates Docker is active and running.

2. Installing Docker from the Default Ubuntu Repositories

If you prefer a simpler installation and do not need the latest version, you can install Docker from the default Ubuntu repositories.

  1. Update the Package Repository

Update your system's package index:

sudo apt update

  1. Install Docker on Ubuntu

Install Docker using the default repository:

sudo apt install docker.io -y

  1. Start and Enable Docker

Launch Docker and make it boot-up

sudo systemctl start docker

sudo systemctl enable docker

Or you can also install Docker Compose Ubuntu for running and definingmulti-container Docker applications with a single YAML file configuration.

  1. Add your user to Docker group (optional)

After running this command, you need to log out and back in for the changes to take effect.

sudo usermod -aG docker $USER

  1. Verify the Installation

Check Docker's status to ensure it is running:

sudo systemctl status docker

You can also check the Docker version:

docker --version

You should see something like Docker version [latest_version_number], build [build_number].

Using Docker Commands to Test After Docker Installation

After Docker is installed, start using Docker commands to manage containers.

1. Running a Test Container

To ensure Docker is set up correctly, you can run a simple test container:

docker run hello-world

If Docker is working properly, you will see something like this:

Unable to find image 'hello-world:latest' locally

latest: Pulling from library/hello-world

1a7a86c55b9b: Pull complete

Digest: sha256:67c7c4f8ee7bb1ff729d7a5e9f6c30e828d7a14f7e0dd7a20e73c4818e6e4c4d

Status: Downloaded newer image for hello-world:latest

Hello from Docker!

It looks from this message that everything is operating as it should with your installation.

Docker could not find the hello-world image locally, so it pulled it from Docker Hub, the default repository. Once downloaded, Docker created and ran a container from this image, displaying the welcome message.

2. List Running Containers

docker ps

Working with Docker Images After Installation

Once Docker is installed on your Ubuntu system, the next step is to work with Docker images. Docker containers are created from these images, and by default, Docker pulls them from Docker Hub, a popular repository for Docker images. Here is how you can manage Docker images effectively.

1. Searching for Images on Docker Hub

Search for images that are available on Docker Hub by using the docker search command. For instance, perform the following search for the Ubuntu image:

docker search ubuntu

You will get a list of available images:

Name

Description

Stars

Official

Automated

Ubuntu

A Debian-based Linux operating system.

11000

[OK]

dorowu/ubuntu-desktop-lxde-vnc

Provides HTML5 VNC interface for Ubuntu desktop.

450

[OK]

rastasheep/ubuntu-sshd

Dockerized SSH service built on top of official Ubuntu.

250

[OK]

...

...

...

...

...

The OFFICIAL column indicates images that are officially maintained by the Docker team.

2. Pulling an Image

Once you have identified an image you want, download it using the docker pull command. Taking the official Ubuntu image as an example, download it:

docker pull ubuntu

You will see the following output:

Using default tag: latest

latest: Pulling from library/ubuntu

6e1bee0e3d6a: Pull complete

b1c53e5aeb0d: Pull complete

Status: Downloaded newer image for ubuntu:latest

docker.io/library/ubuntu:latest

3. Listing Downloaded Images

To see which images are downloaded to your system, use the docker images command:

docker images

The output will list all downloaded images:

REPOSITORY

TAG

IMAGE ID

CREATED

SIZE

Ubuntu

latest

6e1bee0e3d6a

2 weeks ago

72.9MB

hello-world

latest

b1c53e5aeb0d

5 months ago

13.2kB

4. Running a Container from an Image

With an image downloaded, you can run a container using the docker run command. For instance, to run a container with the Ubuntu image and get a bash shell:

docker run -it ubuntu bash

This command starts a new container from the Ubuntu image and opens an interactive terminal session.

Running and Managing a Docker Container After Installation

1. Running a Container

Run an Interactive Ubuntu Container:

docker run -it ubuntu

Your prompt will change to indicate you are inside the container:

root@2c4b6f7c8a2d:/#

Update Package Database:

apt update

Install an Application (e.g., curl):

apt install curl

Verify Installation:

curl --version

Example Output:

curl 7.68.0 (x86_64-pc-linux-gnu) libcurl/7.68.0

Exit the Container:

Exit

2. Managing Docker Containers

List Active Containers:

docker ps

List All Containers (Including Stopped):

docker ps -a

Example Output:

CONTAINER ID

IMAGE

COMMAND

CREATED

STATUS

NAMES

2c4b6f7c8a2d

ubuntu

/bin/bash

5 minutes ago

Exited (0) About a minute ago

focused_turing

View Most Recent Container:

docker ps -l

Starting and Stopping Containers

Start a Container:

docker start 2c4b6f7c8a2d

Stop a Running Container:

docker stop focused_turing

Removing Containers

Remove a Container:

docker rm epic_morse

Running a Named Container

Run a Container with a Custom Name:

docker run --name my_ubuntu -it ubuntu

Automatically Remove Container After Exit:

docker run --rm -it ubuntu

Making Changes in a Container to a Docker Image and Pushing Docker Images to a Repository

When you start a Docker container, you have to create, modify, and delete files like you do in a virtual machine. These changes are specific to that container and will be lost once the container is removed with the docker rm command. To preserve your changes, you can commit the container state to a new Docker image.

1. Committing Changes to a New Image

Make Changes Inside a Container:

docker run -it ubuntu

Install Node.js as an example:

apt update && apt install -y nodejs

Verify the installation:

node -v

Example output:

v10.19.0

Commit the Container to a New Image:

docker commit -m "Added Node.js" -a "Your Name" container_id your_username/ubuntu-nodejs

Replace container_id with your container’s ID. Example:

docker commit -m "Added Node.js" -a "David" 3b5e72d1bb7e sammy/ubuntu-nodejs

Verify the New Image:

docker images

Example output:

REPOSITORY

TAG

IMAGE ID

CREATED

SIZE

David/ubuntu-nodejs

latest

c2c95d5c68a3

10 seconds ago

150MB

Pushing the Image to Docker Hub

Log in to Docker Hub:

docker login -u your_username

Enter your Docker Hub password when prompted.

Tag the Image (if necessary): If your Docker Hub username differs from the local username:

docker tag sammy/ubuntu-nodejs your_username/ubuntu-nodejs

Push the Image to Docker Hub:

docker push your_username/ubuntu-nodejs

Example Output:

The push refers to repository [docker.io/sammy/ubuntu-nodejs]

e3fbbfb44187: Pushed

5f70bf18a086: Pushed

...

Final Thoughts

Installing Docker on Ubuntu is a straightforward process that unlocks a powerful tool for modern software development and deployment. Docker containers provide isolated, efficient environments to run your applications, similar to virtual machines but with less overhead.

When you follow the steps outlined in this article to install Docker on Ubuntu, you can quickly set up Docker on your Ubuntu system. You can also run and manage containers, and you can even commit and share your custom container images.

There is no doubt that with Docker, you are well-equipped to handle the complexities of modern software development, and this will ensure your applications run smoothly and consistently no matter where they are deployed.

FAQs

1. How do I install Docker on Ubuntu?

Use the official Docker repository to install Docker on Ubuntu. First, update your package list with sudo apt update, then install prerequisites with sudo apt install apt-transport-https ca-certificates curl software-properties-common. Add Docker’s GPG key and repository, then update your package list again and install Docker with sudo apt install docker-ce.

2. How to install an Ubuntu VM Docker?

To install Docker inside an Ubuntu VM, follow the same steps as you would on a regular Ubuntu system: update your package list, install prerequisites, add Docker’s GPG key and repository, update again, and finally install Docker with sudo apt install docker-ce.

3. How to run Docker in Docker Ubuntu?

Running Docker within Docker (Docker-in-Docker) can be achieved by starting a container with Docker privileges. Use the command docker run --privileged -d --name dind docker:dind to start a Docker container that can run Docker itself.

4. How to install and setup Docker?

First, update your system’s package list. Install the necessary prerequisites, add Docker’s official GPG key, and set up the Docker repository. After that, install Docker with sudo apt install docker-ce. Finally, start the Docker service and enable it to start on boot with sudo systemctl start docker and sudo systemctl enable docker.

5. Which command is used to install Docker?

The command to install Docker in Ubuntu is sudo apt install docker-ce.

6. How to use Docker step by step?

  • Install Docker on your system.
  • Run docker run hello-world to verify the installation.
  • Pull an image with docker pull image_name.
  • Start a container with docker run -it image_name.
  • Manage containers with commands like docker ps, docker stop, and docker rm.

7. How to install Docker in Linux step by step?

  • Update your package list: sudo apt update.
  • Install the required packages: sudo apt install apt-transport-https ca-certificates curl software-properties-common.
  • Add Docker’s GPG key: curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -.
  • Add the Docker repository: sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable".
  • Update your package list again: sudo apt update.
  • Install Docker: sudo apt install docker-ce.

8. What is Docker used for?

Docker is used for developing, shipping, and running applications in isolated environments called containers.

9. How to install and run Docker on Linux?

To install Docker, follow the steps to update your package list, install prerequisites, add Docker’s GPG key and repository, update again, and install Docker with sudo apt install docker-ce. Start and enable Docker with sudo systemctl start docker and sudo systemctl enable docker. Then, run a test container with docker run hello-world to ensure everything is set up correctly.

Rohan Vats

Rohan Vats

Passionate about building large scale web apps with delightful experiences. In pursuit of transforming engineers into leaders.

Talk to Career Expert
form image
+91
*
By clicking, I accept theT&Cand
Privacy Policy
image
Join 10M+ Learners & Transform Your Career
Learn on a personalised AI-powered platform that offers best-in-class content, live sessions & mentorship from leading industry experts.
right-top-arrowleft-top-arrow

upGrad Learner Support

Talk to our experts. We’re available 24/7.

text

Indian Nationals

1800 210 2020

text

Foreign Nationals

+918045604032

Disclaimer

upGrad does not grant credit; credits are granted, accepted or transferred at the sole discretion of the relevant educational institution offering the diploma or degree. We advise you to enquire further regarding the suitability of this program for your academic, professional requirements and job prospects before enr...