A Complete Guide to Setting Up Docker and Deploying Containers on VPS

0
87

Introduction

Docker has revolutionized the way developers deploy and manage applications. By allowing applications to run in isolated containers, Docker simplifies development workflows, ensures consistency across environments, and makes it easy to scale applications. In this guide, you’ll learn how to install Docker on a VPS, deploy your first container, and manage containerized applications effectively.

Whether you’re hosting web applications, APIs, or microservices, Docker provides the flexibility and performance needed for modern infrastructure. Let’s dive into the complete setup process.

Prerequisites

Before starting, ensure you have the following:

  • An Ubuntu 22.04 VPS
  • Root or sudo privileges
  • Basic knowledge of Linux commands

Step 1: Update Your System

It’s important to start with an updated system to avoid compatibility issues. Run the following commands to update all packages:

sudo apt update && sudo apt upgrade -y

This command ensures all existing packages are current, reducing potential conflicts during Docker installation.

Step 2: Install Required Dependencies

Docker requires some dependencies for secure communication and repository management. Install them with:

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

These packages help in adding external repositories and handling HTTPS connections securely.

Step 3: Add Docker’s Official GPG Key and Repository

Next, add Docker’s GPG key to verify the authenticity of packages:

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

Then, add the Docker repository to your APT sources:

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

Step 4: Install Docker Engine

Update the package database with the Docker packages from the newly added repo:

sudo apt update

Now, install Docker:

sudo apt install docker-ce -y

Verify the installation:

docker –version

You should see the installed Docker version.

Step 5: Manage Docker as a Non-Root User

By default, Docker requires root privileges. To run Docker commands without sudo, add your user to the Docker group:

sudo usermod -aG docker ${USER}

Apply the new group membership:

newgrp docker

Now you can run Docker commands as a regular user.

Step 6: Deploy Your First Docker Container

To verify Docker’s functionality, run the hello-world container:

docker run hello-world

This command pulls a test image from Docker Hub and runs it in a container. If everything is set up correctly, you’ll receive a confirmation message.

Step 7: Running a Web Application with Docker

Let’s deploy a simple NGINX web server to demonstrate Docker’s real-world use. Pull and run the NGINX image:

docker run -d -p 80:80 nginx

Here’s what this command does:

  • -d: Runs the container in detached mode (in the background).
  • -p 80:80: Maps port 80 of the host to port 80 of the container.
  • nginx: Specifies the image to run.

Visit your server’s IP address in a browser, and you should see the NGINX welcome page.

Step 8: Managing Docker Containers

To list running containers:

docker ps

To view all containers (including stopped ones):

docker ps -a

To stop a running container:

docker stop <container_id>

To remove a container:

docker rm <container_id>

Step 9: Working with Docker Images

To list downloaded images:

docker images

To remove an image:

docker rmi <image_id>

Step 10: Using Docker Compose (Optional)

Docker Compose allows you to manage multi-container applications. Install it with:

sudo apt install docker-compose -y

Example docker-compose.yml for running NGINX:

version: ‘3’
\nservices:
\n web:
\n image: nginx
\n ports:
\n – \”80:80\”

Run the composition:

docker-compose up -d

Conclusion

Congratulations! You’ve successfully set up Docker on your VPS and deployed containers. Docker streamlines application deployment, ensures consistency across environments, and simplifies scaling. As you explore Docker further, consider using orchestration tools like Kubernetes for managing complex containerized applications.

Stay updated with Docker’s official documentation for more advanced features, and keep experimenting to unlock the full potential of containerization!