Linux

How To Install and Use Docker on Ubuntu

How To Install and Use Docker on Ubuntu

Docker is a popular open-source platform that automates the deployment, scaling, and management of applications in lightweight containers. It simplifies development workflows by allowing developers to package their applications and dependencies into containers that can run consistently across different environments. In this guide, we will walk you through the process of installing Docker on Ubuntu, and provide an overview of how to use Docker to create and manage containers.

Why Use Docker?

Before diving into the installation process, it’s important to understand why Docker is so widely used. Here are some key benefits:

Portability: Docker containers can run on any system that supports Docker, ensuring consistency across development, testing, and production environments.
Efficiency: Containers use fewer resources than virtual machines, as they share the same OS kernel.
Scalability: Docker simplifies scaling by making it easy to deploy multiple containers and manage them in a cluster.
Speed: Containers start quickly and allow faster iteration during development.

Prerequisites

Before installing Docker, make sure you have the following:

Ubuntu 18.04, 20.04, or newer: This guide is tailored for Ubuntu, but Docker can be installed on other Linux distributions as well.
A sudo user: You’ll need root access to install Docker and manage containers.
Internet access: Docker packages will be downloaded from the official repository.

Step 1: Update Your System

Before installing any software, it’s always a good practice to ensure that your system is up to date. Open the terminal and run the following commands to update your package index and upgrade installed packages:

sudo apt update
sudo apt upgrade

This ensures that your system has the latest security patches and software versions.

Step 2: Install Docker Dependencies

Docker requires a few additional packages for it to run smoothly. Install them by running:

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

These packages ensure that Ubuntu can download Docker from its official repository and verify the downloaded packages.

Step 3: Add Docker’s Official GPG Key

Next, we need to add Docker’s official GPG key to verify the authenticity of the Docker packages. Use the following command:

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

This will download the GPG key and store it locally.

Step 4: Add Docker’s APT Repository

With the GPG key in place, add Docker’s repository to your system’s software sources by running:

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

This will allow you to install Docker from the official repository, ensuring you get the latest stable version.

Step 5: Install Docker Engine

Now that the repository has been added, update your package index again to include Docker’s packages:

sudo apt update

Next, install Docker Engine:

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

Docker CE (Community Edition) is the free and open-source version of Docker. The docker-ce-cli is the command-line interface for Docker, and containerd.io is the container runtime.

Step 6: Start and Enable Docker

After installation, start the Docker service and ensure it starts automatically at boot:

sudo systemctl start docker
sudo systemctl enable docker

To verify that Docker is running, use the following command:

sudo systemctl status docker

You should see a status message indicating that Docker is active and running.

Step 7: Verify Docker Installation

To verify that Docker was installed successfully, run the following command:

sudo docker –version

This should display the Docker version installed on your system.

To further ensure everything is working correctly, run the hello-world Docker container, which is a test container provided by Docker:

sudo docker run hello-world

If Docker is installed correctly, this command will download the hello-world image, run it in a container, and output a message confirming the installation.

Step 8: Managing Docker as a Non-Root User (Optional)

By default, Docker commands must be run with sudo for root access. To avoid having to use sudo with every Docker command, you can add your user to the docker group:

sudo usermod -aG docker $USER

After adding your user to the docker group, log out and back in for the changes to take effect. You can now run Docker commands without sudo.

Using Docker: Basic Commands
Now that Docker is installed, let’s cover some basic Docker commands.

1. Pull a Docker Image

A Docker image is a pre-configured package containing the application and its dependencies. To download a Docker image from the Docker Hub repository, use the docker pull command:

docker pull ubuntu

This command downloads the official Ubuntu image.

2. List Docker Images

To view all the images on your system, run:

docker images

This will list all the available Docker images, their tags, and their sizes.

3. Run a Docker Container

Once you have an image, you can create and run a container using the docker run command. For example, to run an Ubuntu container:

docker run -it ubuntu

The -it option ensures you get an interactive terminal, so you can interact with the Ubuntu container. Once inside the container, you can run commands like you would on a regular Ubuntu system.

4. List Running Containers

To view all running containers, use:

docker ps

If you want to see all containers, including those that have stopped, use:

docker ps -a

5. Stop and Remove Containers

To stop a running container, use the docker stop command followed by the container ID or name:

docker stop

To remove a stopped container, use:

docker rm

Conclusion

Docker is a powerful tool that allows you to develop, deploy, and manage applications in lightweight containers, making it a must-have for developers and system administrators alike. In this guide, we’ve covered the installation process for Docker on Ubuntu, along with some basic commands to help you get started. With Docker, you can ensure consistency across different environments, streamline your development workflow, and deploy applications with ease.

By following the steps outlined in this guide, you’ll be able to install Docker on your Ubuntu system and start managing containers efficiently.

Related Articles

Leave a Reply

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

Back to top button