How to Install Docker on Ubuntu Server: A Comprehensive Guide

Docker has revolutionized application deployment by using containerization, making it simpler to manage applications across different environments. Ubuntu Server, known for its stability and wide adoption, is a popular choice for hosting Docker. This guide provides a detailed walkthrough on how to Install Docker On Ubuntu Server, ensuring you can quickly get started with containerization.

Prerequisites for Docker Installation

Before you begin with the installation process, ensure that you have the following:

  • An Ubuntu Server: This guide is tailored for Ubuntu Server. Ensure you have a running instance of Ubuntu Server.
  • User with sudo privileges: You need a user account with sudo privileges to execute administrative commands.
  • Internet connection: Docker installation requires downloading packages from online repositories.

Step-by-Step Installation of Docker on Ubuntu Server

Here’s how to install Docker on Ubuntu Server:

1. Update Your Package Index

It’s always a good practice to start by updating your server’s package index. This ensures you install the latest versions of packages and their dependencies. Open your terminal and run:

sudo apt-get update

This command refreshes the package lists, ensuring you have the most up-to-date information on available packages.

2. Install Prerequisites

Docker requires a few prerequisite packages to be installed on your system. Install them using the following command:

sudo apt-get install apt-transport-https ca-certificates curl software-properties-common
  • apt-transport-https: Allows apt to access repositories over HTTPS.
  • ca-certificates: Installs CA certificates, allowing HTTPS-based repositories to be verified.
  • curl: A tool to transfer data from or to a server, used here to fetch the Docker GPG key.
  • software-properties-common: Provides scripts to manage your software sources.

3. Add Docker’s Official GPG Key

To ensure the authenticity of the Docker packages, you need to add Docker’s GPG key to your system.

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

This command downloads the Docker GPG key and adds it to your system’s keyring.

4. Set Up the Docker Repository

Next, you need to add the Docker repository to your APT sources list. This allows apt to find and install Docker packages from Docker’s official 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

This command adds the Docker repository to your system’s APT sources.

5. Install Docker Engine

Now you are ready to install Docker Engine, containerd, and Docker Compose. Update the package index again to include the new Docker repository, and then install Docker Engine:

sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-compose-plugin
  • docker-ce: Installs Docker Community Edition.
  • docker-ce-cli: Installs the Docker CLI, allowing you to interact with Docker.
  • containerd.io: Container runtime.
  • docker-compose-plugin: Docker Compose plugin for defining and running multi-container Docker applications.

During the installation, you might be prompted to confirm the installation; type Y and press Enter.

6. Verify Docker Installation

After the installation is complete, verify that Docker is installed correctly by checking the Docker version:

docker --version

This command should output the Docker version, confirming successful installation.

You can also check the Docker service status:

sudo systemctl status docker

This command shows the status of the Docker service. It should be active and running.

To further verify the installation, run the hello-world container:

sudo docker run hello-world

This command downloads and runs a test image, confirming that Docker is working correctly. You should see a message from hello-world in your terminal if everything is set up correctly.

Understanding Docker with Ubuntu Image

Now that Docker is installed on your Ubuntu Server, it’s important to understand how Docker images work, especially when using Ubuntu as a base image.

When you use the instruction FROM ubuntu in your Dockerfile, you are essentially starting your Docker image with pre-built layers that include parts of a standard Ubuntu file system and packages.

Alt text: Diagram illustrating Docker image layers, showing how each command in a Dockerfile adds a new layer on top of the base image, optimizing storage and deployment.

Think of a Docker image as being built in layers. Each command in your Dockerfile adds a new layer. For example, consider a simple Dockerfile:

FROM ubuntu:latest

RUN apt-get update && apt-get install -y nginx

COPY index.html /var/www/html/

CMD ["nginx", "-g", "daemon off;"]
  • FROM ubuntu:latest: This line sets the base image to the latest version of Ubuntu. Docker pulls this image as the base layer.
  • RUN apt-get update && apt-get install -y nginx: This command adds a new layer on top of the Ubuntu base image by updating the package list and installing Nginx.
  • COPY index.html /var/www/html/: This line adds another layer, copying your index.html file into the Nginx default web directory.
  • CMD ["nginx", "-g", "daemon off;"]: This sets the command to run when the container starts, which is starting Nginx.

Each RUN, COPY, ADD command results in a new layer in your Docker image. This layered architecture is one of Docker’s core features, optimizing storage and deployment by reusing layers across images.

Basic Docker Commands and Usage on Ubuntu Server

Here are a few basic Docker commands to get you started on your Ubuntu Server:

Building Docker Images

To build a Docker image from a Dockerfile in your current directory, use:

docker build -t my-ubuntu-nginx-image .
  • docker build: The command to build a Docker image.
  • -t my-ubuntu-nginx-image: Tags your image with the name my-ubuntu-nginx-image.
  • .: Specifies the current directory as the build context, where the Dockerfile is located.

Running Docker Containers

To run a container from the image you just built:

docker run -d -p 80:80 my-ubuntu-nginx-image
  • docker run: The command to run a container.
  • -d: Runs the container in detached mode (in the background).
  • -p 80:80: Maps port 80 on the host to port 80 in the container, allowing you to access the Nginx server from your browser via the server’s IP address.
  • my-ubuntu-nginx-image: Specifies the image to use for creating the container.

Managing Docker Containers

To list running containers:

docker ps

To list all containers (running and stopped):

docker ps -a

To stop a running container:

docker stop <container_id>

Replace <container_id> with the actual container ID from docker ps.

Conclusion

You have now successfully installed Docker on your Ubuntu Server and have a basic understanding of how Docker images work with Ubuntu. This setup allows you to start containerizing applications, manage them efficiently, and ensure consistency across different environments. Docker on Ubuntu Server is a powerful combination for modern application deployment and management. Explore further Docker commands and concepts to unlock the full potential of containerization in your projects.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

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