How to install and use Docker on Ubuntu

Docker streamlines application development by containerizing your processes, ensuring that your applications run in isolated, lightweight environments. Containers help you deploy software faster by encapsulating all necessary dependencies, resulting in a portable and efficient deployment mechanism compared to traditional virtual machines.

This guide covers installing Docker Community Edition (CE) on Ubuntu 20.04, working with images and containers, and pushing your custom images to a Docker repository. For cloud users, UpCloud offers robust, high-performance servers that enhance container workloads with superior networking and scalable infrastructure.

Prerequisites

Before you begin, confirm that your Ubuntu 20.04 server is correctly configured with a non-root user who has sudo privileges. Ensure your firewall is properly set up, and verify that your system is updated with the latest security patches. Additionally, prepare a Docker Hub account if you plan to push custom images later.

Installing Docker

Updating the Package List

Begin by refreshing your package list to ensure that your system retrieves the latest version information for all packages. Run the following command:

Bash
sudo apt update

This command connects to Ubuntu repositories and updates the local package database, setting the stage for installing Docker’s dependencies.

Installing Required Packages

Install the necessary packages that enable apt to use HTTPS for package downloads. Secure downloads and verify package integrity by executing:

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

These packages are essential for handling secure communication and software property management during the Docker installation process.

Adding Docker’s GPG Key

To maintain package authenticity, add Docker’s official GPG key. This step validates the Docker packages you will download and protects against potential tampering:

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

Adding the Docker Repository

Incorporate Docker’s repository into your package sources to access the most recent Docker CE releases. Execute the following command to add the repository for Ubuntu 20.04 (focal):

Bash
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu focal stable"

This action updates your package sources and ensures that Docker packages are retrieved from Docker’s official repository.

Verifying the Repository Setup

Before installing Docker, confirm that the repository has been added correctly. Use the command below to display Docker CE’s candidate version:

Bash
apt-cache policy docker-ce

This verification step ensures that Docker packages will be installed from the correct source rather than the default Ubuntu repository.

Installing Docker

Now that the repository is set up, install Docker CE by running:

Bash
sudo apt install docker-ce

This command installs both the Docker daemon and the Docker command-line interface, preparing your system for container management.

Checking Docker’s Status

After installation, verify that Docker is running and configured to start on boot. Check the service status with:

Bash
sudo systemctl status docker

The output should indicate that Docker is active and running, confirming that the installation was successful.

Running Docker Without Sudo

Adding Your User to the Docker Group

By default, Docker commands require sudo privileges. To allow running Docker commands without sudo, add your user to the Docker group with:

Bash
sudo usermod -aG docker ${USER}

Applying the Group Change

After modifying group membership, you need to apply the change. Either log out and log back in or run the following command to refresh your session:

Bash
su - ${USER}

Confirming Group Membership

Confirm that your user is now a member of the Docker group by executing:

Bash
groups

This output should list “docker” among your groups, enabling you to run Docker commands without sudo.

Using Docker Commands

Understanding Docker’s Command Structure

Docker commands follow a consistent structure that simplifies container management. The general syntax is:

Bash
docker [option] [command] [arguments]

This structure makes it straightforward to explore Docker’s capabilities and execute specific commands.

Accessing Help for Commands

To view a list of all available Docker commands, type:

Bash
docker

For detailed help regarding any specific command, append the --help option, as shown below:

Bash
docker run --help

This approach provides comprehensive guidance on command usage and available options.

Working with Docker Images

Testing Docker with the Hello-World Image

Images form the foundation of Docker containers. Validate your installation by running a simple test with the hello-world image:

Bash
docker run hello-world

When executed, Docker pulls the hello-world image (if not already available), creates a container, and displays a welcome message that confirms your setup is correct.

Searching for Docker Images

To explore images available on Docker Hub, you can search for specific images. For example, to look for an Ubuntu image, run:

Bash
docker search ubuntu

This command returns a list of available images, providing details about each image’s origin and popularity.

Pulling an Image from Docker Hub

Download the Ubuntu image by executing the pull command:

Bash
docker pull ubuntu

This command retrieves the image from Docker Hub and stores it locally for subsequent use.

Listing Downloaded Images

After pulling images, verify their presence on your system by listing them with:

Bash
docker images

The output displays details such as image names, tags, sizes, and creation dates.

Running a Docker Container

Launching an Interactive Container

To launch a container based on the Ubuntu image with interactive shell access, execute:

Bash
docker run -it ubuntu

The -it options allocate a pseudo-TTY and keep the standard input open, allowing you to interact directly with the container.

Updating the Container and Installing Applications

Once inside the container, update its package database with:

Bash
apt update

Install applications such as Node.js to observe changes within the container environment:

Bash
apt install nodejs

Verify the installation by checking the Node.js version:

Bash
node -v

Exiting the Container

When you have finished working inside the container, type exit to leave the interactive session. This action terminates the container’s interactive mode while preserving any modifications you may have made.

Managing Docker Containers

Viewing Active Containers

Monitor your running containers by listing only the active ones:

Bash
docker ps

This command provides details such as container IDs, images used, and runtime status.

Viewing All Containers

To obtain a complete list that includes both running and stopped containers, use:

Bash
docker ps -a

This comprehensive view aids in tracking container history and usage.

Starting and Stopping Containers

Restart a stopped container by specifying its container ID or name with:

Bash
docker start [container_id_or_name]

Conversely, stop a running container with:

Bash
docker stop [container_id_or_name]

These commands enable you to control container states effectively.

Removing Unnecessary Containers

When a container is no longer needed, remove it to free system resources. Use the command below, replacing the placeholder with the actual container identifier:

Bash
docker rm [container_id_or_name]

Removing unused containers helps maintain a clean and efficient environment.

Creating Docker Images

Committing Changes from a Container

After making modifications within a container, preserve the changes by committing them to a new image. Run the commit command along with a descriptive message and author information:

Bash
docker commit -m "Installed Node.js and updated packages" -a "Your Name" [container_id] new_image_name

This process creates a snapshot of your container that you can reuse for future deployments.

Confirming the New Image

Verify that the new image has been successfully created by listing your local images:

Bash
docker images

This verification ensures that your custom image appears among the available images.

Pushing Images to Docker Hub

Logging into Docker Hub

To share your custom image, begin by logging into Docker Hub with your credentials:

Bash
docker login

Successful authentication allows you to push images to your Docker Hub repository.

Tagging the New Image

If your Docker Hub username differs from your local username, retag your image appropriately:

Bash
docker tag new_image_name your_dockerhub_username/new_image_name

Proper tagging ensures that the image is associated with your Docker Hub account.

Pushing the Image to the Repository

Upload your image to Docker Hub with the push command:

Bash
docker push your_dockerhub_username/new_image_name

This command transmits your image to the repository, making it available for use on other systems. The process may take several minutes, depending on your network speed and image size.

Conclusion

You have now installed Docker on Ubuntu 20.04, explored fundamental Docker commands, managed images and containers, and pushed a custom image to Docker Hub. This guide provides a strong foundation in containerization, and you are well-prepared to integrate Docker into your development and deployment workflows. For high-performance cloud solutions that optimize container workloads, UpCloud offers superior networking and scalable infrastructure, ensuring that your Docker applications run efficiently in production environments. Continue exploring advanced Docker topics, such as Docker Compose and container orchestration, to further enhance your capabilities.

Comments

Leave a Reply

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