Docker on a DigitalOcean Droplet is surprisingly more about managing the lifecycle of your containers than just installing the software.

Let’s get a basic Docker setup running on a fresh Ubuntu Droplet.

First, log into your Droplet via SSH:

ssh root@192.0.2.10

Now, update your package lists and upgrade existing packages. This is standard practice on any new server.

apt update && apt upgrade -y

Next, install the apt-transport-https, ca-certificates, curl, gnupg, and lsb-release packages. These are prerequisites for adding Docker’s official repository.

apt install -y apt-transport-https ca-certificates curl gnupg lsb-release

Add Docker’s official GPG key. This ensures that the packages you download are authentic.

mkdir -p /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | gpg --dearmor -o /etc/apt/keyrings/docker.gpg

Set up the stable Docker repository. This tells your system where to find the Docker packages.

echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
  $(lsb_release -cs) stable" | tee /etc/apt/sources.list.d/docker.list > /dev/null

Update your package index again to include the new Docker repository.

apt update

Install Docker Engine, containerd.io, and docker-compose-plugin. The plugin makes docker compose available as a top-level command.

apt install -y docker-ce docker-ce-cli containerd.io docker-compose-plugin

Verify that Docker is installed and running by checking its version and running a simple container.

docker --version
docker run hello-world

If hello-world runs successfully, you’ll see a message indicating Docker is working.

Now, let’s add your user to the docker group. This allows you to run Docker commands without sudo. You’ll need to log out and log back in for this change to take effect.

usermod -aG docker $USER

Log out and log back in:

exit
ssh your_user@192.0.2.10

Test running a container without sudo:

docker run hello-world

A common pattern is to run a web server. Here’s how you’d start Nginx and expose its default port (80) to your Droplet’s IP address.

First, pull the latest Nginx image:

docker pull nginx:latest

Then, run the container, mapping port 80 on your Droplet to port 80 inside the container, and naming the container my-nginx. The -d flag runs it in detached mode.

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

You can check if it’s running with docker ps.

docker ps

You should see my-nginx in the list. Visiting your Droplet’s IP address in a web browser should now show the Nginx welcome page.

The real power comes when you start managing multiple containers and their configurations. This is where docker-compose shines. Create a docker-compose.yml file to define your multi-container applications.

For example, a simple web application with a database:

# docker-compose.yml
version: '3.8'

services:
  web:
    image: nginx:latest
    ports:
      - "8080:80"
    volumes:
      - ./html:/usr/share/nginx/html
  db:
    image: postgres:13
    environment:
      POSTGRES_DB: mydatabase
      POSTGRES_USER: myuser
      POSTGRES_PASSWORD: mypassword
    volumes:
      - db_data:/var/lib/postgresql/data

volumes:
  db_data:

To use this, create a directory named html in the same directory as your docker-compose.yml file and place an index.html file inside it.

Then, run:

docker compose up -d

This will build and start your services. You can access the Nginx container on port 8080 of your Droplet.

The volumes section in docker-compose.yml is crucial. db_data:/var/lib/postgresql/data creates a named volume called db_data and mounts it into the PostgreSQL container. This means your database data persists even if the container is stopped or removed. If you simply mounted a host directory like ./pgdata:/var/lib/postgresql/data, the data would be stored directly on your Droplet’s filesystem, which can be harder to manage and back up compared to Docker’s managed volumes.

To stop and remove the services defined in your docker-compose.yml:

docker compose down

If you need to remove the named volumes as well (which deletes the data):

docker compose down -v

When you’re done, you can uninstall Docker:

apt remove docker docker-ce docker-ce-cli containerd.io docker-compose-plugin
apt autoremove -y
rm -rf /var/lib/docker
rm -rf /etc/docker
rm /etc/apt/sources.list.d/docker.list
rm /etc/apt/keyrings/docker.gpg

The next hurdle is often understanding how to manage container networking, especially when you need services to communicate securely or expose specific ports to the outside world.

Want structured learning?

Take the full Digitalocean course →