Skip to main content

How to Install and Configure Docker And Docker-compose

Step-by-Step Guide to Installing Docker and Docker Compose on Your VPS or Root Server

Docker and Docker Compose are powerful tools for running and managing containerized applications. Whether you’re deploying web apps, databases, or microservices, setting them up on a VPS or root server is straightforward. In this guide, I’ll walk you through connecting to your server via SSH using PuTTY, installing Docker and Docker Compose, and configuring them on an Ubuntu or Debian system. Let’s get started!


Prerequisites

Before diving in, ensure you have:

  • A VPS or root server running Ubuntu or Debian.
  • Root or sudo access to the server.
  • An internet connection for downloading packages.
  • The IP address or domain name of your server.

Step 1: Download and Install PuTTY

If you haven’t already, download PuTTY, a free SSH client for connecting to your server:

  • Visit the official PuTTY website.
  • Download the installer or standalone putty.exe for your Windows system.
  • Install PuTTY (or run the executable directly if using the standalone version).

Step 2: Connect to Your Server via SSH

  1. Open PuTTY:
    • Launch PuTTY from your Start menu or by double-clicking putty.exe.
  2. Enter Server Details:
    • In the “Host Name (or IP address)” field, type your server’s IP address or domain name (e.g., 192.168.1.1 or yourdomain.com).
    • Ensure the Port is set to 22 (default for SSH).
    • Select SSH as the connection type (default).
  3. Connect:
    • Click the “Open” button (not “OK”, as PuTTY uses “Open” to initiate the connection).
    • If prompted with a security alert about the server’s key, click “Accept” to trust the server.
  4. Log In:
    • Enter your username (e.g., root or another user with sudo privileges).
    • Type your password when prompted (note: passwords don’t display as you type for security).

Once logged in, you’ll see a terminal prompt (e.g., root@yourserver:~#).


Step 3: Update Your Server’s Package Lists

To ensure your server has the latest package information:

apt update

This command refreshes the list of available packages.


Step 4: Upgrade Installed Packages

Install any available updates for existing packages to keep your system secure:

apt upgrade -y

The -y flag automatically confirms the installation, saving you from manual prompts.


Step 5: Install Required Dependency Packages

Install tools and dependencies needed for Docker and future installations:

apt install curl nano git apt-transport-https ca-certificates gnupg2 software-properties-common -y

Here’s what these packages do:

  • curl: Downloads files from the web.
  • nano: A simple text editor.
  • git: Version control system.
  • apt-transport-https, ca-certificates, gnupg2, software-properties-common: Enable secure repository access.

Step 6: Install Docker

Follow these steps to install Docker on your server:

  1. Add Docker’s GPG Key:

    • For Debian:

      curl -fsSL https://download.docker.com/linux/debian/gpg | gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
    • For Ubuntu:

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

    This key ensures the Docker repository is trusted.

  2. Add Docker Repository:

    • For Debian:

      echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/debian $(lsb_release -cs) stable" | tee /etc/apt/sources.list.d/docker.list
    • For Ubuntu:

      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" | tee /etc/apt/sources.list.d/docker.list

    This adds Docker’s official repository to your system.

  3. Update Package Lists Again:

    apt update
  4. Install Docker:

    apt install docker-ce docker-ce-cli -y
  5. Verify Docker Installation:

    • Start and enable Docker:

      systemctl start docker
      systemctl enable docker
    • Check the version:

      docker --version

      You should see output like Docker version 27.3.1, build abc123.

  6. Test Docker:

    docker run hello-world

    This pulls and runs a test container, confirming Docker is working.


Step 7: Install Docker Compose

Docker Compose simplifies managing multi-container applications. Install it with these steps:

  1. Download Docker Compose:

    curl -L https://github.com/docker/compose/releases/download/v$(curl -Ls https://www.servercow.de/docker-compose/latest.php)/docker-compose-$(uname -s)-$(uname -m) > /usr/local/bin/docker-compose

    This command fetches the latest Docker Compose version dynamically.

  2. Set Execution Permissions:

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

    This makes the Docker Compose binary executable.

  3. Verify Installation:

    docker-compose --version

    You should see output like Docker Compose version v2.24.5.


Step 8: Navigate to the /opt Directory

To prepare for future projects (e.g., storing Docker Compose files), switch to the /opt directory:

cd /opt

The /opt directory is commonly used for optional software and configurations.


Step 9: Optional Configuration and Testing

  1. Run Docker Without Sudo (Recommended): Add your user to the docker group to avoid typing sudo for Docker commands:

    usermod -aG docker $USER

    Log out and back in (or reconnect via PuTTY) for this to take effect.

  2. Test Docker Compose:

    • Create a sample docker-compose.yml file:

      nano /opt/docker-compose.yml
    • Add this content:

      version: '3.8'
      services:
        web:
          image: nginx:latest
          ports:
            - "8080:80"
    • Save and exit (Ctrl+O, Enter, Ctrl+X).

    • Run the Compose file:

      cd /opt
      docker-compose up -d
    • Verify the container is running:

      docker ps
    • Open a browser and visit http://your-server-ip:8080 to see the Nginx welcome page.

    • Stop the containers:

      docker-compose down
  3. Configure Docker Daemon (Optional):

    • Edit Docker’s settings (e.g., for logging limits):

      nano /etc/docker/daemon.json
    • Add:

      {
        "log-driver": "json-file",
        "log-opts": {
          "max-size": "10m",
          "max-file": "3"
        }
      }
    • Restart Docker:

      systemctl restart docker

Step 10: Maintenance Tips

  • Update Docker:

    apt update && apt upgrade docker-ce -y
  • Update Docker Compose: Repeat Step 7.1 with the latest version.

  • Clean Up Unused Resources:

    docker system prune -a
  • Check Logs for Issues:

    journalctl -u docker

Troubleshooting

  • PuTTY Connection Fails:
    • Verify your server’s IP, port (22), and credentials.
    • Ensure SSH is enabled on your server (sudo systemctl status ssh).
  • Docker Not Starting:
    • Check status: systemctl status docker.
    • View logs: journalctl -u docker.
  • Docker Compose Errors:
    • Validate YAML: docker-compose config.
    • Ensure the binary is executable: ls -l /usr/local/bin/docker-compose.

Conclusion

Congratulations! You’ve connected to your VPS or root server using PuTTY, installed Docker and Docker Compose, and set up a basic configuration. You’re now ready to deploy containerized applications. Try experimenting with Docker Compose files to run databases, web servers, or full-stack apps. If you run into issues or want to explore advanced setups, leave a comment below or reach out!

Happy containerizing!


docker, how to, install

Leave a Reply