A Step-by-Step Guide to Installing and Configuring Docker on Ubuntu Server A Step-by-Step Guide to Installing and Configuring Docker on Ubuntu Server

A Step-by-Step Guide to Installing and Configuring Docker on Ubuntu Server

Docker is a powerful tool for Developers, System Administrator or DevOps Engineers. It made it easier to deploy your application without any hassle by using it’s containers. Docker containers are lightweight, doesn’t require you to install all dependencies in your server that’s why it’s lightweight and we can bundle an application with it’s all dependencies ensuring it runs smoothly across different environment. In this post, I’ll show you how to install and set up Docker on your Ubuntu or Linux server.

Prerequisites

Before we dive into the installation, ensure the following prerequisites are met:

  1. Ubuntu Server: This guide is designed for Ubuntu 24.04, but the steps should be similar for other versions. See how to lunch EC2 instance
  2. Sudo Privileges: You’ll need administrative access on the server.
  3. Updated System: Ensure your server is up-to-date by running the command sudo apt update && sudo apt upgrade

Step 1: Install Docker

The first step is to install Docker. We’ll use Docker’s official repository to ensure we get the latest version.

1.1. Uninstall Old Versions

If you’ve installed Docker previously, uninstall any older versions (if present):

sudo apt update && sudo apt upgrade

1.2. Set Up Docker’s Official Repository

First, install the prerequisite packages to allow apt to use HTTPS repositories:

sudo apt install apt-transport-https ca-certificates curl software-properties-common

Next, add Docker’s official GPG key:

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg

Then, add the Docker APT repository:

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

1.3. Install Docker Engine

Update the package index and install Docker Engine:

sudo apt update
sudo apt install docker-ce docker-ce-cli containerd.io

Verify the installation by checking the Docker version:

docker --version

This should display the version of Docker you installed, confirming that Docker is now installed on your server.

Step 2: Manage Docker as a Non-Root User

By default, Docker commands require sudo. If you want to run Docker commands as a non-root user, follow these steps:

2.1. Create the Docker Group

Create a Docker group to allow users to run Docker commands without sudo:

sudo groupadd docker

2.2. Add Your User to the Docker Group

Add your user to the Docker group:

sudo usermod -aG docker $USER

For the changes to take effect, either log out and log back in, or run:

newgrp docker

Verify that you can now run Docker without sudo:

docker run hello-world

This should download and run a test container, confirming that Docker is functioning correctly.

Step 3: Configure Docker to Start on Boot

To ensure Docker starts automatically when the server reboots, enable the Docker service:

sudo systemctl enable docker

You can verify if Docker is running with:

sudo systemctl status docker

Step 4: Basic Docker Commands

Now that Docker is installed, here are some basic commands to get started:

  • Check Docker Version:
docker --version
  • Run a Test Container:
docker run hello-world

List Running Containers:

docker ps

Stop a Running Container:

docker stop <container-id/name> 

Remove a Container:

docker rm <container-id>

List All Containers (Including Stopped):

docker ps -a

Pull an Image from Docker Hub:

docker pull <image-name>

Step 5: Using Docker Compose (Useful to run containers easily)

Docker Compose is a tool for defining and running multi-container Docker applications. To install Docker Compose, follow these steps:

5.1. Install Docker Compose

Run the following command to download the latest version of Docker Compose:

sudo curl -L "https://github.com/docker/compose/releases/download/$(curl -s https://api.github.com/repos/docker/compose/releases/latest | grep -Po '"tag_name": "\K[0-9.]+')" /usr/local/bin/docker-compose

Set the appropriate permissions:

sudo chmod +x /usr/local/bin/docker-compose

Verify the installation:

docker-compose --version

Step 6: Configure Docker for Your Projects

Docker can be used to run various applications by using Dockerfiles or docker-compose.yml files to define your services. Here’s a quick example of how you can set up a simple web service using Docker:

6.1. Create a Dockerfile

Let’s say you want to containerize a simple Nginx web server. First, create a new directory for your project:

mkdir my-docker-app
cd my-docker-app

Create a Dockerfile:

nano Dockerfile

In the file, add the following contents:

FROM nginx:alpine
COPY . /usr/share/nginx/html

6.2. Build and Run Your Docker Container

Build your Docker image:

docker build -t my-nginx-app .

Run the container:

docker run -d -p 80:80 my-nginx-app

You can now access your Nginx web server by navigating to your server’s IP address in a browser.

Conclusion

Now that You’ve successfully installed and configured Docker on your Ubuntu server! With Docker, you can now deploy applications in a balanced and efficient way. Whether you’re setting up a simple web server or managing complex microservices, Docker offers a big platform for containerized applications. Continue exploring Docker by creating your own containers, using Docker Compose, or deploying Docker in production environments. I will publish more post about in-depth docker.

Leave a Reply

Your email address will not be published. Required fields are marked *