Docker on Ubuntu 20.04: Installation and Usage Guide


6 min read 14-11-2024
Docker on Ubuntu 20.04: Installation and Usage Guide

Docker has revolutionized the way we think about application deployment and management. Its unique ability to package applications and their dependencies into standardized units called containers has made it a go-to tool for developers and system administrators alike. In this comprehensive guide, we’ll walk you through the installation process of Docker on Ubuntu 20.04, delve into its essential features, and showcase how to use Docker effectively. We aim to provide you with a rich understanding of Docker that builds your expertise and confidence in using it.

What is Docker?

Docker is an open-source platform designed for developing, shipping, and running applications in a lightweight manner using containers. Unlike traditional virtual machines, containers share the host OS kernel and isolate the application processes, resulting in faster start-up times, lower overhead, and more efficient resource utilization.

Key Features of Docker

  • Isolation: Each container runs in its own environment, which prevents conflicts between applications.
  • Portability: Docker containers can run on any system that supports Docker, regardless of the underlying infrastructure.
  • Scalability: Deploy and scale applications quickly without worrying about the underlying hardware.
  • Version Control: Docker images can be versioned, allowing developers to roll back to previous versions if necessary.

By leveraging these features, Docker simplifies the development and deployment workflow, making it a preferred choice for cloud-native applications.

System Requirements for Docker on Ubuntu 20.04

Before diving into the installation process, let's ensure your system meets the minimum requirements:

  • Operating System: Ubuntu 20.04 (Focal Fossa) or higher.
  • Architecture: 64-bit (x86).
  • Kernel Version: At least 3.10. If you're unsure about your kernel version, you can check it by running uname -r in your terminal.
  • RAM: Minimum of 2 GB (4 GB recommended).
  • Disk Space: At least 10 GB of free disk space.

Installing Docker on Ubuntu 20.04

Now that we've established what Docker is and the system requirements, let’s get our hands dirty with the installation process.

Step 1: Update Your System

Before you begin, it's always a good practice to update your system. Open your terminal and execute the following commands:

sudo apt update
sudo apt upgrade

Step 2: Install Required Packages

Docker relies on a few packages that allow apt to use packages over HTTPS. To install them, run:

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

Step 3: Add Docker’s Official GPG Key

Next, you need to add Docker’s official GPG key to ensure the software's authenticity. Use the command:

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

Step 4: Add Docker’s Repository

Next, you'll add Docker's official repository. Run the following command:

sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"

Step 5: Install Docker Engine

Once the repository is added, update the package index again and install Docker:

sudo apt update
sudo apt install docker-ce

Step 6: Verify Docker Installation

To confirm that Docker is installed successfully, run:

sudo systemctl status docker

You should see an output indicating that Docker is active (running). If you want to see the Docker version installed, use:

docker --version

Step 7: Manage Docker as a Non-root User

By default, Docker commands must be run with sudo. To allow your user account to run Docker commands without sudo, add your user to the docker group:

sudo usermod -aG docker ${USER}

You’ll need to log out and log back in for the changes to take effect. You can verify that your user is in the docker group by running:

groups

Using Docker on Ubuntu 20.04

Now that Docker is installed, let’s explore some basic commands and usage scenarios.

Running Your First Docker Container

To get started, let’s pull a simple Docker image and run it. The following command pulls the “hello-world” image from Docker Hub:

docker run hello-world

This command performs several actions:

  1. Checks for the Image: Docker looks for the image locally on your machine.
  2. Pulls the Image: If it’s not found locally, Docker downloads the image from Docker Hub.
  3. Runs the Container: Once downloaded, Docker creates a container based on that image and executes the application inside it.

Understanding Docker Images and Containers

  • Docker Image: A lightweight, standalone, executable package containing everything needed to run a piece of software, including the code, libraries, dependencies, and runtime.
  • Docker Container: An instance of a Docker image that runs in a separate environment.

To view all available Docker images on your system, use:

docker images

And to see the running containers, run:

docker ps

Managing Docker Containers

You can start, stop, and remove Docker containers with the following commands:

  • Start a Container:
docker start <container_id>
  • Stop a Container:
docker stop <container_id>
  • Remove a Container:
docker rm <container_id>

Docker Networking

Docker enables communication between containers through its built-in networking features. You can create custom networks and connect containers to them. For example, to create a new bridge network, use:

docker network create my-network

Docker Volumes for Data Persistence

Data stored in a container is lost when the container is removed. To prevent data loss, you can use Docker volumes. Create a volume with the following command:

docker volume create my-volume

You can then mount this volume to a container:

docker run -d -v my-volume:/data --name my-container my-image

Advanced Docker Usage

As you become more familiar with Docker, you'll find additional functionalities to enhance your workflow.

Docker Compose

Docker Compose is a tool that allows you to define and run multi-container Docker applications. Using a docker-compose.yml file, you can configure your application services in one place.

Here’s a simple example of a docker-compose.yml file:

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

To deploy the application, run:

docker-compose up

Docker Hub and Image Management

Docker Hub is the default registry for Docker images, where you can find thousands of pre-built images. You can push your custom images to Docker Hub for easy access.

To log in to Docker Hub, use:

docker login

After logging in, you can tag and push your images:

docker tag my-image username/my-image
docker push username/my-image

Monitoring and Logging

Monitoring Docker containers can be crucial for maintaining application health. Docker provides built-in logging mechanisms that you can use to monitor logs:

docker logs <container_id>

For advanced monitoring, consider using third-party tools like Prometheus or Grafana to visualize container metrics.

Troubleshooting Common Docker Issues

  1. Docker Daemon Not Running: If you encounter errors like "Cannot connect to the Docker daemon," ensure the Docker service is running. You can start it with:

    sudo systemctl start docker
    
  2. Permission Denied: If you see "permission denied" errors when running Docker commands, ensure you added your user to the docker group correctly and logged out and back in.

  3. Disk Space Issues: Docker can consume significant disk space. To remove unused images, containers, and volumes, run:

    docker system prune
    

Conclusion

In this guide, we have taken a deep dive into Docker on Ubuntu 20.04, from installation to basic usage and advanced techniques. Docker empowers developers to create, deploy, and manage applications with unprecedented efficiency. By mastering Docker, you're setting yourself up for success in the modern world of DevOps and cloud computing.

As you continue your Docker journey, remember that practice makes perfect. Experiment with different images, create your own Dockerfiles, and explore Docker Compose. The more you use it, the more you'll understand the vast capabilities Docker offers.

Frequently Asked Questions (FAQs)

1. What is the difference between Docker and a virtual machine?

Docker containers share the host operating system's kernel, while virtual machines run a complete guest operating system on top of hypervisor technology. Containers are more lightweight and start faster compared to virtual machines.

2. Can I run Docker on a non-Linux OS?

Yes, while Docker is primarily built for Linux, you can run Docker on Windows and macOS using Docker Desktop, which utilizes a lightweight virtual machine to run Linux containers.

3. How do I remove Docker from Ubuntu?

To uninstall Docker from Ubuntu, you can use the following commands:

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

4. What are Dockerfiles?

Dockerfiles are scripts that contain a series of commands and instructions to create a Docker image. They define what goes into an image and how the application should run.

5. How can I secure my Docker containers?

To secure your Docker containers, you can follow best practices such as minimizing privileges, using trusted images, regularly updating images, and implementing network segmentation.

With this knowledge, you are well-equipped to explore the world of Docker and leverage its capabilities for efficient application management and deployment. Happy Dockering!