How to Install and Use Docker on Your Ubuntu Server 24.04 VPS
Hey folks! If you're like me, you might be exploring Docker to manage your applications more efficiently on your Ubuntu Server 24.04 VPS. In this post, I'll show you how I installed Docker and how you can get started using it.
Why Docker?
- Portability: Your Docker containers will run the same way locally or on any cloud platform.
- Isolation: This is key! Each container runs in its own little world, preventing conflicts and improving security.
- Security: By keeping Docker isolated, you reduce potential attack surfaces.
- Cleanliness: Your VPS stays tidy since applications are contained.
Let's Get It Done!
Prerequisites
- A freshly installed Ubuntu 24.04 VPS
- Basic Linux command-line knowledge
- A user account with
sudo
privileges - An SSH connection to your server
Step 1: System Update
Before we start, let's make sure your system is up-to-date:
sudo apt update && sudo apt upgrade
Expected Output: You'll see a list of packages being checked and potentially upgraded. If there are updates, you'll be prompted to confirm the installation.
Step 2: Install Docker
We'll use the official Docker repository to get the latest version:
# Add Docker's official GPG key:
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
# Expected output: OK
# Add the Docker repository:
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
# Update the package index again:
sudo apt update
# IMPORTANT: Verify you're pulling from the correct repository
apt-cache policy docker-ce
Expected Output:
docker-ce:
Installed: (none)
Candidate: 5:24.0.5-1~ubuntu24.04~lunar
Version table:
5:24.0.5-1~ubuntu24.04~lunar 500
500 https://download.docker.com/linux/ubuntu lunar/stable amd64 Packages
If the "Candidate" line points to the correct version and the URL includes https://download.docker.com
, you're good to go!
# Install Docker Engine, containerd, and Docker Compose:
sudo apt install docker-ce docker-ce-cli containerd.io docker-compose-plugin
# Check Docker status to ensure it's running
sudo systemctl status docker
Expected Output: You should see a status message similar to this:
● docker.service - Docker Application Container Engine
Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
Active: active (running) since Mon 2024-01-01 12:34:56 PST; 1 weeks 3 days ago
...
If it shows "Active: active (running)", Docker is working as expected.
Step 3: Verify Docker Installation
Run a quick test to make sure Docker is working:
Bash
sudo docker run hello-world
Expected Output:
You should see a series of messages, ending with:
Hello from Docker!
This message shows that your installation appears to be working correctly.
...
Step 4: Manage Docker as a Non-Root User (Recommended)
For convenience and security, add your user to the docker
group:
sudo usermod -aG docker $USER
Expected Output: No output, but after logging out and back in, you should be able to run Docker commands without sudo
.
Step 5: Using Docker
We'll focus on keeping things isolated, so no port opening is needed. You'll use tools like:
- SSH Tunneling: Securely access applications inside containers.
- Docker Volumes: Share files between your VPS and containers.
- Docker Networks: Create private networks for your containers to communicate.
Example: Running a Container and Accessing it via SSH Tunnel
# Run a container in detached mode:
docker run -d --name my-container ubuntu
# Expected output: Unique container ID (e.g., a62079e23c71)
# Create an SSH tunnel (assuming the app in the container is listening on port 80):
ssh -L 8080:localhost:80 user@your_vps_ip
Now, visit http://localhost:8080
in your web browser to access the application inside the container!
Next Steps
- Explore Docker Compose to manage multi-container applications.
- Learn about Docker volumes for persistent storage.
- Dive into Docker networking to create isolated networks.
Let me know if you have any questions. Happy Dockering!