Docker is a platform that enables developers to automate the deployment of applications inside lightweight, portable containers. Here's how to install and use Docker on Ubuntu Server 22.04:
Install the necessary packages that allow apt to use repositories over HTTPS:
sudo apt update && sudo apt install apt-transport-https \ ca-certificates curl software-properties-common
Add Docker’s official GPG key to verify the authenticity of the packages:
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | \ sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
Add Docker’s official repository to your system’s software repository list:
echo "deb [arch=amd64 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
Update the package index again to include Docker's packages:
sudo apt update
Install Docker Engine and related components:
sudo apt install docker-ce docker-ce-cli containerd.io
Check the status of Docker to ensure it's running:
sudo systemctl status docker
You should see output indicating that Docker is active and running.
To allow your user to run Docker commands without sudo, add your user to the Docker group:
sudo usermod -aG docker $USER
Apply the group membership changes:
newgrp docker
Now, you can run Docker commands without sudo.
Run a test container to verify that Docker is working correctly:
docker run hello-world
If Docker is installed correctly, you should see a message saying “Hello from Docker!”
Docker Compose is a tool that allows you to define and manage multi-container Docker applications. Here's how to install and use Docker Compose on Ubuntu 22.04:
Docker Compose is now included as a plugin in Docker Engine. To install it:
sudo apt install docker-compose-plugin
Check the version of Docker Compose to ensure it's installed correctly:
docker compose version
You should see output indicating the version of Docker Compose installed.
To define and run multi-container applications:
mkdir ~/myapp cd ~/myapp
docker-compose.yml file:vim docker-compose.yml
Add the following content to define a simple web application with Nginx:
version: '3.8' services: web: image: nginx:latest ports: - "8080:80"
docker compose up -d
The -d flag runs the containers in detached mode.
docker ps
You should see the Nginx container listed.
Open a web browser and navigate to http://localhost:8080. You should see the Nginx welcome page.
Docker Compose provides several commands to manage your applications:
docker compose down
This stops and removes all containers defined in the docker-compose.yml file.
docker compose logs
This displays the logs of all containers. You can specify a service to view logs for a specific container:
docker compose logs web
docker compose up -d --scale web=3
This scales the web service to 3 instances.
docker compose ps
This lists the status of all services defined in the docker-compose.yml file.