Test your skills on our all Hosting services and get 15% off!

Use code at checkout:

Skills
08.10.2024

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 need
    sudo
    or root access to install and configure Docker.

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
to use repositories over HTTPS:

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
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

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
container, which is a simple test to confirm Docker is functioning correctly:

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
commands need to be run with
sudo
. To avoid this, add your user to the
docker
group:

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
, run:

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
file.

6.1. Create a
docker-compose.yml
File

Create a new directory for your Docker Compose project:

mkdir my_docker_app
cd my_docker_app

Create a

docker-compose.yml
file with the following content:

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
flag runs the container in detached mode, meaning it runs in the background.

6.3. Verify the Service

Visit

http://localhost:8080
in your web browser. You should see the default Nginx welcome page.

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.

Test your skills on our all Hosting services and get 15% off!

Use code at checkout:

Skills