CloudContainersHow ToTech/Web

How To Install and Use Docker on Ubuntu

Introduction

Docker is a popular containerization platform that allows developers to package applications and their dependencies into portable containers. Containers enable consistent development, testing, and deployment across different environments. In this guide, we will cover how to install and use Docker on Ubuntu, including managing containers, images, and Docker Compose.

Prerequisites

Before installing Docker, ensure that:

  • You have an Ubuntu system (Ubuntu 20.04, 22.04, or later recommended).
  • You have a user account with sudo privileges.
  • The system is updated.

Step 1: Updating Your System

It is always a good practice to update your system before installing new software. Run:

sudo apt update && sudo apt upgrade -y

Step 2: Installing Docker

2.1 Install Required Dependencies

Before installing Docker, install necessary dependencies:

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

2.2 Add Docker’s Official GPG Key

Docker provides an official repository. Add its GPG key for security:

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg

2.3 Add the Docker Repository

Next, add the stable Docker repository:

echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

2.4 Install Docker Engine

Now, update the package list and install Docker:

sudo apt update
sudo apt install docker-ce docker-ce-cli containerd.io -y

2.5 Verify Docker Installation

Check if Docker is installed correctly by running:

docker --version

It should output the installed Docker version.

To check if the Docker service is running:

sudo systemctl status docker

If it’s not running, start Docker:

sudo systemctl start docker

To enable Docker to start on boot:

sudo systemctl enable docker

Step 3: Running Docker Without Sudo

By default, Docker requires sudo to run. To allow your user to run Docker commands without sudo, add it to the docker group:

sudo usermod -aG docker $USER

Then log out and log back in for the changes to take effect.

Step 4: Using Docker

4.1 Running Your First Container

To test Docker, run:

docker run hello-world

This downloads a test image and runs a simple container to confirm that Docker is working correctly.

4.2 Searching for Docker Images

To find available Docker images, use:

docker search ubuntu

4.3 Pulling an Image

To download an image from Docker Hub, run:

docker pull ubuntu

4.4 Running a Container

To run a container from an image:

docker run -it ubuntu /bin/bash

This starts an interactive shell inside an Ubuntu container.

4.5 Listing Running Containers

To view running containers, use:

docker ps

To view all containers (including stopped ones):

docker ps -a

4.6 Stopping and Removing Containers

To stop a running container:

docker stop <container_id>

To remove a container:

docker rm <container_id>

Step 5: Managing Docker Images

5.1 Listing Images

To see all downloaded images, use:

docker images

5.2 Removing an Image

To delete an image:

docker rmi <image_id>

Step 6: Using Docker Compose

Docker Compose is a tool for managing multi-container applications.

6.1 Installing Docker Compose

Run:

sudo apt install docker-compose -y

Verify the installation:

docker-compose --version

6.2 Creating a docker-compose.yml File

Create a directory for your application:

mkdir myapp && cd myapp

Create a docker-compose.yml file:

touch docker-compose.yml

Open the file with a text editor and add:

version: '3'
services:
  web:
    image: nginx:latest
    ports:
      - "8080:80"

6.3 Running a Multi-Container Application

To start the containers:

docker-compose up -d

To stop the containers:

docker-compose down

Step 7: Uninstalling Docker

If you need to remove Docker, run:

sudo apt purge docker-ce docker-ce-cli containerd.io -y
sudo rm -rf /var/lib/docker

Conclusion

Docker simplifies application deployment by providing containerized environments that work across different systems. This guide covered installing and using Docker on Ubuntu, from running basic containers to managing Docker Compose applications. With Docker, developers can build, ship, and run applications more efficiently.

Harshvardhan Mishra

Hi, I'm Harshvardhan Mishra. Tech enthusiast and IT professional with a B.Tech in IT, PG Diploma in IoT from CDAC, and 6 years of industry experience. Founder of HVM Smart Solutions, blending technology for real-world solutions. As a passionate technical author, I simplify complex concepts for diverse audiences. Let's connect and explore the tech world together! If you want to help support me on my journey, consider sharing my articles, or Buy me a Coffee! Thank you for reading my blog! Happy learning! Linkedin

Leave a Reply

Your email address will not be published. Required fields are marked *