---
title: Installing and Using Docker on a Linux Server
description: "## Step 1: Update the Server Before installing Docker, update the package index on your server: ``` sudo apt update && sudo apt upgrade -y # For Debian-based systems sudo yum update -y # For Red Hat/CentOS-based systems ``` ##…"
url: "https://truehost.com/support/knowledge-base/installing-and-using-docker-on-a-linux-server/"
updated: "2024-11-02"
category: Cloud servers in Kenya
---

# Installing and Using Docker on a Linux Server

## Step 1: Update the Server

Before installing Docker, update the package index on your server:

```
sudo apt update && sudo apt upgrade -y # For Debian-based systems

sudo yum update -y # For Red Hat/CentOS-based systems
```

## Step 2: Install Docker

### For Debian/Ubuntu-based Systems

1. **Install necessary prerequisites**:

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

2. Add Docker’s official GPG key and repository:

```
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpgecho "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
```

3. **Install Docker**:

```
sudo apt updatesudo apt install -y docker-ce docker-ce-cli containerd.io
```

### For CentOS/RHEL-based Systems

1. **Add Docker’s official repository**:

```
sudo yum install -y yum-utils

sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
```

2. **Install Docker**:

```
sudo yum install -y docker-ce docker-ce-cli containerd.io
```

3. **Start and enable Docker**:

```
sudo systemctl start dockersudo systemctl enable docker
```

## Step 3: Verify the Docker Installation

After installation, verify Docker by checking its version:

```
docker --version
```

You should see the Docker version output, confirming that it’s installed correctly.

## Step 4: Run Docker Commands Without `sudo`

By default, Docker requires root privileges. To avoid using `sudo` with every Docker command:

1. **Add your user to the `docker` group**:

```
sudo usermod -aG docker $USER
```

2. **Log out and back in** to apply the group changes.

## Step 5: Test Docker Installation

Run a test Docker container to ensure everything works as expected:

```
docker run hello-world
```

This command downloads a test image and runs it in a container. If it runs successfully, Docker is installed and working.

## Basic Docker Usage

### 1. **List Docker Images**

To see the images on your system:

```
docker images
```

### 2. **Pull an Image**

Download an image from Docker Hub, such as Ubuntu:

```
docker pull ubuntu
```

### 3. **Run a Container**

Run a container interactively using the Ubuntu image:

```
docker run -it ubuntu /bin/bash
```

This starts a new container, opens an interactive terminal, and uses `/bin/bash` as the default command.

### 4. **List Running Containers**

To list all active containers:

```
docker ps
```

To list all containers (active and inactive):

```
docker ps -a
```

### 5. **Stop a Container**

Stop a running container by using its **Container ID**:

```
docker stop <container_id>
```

### 6. **Remove a Container**

Remove a stopped container:

```
docker rm <container_id>
```

### 7. **Remove an Image**

Remove an unused image:

```
docker rmi <image_id>
```

## Configuring Docker to Start on Boot

To enable Docker to start automatically at boot:

```
sudo systemctl enable docker
```

## Additional Configuration (Optional)

### 1. **Docker Compose**

Docker Compose is a tool that lets you define and manage multi-container Docker applications.

1. **Install Docker Compose**:

```
sudo curl -L "https://github.com/docker/compose/releases/download/2.0.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
```

2. **Make it executable**:

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

3. **Verify installation**:

```
docker-compose --version
```

### 2. **Docker Swarm (for Orchestration)**

Docker Swarm is Docker’s native clustering and orchestration tool, ideal for managing a cluster of Docker nodes.

1. **Initialize Docker Swarm**:

```
docker swarm init
```

2. J**oin nodes to the swarm**:

- Use the output from the `swarm init` command to add nodes to the swarm.

## Conclusion

This guide covered installing Docker on a Linux server, verifying the installation, and using Docker for container management.

With Docker, you can simplify the deployment process, create reproducible environments, and easily scale your applications. Docker Compose and Docker Swarm offer extended functionality for multi-container setups and orchestration, respectively.
