What Is Docker and How to Use It on a VPS? Print

  • 0

What Is Docker?

Docker is an open-source platform that uses containerization to package applications and their dependencies into lightweight, portable containers. Containers run consistently across environments, making them ideal for deploying applications on a VPS. Unlike virtual machines, Docker containers share the host OS, reducing overhead and improving performance, perfect for developers and businesses needing scalable, isolated environments.

Prerequisites

  • A VPS with Ubuntu or CentOS (e.g., VPS.DO’s 1H2G plan with Ubuntu 20.04/22.04).
  • Root or sudo access, available via VPS.DO’s SolusVM control panel.
  • Basic SSH and terminal knowledge.

Step-by-Step Guide to Using Docker on a VPS

1. Connect to Your VPS

Access your VPS via SSH. For VPS.DO users, use credentials from the SolusVM panel:

ssh root@your_vps_ip

2. Install Docker

Update the system and install Docker:

sudo apt update && sudo apt upgrade -y  # Ubuntu
sudo apt install docker.io -y
sudo systemctl start docker
sudo systemctl enable docker

For CentOS:

sudo yum update -y
sudo yum install docker -y
sudo systemctl start docker
sudo systemctl enable docker

3. Verify Docker Installation

Check Docker is running:

docker --version
docker run hello-world

The hello-world container should output a success message.

4. Pull and Run a Container

Pull an image (e.g., Nginx web server) from Docker Hub:

docker pull nginx

Run the container, mapping port 80 on the VPS to port 80 in the container:

docker run -d -p 80:80 --name my-nginx nginx

Visit http://your_vps_ip to see the Nginx welcome page.

5. Manage Containers

  • List running containers:
    docker ps
    
  • Stop a container:
    docker stop my-nginx
    
  • Remove a container:
    docker rm my-nginx
    
  • View all images:
    docker images
    

6. Create a Custom Dockerfile

Build your own application container. Create a directory and Dockerfile:

mkdir my-app && cd my-app
nano Dockerfile

Example Dockerfile for a simple Node.js app:

FROM node:16
WORKDIR /app
COPY . .
RUN npm install
CMD ["node", "index.js"]

Create a basic index.js:

echo 'console.log("Hello from Docker!");' > index.js

Build and run:

docker build -t my-app .
docker run -d -p 3000:3000 my-app

7. Optimize and Secure

  • Limit Resources: Restrict container CPU and memory:
    docker run -d -p 80:80 --memory="512m" --cpus="0.5" nginx
    
  • Firewall: Allow Docker ports (e.g., 80, 443) with UFW:
    sudo ufw allow 80
    sudo ufw allow 443
    sudo ufw enable
    
  • Monitor: Use VPS.DO’s SolusVM panel to track resource usage and ensure containers don’t overload the VPS.

8. Back Up and Scale

  • Save images for backup:
    docker commit my-nginx my-nginx-backup
    docker save -o nginx-backup.tar my-nginx-backup
    
  • Scale with Docker Compose for multi-container apps. Install:
    sudo apt install docker-compose -y
    
    Create a docker-compose.yml for complex setups.

Conclusion

Docker simplifies application deployment with lightweight containers, ideal for VPS environments. With VPS.DO’s KVM-based VPS and full root access, you can efficiently run and manage containers. Experiment with Docker Hub images and custom builds to suit your project, and use VPS.DO’s 24/7 support for assistance.


Was this answer helpful?

« Back