Ultimate Guide: How to Configure Docker on Linux Server

Docker is a platform that helps developers package and deploy applications in lightweight, portable containers. By using containers, developers can ensure their applications run consistently across any environment. If you’re using a Linux server, configuring Docker can bring you many advantages, including better resource management, faster deployments, and easy scalability.

Configure Docker on Linux

This guide will walk you through the steps to configure Docker on a Linux server in a straightforward manner.

Prerequisites

Before you begin, there are a few things you should ensure:

  • Supported Linux Distributions: Docker can be installed on many Linux distributions such as Ubuntu 22.04 LTS, CentOS 8, Debian 11, and Fedora 34+.
  • System Requirements: Your Linux server should have a 64-bit architecture and run a Linux kernel version of 3.10 or higher.
  • User Permissions: Make sure you have sudo or root access to the server.

Install Docker Engine

Uninstall Old Versions

If you have any old versions of Docker installed, remove them first to avoid conflicts. Run the following command:

sudo apt-get remove docker docker-engine docker.io containerd runc

Set Up Docker’s Official Repository

To get the latest version of Docker, you’ll need to add Docker’s official repository to your package manager. Follow these commands based on your distribution:

  • Update package index:
sudo apt-get update
  • Install prerequisites:
sudo apt-get install apt-transport-https ca-certificates curl software-properties-common
  • Add Docker’s official GPG key:
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
  • Set up the Docker stable repository:
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"

Install Docker Engine

Now, you can install Docker using your package manager:

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

Verify Installation

To ensure Docker was installed correctly, check the Docker version:

docker --version

You should see the installed version of Docker.

Check Out | How to Configure SSH on a Linux Server: Comprehensive Guide for Beginners

Manage Docker as a Non-Root User

By default, Docker requires root privileges. To avoid using sudo every time you run a Docker command, you can set up Docker to be used by non-root users.

Create Docker Group

First, create a Docker group:

sudo groupadd docker

Add User to Docker Group

Add your user to the Docker group:

sudo usermod -aG docker $USER

Apply Group Changes

To apply the group changes, log out and log back in, or run:

newgrp docker

Test Docker Without Sudo

Test if Docker works without sudo by running the following command:

docker run hello-world

If everything is set up properly, you should see a “Hello from Docker!” message.

Enable Docker to Start on Boot

To make sure Docker starts automatically when your server reboots, enable it as a system service:

Enable Docker Service

sudo systemctl enable docker

Start Docker Service

sudo systemctl start docker

Verify Docker Status

Check if Docker is running with:

sudo systemctl status docker

Test Docker Installation

At this point, Docker should be fully installed and running. To verify it’s working properly, you can run a simple Docker container.

Run Hello-World Container

Run the following command to start a test container:

docker run hello-world

You should see a confirmation message indicating Docker is installed and working correctly.

Basic Docker Commands

Now that Docker is up and running, here are some basic commands to get started:

  • List Running Containers
docker ps
  • Run a Container

To run an Nginx container, for example:

docker run -d -p 80:80 nginx

This command will run Nginx in the background, exposing port 80 to the server.

  • Stop a Running Container

To stop a running container, use:

docker stop <container_id>
  • Remove a Container

To remove a container:

docker rm <container_id>
  • List Docker Images

To list all the Docker images installed:

docker images
  • Remove an Image

To remove an image:

docker rmi <image_id>

Install Docker Compose

Docker Compose allows you to run multi-container applications. To install Docker Compose:

Download Docker Compose

Run the following command to download Docker Compose:

sudo curl -L "https://github.com/docker/compose/releases/download/$(curl -s https://api.github.com/repos/docker/compose/releases/latest | jq -r .tag_name)/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

Apply Executable Permissions

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

Verify Installation

docker-compose --version

Configure Docker Daemon (Optional)

You may want to configure the Docker daemon for better control over Docker behavior. For example, to change the logging driver or configure storage settings.

Edit Docker Daemon Configuration

Open the Docker daemon configuration file:

sudo nano /etc/docker/daemon.json

Add or modify your desired settings, such as logging options.

Restart Docker Service

sudo systemctl restart docker

Verify Configuration

Check the Docker configuration with:

docker info

Secure Docker Installation

Security is crucial when using Docker. Here are a few steps to enhance the security of your Docker setup:

Configure Firewall Rules

Make sure your firewall allows only trusted IP addresses to access Docker’s ports.

Use Docker Content Trust

To ensure the integrity of images, you can enable Docker Content Trust:

export DOCKER_CONTENT_TRUST=1

Regularly Update Docker

Security patches and updates are regularly released for Docker. Keep your Docker installation updated to avoid vulnerabilities.

Conclusion

You’ve successfully configured Docker on your Linux server. With Docker set up, you can now run containers and manage applications with ease. If you’re planning to scale your applications, consider learning Docker Compose for multi-container setups and Docker Swarm for orchestration.

Docker is a powerful tool, and this guide covered the essential steps to get you started. As you continue working with Docker, refer to the official Docker documentation for advanced configurations and usage.

Leave A Comment