How to Install Docker on Ubuntu Server: A Comprehensive Guide

Docker has revolutionized the way applications are deployed and managed by using containers. These containers offer an efficient and isolated environment for running applications, making them incredibly useful for developers and system administrators alike. If you’re looking to leverage Docker on your Ubuntu server, this guide will walk you through the complete installation process and get you started with using Docker. We will cover everything from installing Docker to managing images and containers, ensuring you have a solid foundation for your containerization journey on Ubuntu server.

Prerequisites

Before you begin the Docker installation on your Ubuntu server, ensure you have the following:

  • An Ubuntu server (version 20.04 or later is recommended).
  • A user account with sudo privileges.
  • A stable internet connection to download packages.

Step 1 — Installing Docker Engine on Ubuntu Server

The Docker package available in Ubuntu’s default repositories might not be the latest version. For the most up-to-date features and security patches, it’s best to install Docker Engine from Docker’s official repository. This involves adding Docker’s repository to your system’s package sources, importing their GPG key to verify package authenticity, and then installing the Docker packages.

First, update your server’s package index to ensure you have the latest package information:

sudo apt update

Next, install prerequisite packages that allow apt to use repositories over HTTPS. These packages are essential for securely downloading packages and verifying their authenticity:

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

Now, add Docker’s official GPG key to your system. This key is used to ensure that the packages you download from the Docker repository are valid and haven’t been tampered with:

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

With the GPG key added, you can now add the Docker repository to your APT sources list. This tells your Ubuntu server where to find the Docker packages. For Ubuntu 20.04 (Focal Fossa), use the following command:

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 command adds the Docker repository for the ‘stable’ channel. After adding the repository, update the package index again to include the Docker packages from the newly added repository:

sudo apt update

To verify that you are installing from the Docker repository and not the default Ubuntu repository, you can use the apt-cache policy command:

apt-cache policy docker-ce

The output should indicate that the installation candidate is from the Docker repository. It will look similar to this (version numbers may vary):

docker-ce:
  Installed: (none)
  Candidate: 5:24.0.5-1~ubuntu.20.04~focal
  Version table:
    5:24.0.5-1~ubuntu.20.04~focal 500
        https://download.docker.com/linux/ubuntu focal/stable amd64 Packages

Finally, install Docker Engine, containerd, and Docker Compose using the following command:

sudo apt install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

This command installs Docker Engine, the Docker CLI, containerd (the container runtime), Docker Buildx plugin, and Docker Compose plugin. Once the installation is complete, Docker service should start automatically.

Verify that Docker Engine is installed correctly by checking the status of the Docker service:

sudo systemctl status docker

A successful installation will show the Docker service as active and running, similar to the output below:

● docker.service - Docker Application Container Engine
     Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
     Active: active (running) since Mon 2023-10-23 14:30:00 UTC; 30s ago
       Docs: https://docs.docker.com
   Main PID: 1234 (dockerd)
      Tasks: 8
     Memory: 50.0M
     CGroup: /system.slice/docker.service
             └─1234 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock

With Docker Engine installed, you now have the Docker daemon (the background service) and the Docker CLI (docker command) available on your Ubuntu server.

Step 2 — Managing Docker as a Non-Root User (Optional)

By default, Docker commands can only be executed by the root user or a user in the docker group. The docker group is automatically created during the installation process. Running Docker commands with sudo each time can be inconvenient. To run Docker commands without sudo, add your user account to the docker group.

Add your username to the docker group using the usermod command:

sudo usermod -aG docker $USER

For the changes to take effect, you need to log out and log back in to your server session, or apply the group membership to your current session using the following command:

su - $USER

You might be prompted to enter your user password.

Verify your user is now in the docker group by running the groups command:

groups

The output should list docker among the groups your user belongs to, like this:

yourusername sudo docker

From this point forward, you can execute Docker commands without prepending sudo. If you need to add a user to the docker group who is not the current user, specify the username explicitly:

sudo usermod -aG docker username

The rest of this tutorial assumes you are running Docker commands as a user in the docker group. If you choose not to, remember to prepend sudo to your Docker commands.

Step 3 — Getting Familiar with Docker Commands

The Docker CLI provides a powerful interface for managing containers, images, and more. Docker commands follow this general structure:

docker [option] [command] [arguments]

To see a list of available Docker commands, simply type docker in your terminal:

docker

This will display a comprehensive list of Docker commands, including commands for managing containers, images, volumes, networks, and more. For detailed help on any specific command, use the --help flag:

docker command --help

For example, to get help on the run command, type:

docker run --help

To get system-wide information about your Docker installation, use the info command:

docker info

This command provides details about the Docker server, containers, images, storage drivers, and other useful information for debugging and understanding your Docker environment.

Let’s explore some essential Docker commands by working with Docker images.

Step 4 — Working with Docker Images

Docker images are the building blocks of containers. They are read-only templates used to create containers. Docker Hub (hub.docker.com) is the default public registry for Docker images, hosting a vast collection of images from official sources and community contributors.

To test your Docker installation and verify you can pull images from Docker Hub, run the hello-world image:

docker run hello-world

This command does the following:

  1. Checks locally: Docker first checks if the hello-world image is present on your server.
  2. Pulls from Docker Hub: If the image is not found locally, Docker pulls it from Docker Hub.
  3. Creates and starts a container: Docker then creates a container from the hello-world image and starts it.
  4. Runs the application: The hello-world container executes a simple application that prints a greeting message.

The output confirms that Docker is working correctly and you can pull images from Docker Hub:

Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
0e03bdcc26d7: Pull complete
Digest: sha256:6a65f928fb91fcfbc963f7aa6d57c8eeb426ad9a20c7ee045538ef34847f44f1
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon then streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can deploy a web application by following part 2 of this guide.

To search for images on Docker Hub, use the docker search command. For example, to search for Ubuntu images:

docker search ubuntu

This command queries Docker Hub and returns a list of images matching the search term “ubuntu,” along with information like description, stars (popularity), official status, and automation status. The [OK] in the OFFICIAL column indicates images officially maintained by the respective projects.

To download a specific image to your server, use the docker pull command. For example, to download the official Ubuntu 20.04 image:

docker pull ubuntu:20.04

This command pulls the Ubuntu 20.04 image from Docker Hub to your local server. You can specify image tags (like :20.04) to pull specific versions, or omit the tag to pull the latest version.

To list images downloaded to your server, use the docker images command:

docker images

The output shows a list of locally stored Docker images, including repository name, tag, image ID, creation date, and size.

Step 5 — Running Docker Containers

Once you have Docker images, you can create and run containers from them. Containers are running instances of images. Let’s run an interactive container using the Ubuntu 20.04 image you downloaded.

Use the docker run command with the -it flags to allocate a pseudo-TTY connected to the container and keep STDIN open, creating an interactive shell session within the container:

docker run -it ubuntu:20.04

This command starts a new container from the ubuntu:20.04 image, dropping you into a shell prompt inside the container. The command prompt will change to indicate you are working within the container, typically looking like this:

root@container_id:/#

The container_id is a unique identifier for your running container. You can run any commands inside this container, just like you would on a regular Ubuntu server. For example, update the package list inside the container:

apt update

Then, install a package, like nginx:

apt install nginx

This installs Nginx web server within the Docker container, isolated from your host system. Any changes you make inside the container are isolated to that container.

To exit the container, type exit in the prompt:

exit

The container will stop, but the changes you made (like installing Nginx) are still stored within the container’s filesystem.

Step 6 — Managing Docker Containers

After using Docker for some time, you will accumulate both running and stopped containers. To manage these containers effectively, Docker provides several useful commands.

To list running containers, use docker ps:

docker ps

This command displays information about currently running containers, including container ID, image, command, creation time, status, ports, and names.

To list all containers (both running and stopped), use docker ps -a:

docker ps -a

This command shows all containers, including those that have exited. You can see the status of stopped containers (e.g., ‘Exited’).

To view the most recently created container (whether running or stopped), use docker ps -l:

docker ps -l

To start a stopped container, use docker start followed by the container ID or container name. For example, to start a container with ID container_id_from_docker_ps:

docker start container_id_from_docker_ps

To stop a running container, use docker stop followed by the container ID or name. For example, to stop a container named container_name:

docker stop container_name

You can find container names using docker ps or docker ps -a. Docker automatically assigns names to containers, but you can also specify a name using the --name flag when running a container.

To remove a container that is no longer needed, use docker rm followed by the container ID or name. Make sure the container is stopped before removing it. For example, to remove a container with ID container_id_to_remove:

docker rm container_id_to_remove

You can use the --name option with docker run to assign a specific name to a container. Also, the --rm option can be used with docker run to automatically remove the container when it exits. Explore docker run --help for more options.

Step 7 — Committing Changes to a Docker Image

When you make changes inside a Docker container (e.g., install software, modify files), these changes are initially only present in that container. If you want to save these changes as a new Docker image, you need to “commit” the container. This creates a new image from the container’s current state.

For example, if you installed Nginx inside an Ubuntu container, and want to create a new image with Nginx pre-installed. First, find the container ID of your modified container using docker ps -a. Then, use the docker commit command:

docker commit -m "Installed Nginx" -a "Your Name" container_id your_dockerhub_username/ubuntu-nginx

Replace container_id with the actual container ID, Your Name with your name, and your_dockerhub_username with your Docker Hub username (or a repository name if you are using a private registry). ubuntu-nginx is the name for your new image.

  • -m "Installed Nginx": Adds a commit message describing the changes.
  • -a "Your Name": Specifies the author of the image.
  • container_id: The ID of the container you are committing.
  • your_dockerhub_username/ubuntu-nginx: The repository name and new image name.

After committing, the new image your_dockerhub_username/ubuntu-nginx is saved locally on your server. You can verify it by listing your images:

docker images

The new image will appear in the list. Now you can use this image to run new containers that already have Nginx installed.

You can also automate image creation using Dockerfiles, which define the steps to build an image. Dockerfiles are a more robust and repeatable way to create Docker images, but are beyond the scope of this introductory guide.

Step 8 — Pushing Docker Images to Docker Hub

To share your Docker images with others, you can push them to a Docker registry like Docker Hub. Docker Hub is a public registry, but you can also use private registries for more control over image access.

To push an image to Docker Hub, you need a Docker Hub account. If you don’t have one, sign up at hub.docker.com.

First, log in to Docker Hub from your server using the docker login command:

docker login -u your_dockerhub_username

Enter your Docker Hub password when prompted. If your local username is different from your Docker Hub username, you may need to tag your image with your Docker Hub username before pushing. For example:

docker tag ubuntu-nginx your_dockerhub_username/ubuntu-nginx

Then, push your image to Docker Hub using the docker push command:

docker push your_dockerhub_username/ubuntu-nginx

This command uploads your ubuntu-nginx image to your Docker Hub repository. The push process may take some time depending on the image size and your internet connection speed.

After the push is complete, your image is available on Docker Hub and can be pulled by anyone (if public) or users with access (if private).

If you encounter an “unauthorized: authentication required” error during push, ensure you have logged in correctly using docker login.

Conclusion

You have now successfully installed Docker Engine on your Ubuntu server and learned the basics of working with Docker images and containers. This tutorial covered installing Docker, managing containers, working with images, and sharing images on Docker Hub. With this foundation, you can further explore Docker’s capabilities and use it to streamline your application development and deployment workflows.

To continue learning, explore the official Docker documentation and other tutorials on containerization and Docker best practices. Docker opens up a world of possibilities for efficient and scalable application management on your Ubuntu server.

Would you like to explore more advanced Docker topics or specific use cases?

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 *