Setting up Docker on the server

Prerequisites

  • Ubuntu instance with sudo privileges

  • SSH access to the instance

1. Install Docker

Update System and Install Prerequisites

# Update package index
sudo apt-get update

# Install required packages
sudo apt-get install -y \
    ca-certificates \
    curl \
    gnupg \
    lsb-release

Add Docker Repository

# Create directory for Docker GPG key
sudo mkdir -m 0755 -p /etc/apt/keyrings

# Download and add Docker's official GPG key
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | \
    sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg

# Add Docker repository to apt sources
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

Install Docker Engine

2. Configure User Permissions

3. Start and Enable Docker Service

4. Apply Group Changes

Note: Alternatively, you can disconnect and reconnect to your SSH session:

5. Verify Installation

6. Verify Everything is Working

If successful, you should see:

  • docker ps runs without permission errors

  • docker run hello-world downloads and runs a test image

  • Docker version information displays correctly

Troubleshooting

If Permission Denied Still Occurs

If Docker Service Won't Start

Security Considerations for EC2

  1. Security Groups: Ensure your AWS Security Group allows necessary ports:

    • Port 2375 (unencrypted) or 2376 (TLS) only if remote Docker access is needed

    • Keep these closed unless specifically required

  2. IAM Roles: If your containers need AWS access, attach appropriate IAM roles to your EC2 instance

  3. Docker Group Warning: Adding users to the docker group grants root-equivalent privileges. Only add trusted users.

Quick One-Liner Installation

For a fresh Ubuntu EC2 instance, you can run all installation commands in sequence:

Post-Installation Optional Steps

Install Docker Compose (Standalone)

Configure Docker Logging

Useful Docker Commands

Last updated