Installation and Usage Guide for Docker on Ubuntu
Docker is a powerful tool that enables developers to build, test, and deploy applications quickly inside lightweight, portable containers. This guide will walk you through the process of installing Docker on an Ubuntu system and provide some basic commands to get you started.
Prerequisites
- Ubuntu version: This guide works for Ubuntu 20.04, 22.04, or newer.
- User privileges: You needor root access to install and configure Docker.
sudo
Step 1: Update System Packages
Before installing Docker, it’s important to update your system packages to ensure you have the latest versions:
sudo apt update
sudo apt upgrade -y
Step 2: Install Docker
2.1. Install Required Packages
Install packages that allow
apt
sudo apt install apt-transport-https ca-certificates curl software-properties-common -y
2.2. Add Docker’s Official GPG Key
Add the Docker 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
2.3. Add Docker Repository
Add the Docker repository to
apt
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
2.4. Install Docker
Now, update the package index and install Docker:
sudo apt update
sudo apt install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin -y
This command installs Docker Community Edition (CE) and other required components.
Step 3: Verify Docker Installation
Check that Docker is installed and running correctly:
sudo systemctl status docker
You should see output indicating that Docker is active and running.
To verify the installation further, run the following command:
sudo docker --version
You can also run the
hello-world
sudo docker run hello-world
This command will download a test image and run it in a container. If everything is set up correctly, you will see a “Hello from Docker!” message.
Step 4: Manage Docker as a Non-root User
By default,
docker
sudo
docker
sudo usermod -aG docker $USER
After running this command, log out and log back in, or restart your terminal session to apply the changes.
To test if Docker now works without
sudo
docker run hello-world
Step 5: Basic Docker Commands
Here are some basic commands to help you start using Docker:
- Pull an Image: Download a Docker image from Docker Hub.
docker pull ubuntu:latest
- Run a Container: Run an Ubuntu container interactively.
docker run -it ubuntu
- List Running Containers: Display active containers.
docker ps
- List All Containers: Show all containers, including stopped ones.
docker ps -a
- Stop a Container: Stop a running container.
docker stop <container_id>
- Remove a Container: Delete a stopped container.
docker rm <container_id>
- Remove an Image: Delete a Docker image from your system.
docker rmi ubuntu:latest
Step 6: Using Docker Compose
Docker Compose is a tool for defining and running multi-container Docker applications using a
docker-compose.yml
6.1. Create adocker-compose.yml
File
docker-compose.yml
Create a new directory for your Docker Compose project:
mkdir my_docker_app
cd my_docker_app
Create a
docker-compose.yml
version: '3'
services:
web:
image: nginx:latest
ports:
- "8080:80"
This file defines a simple Nginx web server that will run on port 8080.
6.2. Start the Docker Compose Application
Run the following command to start the services defined in
docker-compose.yml
docker-compose up -d
The
-d
6.3. Verify the Service
Visit
http://localhost:8080
6.4. Stop the Docker Compose Application
To stop and remove the services, run:
docker-compose down
Step 7: Update Docker
To keep Docker up to date, use the following commands periodically:
sudo apt update
sudo apt upgrade -y
Conclusion
You have successfully installed Docker and Docker Compose on Ubuntu, and you’ve learned some basic commands to get started. Docker simplifies the deployment of applications by packaging them in containers, making it easier to manage dependencies and ensure consistency across different environments. With Docker up and running, you can explore more advanced features like creating custom Docker images, networking, and orchestration tools like Kubernetes for scaling your applications.