Docker is a powerful platform for developing, shipping, and running applications inside containers. Installing Docker on Ubuntu is straightforward, and this guide will walk you through the process step by step, including how to set up Docker, verify the installation, and run basic containers like Nginx. This guide is based on best practices and incorporates insights from community discussions, such as a recent thread on Docker installation and usage.

Prerequisites

Ubuntu 20.04 or later (this guide is compatible with most recent Ubuntu versions).

  • A user with sudo privileges.
  • An internet connection to download packages.

Step 1: Update the Package Index

Ensure your system’s package index is up to date to avoid compatibility issues.

sudo apt-get update

Step 2: Install Required Dependencies

Install packages needed to add the Docker repository securely.

sudo apt-get install -y ca-certificates curl gnupg lsb-release

Step 3: Add Docker’s Official GPG Key

Docker’s repository requires a GPG key to verify package authenticity.

sudo mkdir -p /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg

Step 4: Set Up the Docker Repository

Add the Docker repository to your system’s sources list.

echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

Step 5: Update Package Index Again

Refresh the package index to include the Docker repository.

sudo apt-get update

Step 6: Install Docker

Install Docker and its essential components.

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

Step 7: Verify Docker Installation

Check that Docker is installed correctly by viewing its version.

docker --version

Step 8: Run Docker Without sudo (Optional)

To avoid using sudo for every Docker command, add your user to the docker group.

sudo usermod -aG docker $USER
newgrp docker

Log out and back in, or restart your session for the group change to take effect.

Step 9: Test Docker with a Simple Container

Run a basic Nginx web server to confirm Docker is working.

docker run --name website -d -p 8080:80 nginx:latest
  • --name website: Names the container “website.”
  • -d: Runs the container in detached mode (background).
  • -p 8080:80: Maps port 8080 on your host to port 80 in the container.
  • nginx:latest: Uses the latest Nginx image from Docker Hub.

Verify the container is running by accessing http://localhost:8080 in a browser or using:

docker ps

Step 10: Manage Docker Containers and Images

Here are some useful commands to manage your Docker environment:

  • List all Docker images:

    docker images
    
  • List running containers:

    docker ps
    
  • List all containers (running and stopped):

    docker ps -a
    
  • Stop a container (replace CONTAINERID with the container’s ID from docker ps):

    docker stop CONTAINERID
    
  • Remove a container:

    docker rm CONTAINERID
    

Step 11: Serve Custom HTML with Nginx

To run an Nginx container that serves your own HTML files, mount a local directory to the container’s web root.

Read-Only Mount

docker run --name site -v /path/to/your/html:/usr/share/nginx/html:ro -d -p 8080:80 nginx:latest

Read-Write Mount

docker run --name site -v /path/to/your/html:/usr/share/nginx/html -d -p 8080:80 nginx:latest

Replace /path/to/your/html with the path to your local HTML files.

Step 12: Share Volumes Between Containers

To start a second Nginx container that reuses the same files as the first:

docker run --name website-2 --volumes-from site -d -p 81:80 nginx:latest
  • --volumes-from site: Reuses the mounted volume from the “site” container.
  • -p 81:80: Maps port 81 on the host to avoid conflicts with the first container.

Troubleshooting Tips

  • Permission issues: Ensure your user is in the docker group or use sudo.
  • Port conflicts: Check if ports (e.g., 8080 or 81) are already in use with sudo netstat -tuln.
  • Repository errors: Verify the Ubuntu codename (lsb_release -cs) matches the repository configuration.
  • Container not starting: Use docker logs CONTAINERID to check for errors.

Conclusion

You’ve now installed Docker on Ubuntu, set up a basic Nginx container, and learned how to manage containers and images. Docker’s flexibility makes it ideal for development and production environments. For more advanced usage, explore Docker Compose for multi-container setups or check the official Docker documentation.