Skip to main content

Docker Setup Guide on Ubuntu Server

This guide will help you set up Docker on your Ubuntu Server for the first time. We'll cover installing Docker, running your first container, and basic usage.

Prerequisites

  • Ubuntu Server: Ubuntu 18.04 LTS or newer is recommended.
  • User Privileges: A user account with sudo privileges.

Step 1: Update Your System

Begin by updating your package list and upgrading existing packages:

Copy code

sudo apt update sudo apt upgrade -y

Step 2: Install Required Packages

Install packages that allow apt to use repositories over HTTPS:

Copy code

sudo apt install -y ca-certificates curl gnupg

Step 3: Add Docker’s Official GPG Key

Create a directory for the keyrings:

Copy code

sudo install -m 0755 -d /etc/apt/keyrings

Add Docker's official GPG key:

Copy code

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

Set appropriate permissions:

Copy code

sudo chmod a+r /etc/apt/keyrings/docker.gpg

Step 4: Set Up the Docker Repository

Add the Docker APT repository to your system:

Copy code

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: Install Docker Engine

Update the package database:

Copy code

sudo apt update

Install Docker Engine and related components:

Copy code

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

Step 6: Verify Docker Installation

Run the hello-world image to verify that Docker is installed correctly:

Copy code

sudo docker run hello-world

You should see a message that says "Hello from Docker!"


Step 7: Manage Docker as a Non-Root User (Optional)

To run Docker commands without sudo, add your user to the docker group:

Copy code

sudo usermod -aG docker $USER

Log out and log back in to apply the group membership.

Test Docker without sudo:

Copy code

docker run hello-world

Step 8: Enable Docker to Start on Boot

Ensure Docker starts automatically when the system boots:

Copy code

sudo systemctl enable docker.service sudo systemctl enable containerd.service

Step 9: Basic Docker Commands

List Docker Images

Copy code

docker images

List Running Containers

Copy code

docker ps

List All Containers (Including Stopped Ones)

Copy code

docker ps -a

Pull an Image from Docker Hub

Copy code

docker pull ubuntu:latest

Run a Container

Copy code

docker run -it ubuntu:latest /bin/bash

This command runs an Ubuntu container and provides an interactive shell.

Stop a Running Container

First, find the container ID using docker ps, then stop it:

Copy code

docker stop <container_id>

Conclusion

You've successfully installed Docker on your Ubuntu Server and run your first container. You can now explore Docker's features further by creating your own images, managing containers, and integrating Docker into your workflows.


References