How to Install Docker and Run Your First Container on Ubuntu Server
Learning how to install Docker and run your first container on Ubuntu Server is essential for modern development and deployment workflows. Docker revolutionizes application deployment by packaging software into lightweight, portable containers that run consistently across different environments. This containerization technology eliminates the “it works on my machine” problem and streamlines development processes.
This comprehensive tutorial will guide you through installing Docker on Ubuntu Server and running your first container. You’ll learn the fundamental Docker commands, understand container basics, and gain hands-on experience with containerized applications. By the end of this guide, you’ll have a working Docker installation and the knowledge to deploy your first containerized application.
Docker containers offer significant advantages over traditional virtual machines. They consume fewer resources, start faster, and provide better scalability. Whether you’re a developer looking to streamline your workflow or a system administrator planning container deployments, this tutorial provides the foundation you need.
Prerequisites for Installing Docker and Running Containers on Ubuntu Server
Before you begin this tutorial on how to install Docker and run your first container on Ubuntu Server, ensure you meet these requirements. You’ll need a fresh Ubuntu Server installation (version 20.04 LTS or newer recommended) with root or sudo access. A minimum of 2GB RAM and 20GB free disk space will provide adequate resources for Docker operations.
Your server should have a stable internet connection for downloading Docker packages and container images. Basic command-line knowledge is essential, including familiarity with package management and file editing. You should understand fundamental Linux concepts like users, permissions, and services.
Verify your Ubuntu version by running lsb_release -a. This tutorial works with Ubuntu 18.04, 20.04, and 22.04 LTS versions. Ensure your system is updated with sudo apt update && sudo apt upgrade -y before proceeding.
The installation process typically takes 15-30 minutes, depending on your internet speed and server performance. Having SSH access configured will make the process more convenient if you’re working remotely.
Step-by-Step Installation Guide for Docker on Ubuntu Server
This event shares similarities with: How to Containerize a Node.js Web Application with Docker
Now let’s walk through how to install Docker and run your first container on Ubuntu Server with detailed instructions.
Step 1: Update System Packages
Start by updating your package index to ensure you have the latest package information:
sudo apt update
sudo apt upgrade -y
This step ensures your system has the latest security updates and package definitions. The upgrade process may take several minutes depending on available updates.
Step 2: Install Required Dependencies
Install packages that allow apt to use repositories over HTTPS:
sudo apt install apt-transport-https ca-certificates curl gnupg lsb-release -y
These packages enable secure communication with Docker’s official repository and handle GPG key verification.
Step 3: Add Docker’s Official GPG Key
Add Docker’s official GPG key to verify package authenticity:
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
This cryptographic key ensures you’re downloading genuine Docker packages from the official repository.
Step 4: Set Up Docker Repository
Add the Docker repository to your system’s package sources:
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 automatically detects your Ubuntu version and architecture, adding the appropriate repository configuration.
Step 5: Install Docker Engine
Update the package index again and install Docker:
sudo apt update
sudo apt install docker-ce docker-ce-cli containerd.io docker-compose-plugin -y
This installs Docker Engine, the command-line interface, container runtime, and Docker Compose plugin for multi-container applications.
Step 6: Start and Enable Docker Service
Start Docker and configure it to start automatically on boot:
sudo systemctl start docker
sudo systemctl enable docker
Verify Docker is running with sudo systemctl status docker. You should see an active (running) status.
Step 7: Add User to Docker Group
Add your user to the docker group to run Docker commands without sudo:
sudo usermod -aG docker $USER
Log out and log back in for this change to take effect. This step improves security and convenience for regular Docker operations.
Step 8: Verify Docker Installation
Test your Docker installation by running the hello-world container:
docker run hello-world
This command downloads and runs a test container that displays a welcome message, confirming your Docker installation works correctly.
Running Your First Container and Understanding Docker Basics
With Docker installed, let’s explore how to install Docker and run your first container on Ubuntu Server through practical examples.
Step 9: Pull Your First Real Container Image
Download the official Ubuntu container image:
docker pull ubuntu:latest
This command downloads the latest Ubuntu container image from Docker Hub, Docker’s official container registry. The image provides a minimal Ubuntu environment for running applications.
Step 10: Run an Interactive Container
Start an interactive Ubuntu container:
docker run -it ubuntu:latest /bin/bash
The -it flags provide an interactive terminal session inside the container. You’ll see a new command prompt indicating you’re inside the container environment.
Step 11: Explore the Container Environment
Inside the container, run these commands to explore the environment:
ls -la
cat /etc/os-release
whoami
Notice you’re running as root inside an isolated Ubuntu environment. This demonstrates Docker’s containerization capabilities.
Step 12: Exit and Manage Containers
Exit the container by typing exit or pressing Ctrl+D. Back on your host system, view running containers:
docker ps
View all containers (including stopped ones):
docker ps -a
This shows your container history and current status.
Step 13: Run a Web Server Container
Let’s run a more practical example with an Nginx web server:
docker run -d -p 8080:80 --name my-nginx nginx:latest
The -d flag runs the container in detached mode, -p 8080:80 maps port 8080 on your host to port 80 in the container, and --name gives your container a friendly name.
Step 14: Verify Web Server Access
Check if Nginx is running by accessing it:
curl localhost:8080
You should see the default Nginx welcome page HTML. This demonstrates how containers can run services accessible from your host system.
Troubleshooting Common Docker Installation Issues
When learning how to install Docker and run your first container on Ubuntu Server, you might encounter several common issues. Here are solutions for the most frequent problems.
Permission Denied Errors
If you see “permission denied” when running Docker commands, ensure you’ve added your user to the docker group and logged out/in again. Alternatively, use sudo docker commands temporarily.
Docker Service Won’t Start
If Docker fails to start, check the service status:
sudo systemctl status docker
sudo journalctl -u docker
Common causes include insufficient disk space or conflicting services. Ensure you have adequate storage and no other container runtimes running.
Container Download Failures
Network issues can prevent image downloads. Verify internet connectivity and try pulling images manually:
docker pull hello-world
If this fails, check your DNS settings and firewall configuration.
Port Binding Conflicts
If port mapping fails with “port already in use” errors, check which process is using the port:
sudo netstat -tlnp | grep :8080
Choose a different host port or stop the conflicting service.
Storage Space Issues
Docker images and containers consume disk space. Monitor usage with:
docker system df
Clean up unused containers and images:
docker system prune
Regular cleanup prevents storage issues and maintains system performance.
Docker provides extensive documentation at the official installation guide for additional troubleshooting steps.
Congratulations! You’ve successfully learned how to install Docker and run your first container on Ubuntu Server. You now have a working Docker installation capable of running containerized applications. You’ve explored basic Docker commands, run interactive containers, and deployed a web server container.
This foundation enables you to explore advanced Docker features like custom images, Docker Compose for multi-container applications, and container orchestration. Consider learning about Dockerfile creation, volume management, and networking for more complex deployments. Docker’s containerization technology will streamline your development and deployment processes significantly.
Your next steps might include exploring the Docker Hub registry for pre-built applications, learning Docker Compose for complex applications, or investigating container orchestration platforms like Kubernetes. The containerization journey you’ve started today opens doors to modern, scalable application deployment strategies.
