Installing and using Docker
Docker bundles a program together with its whole runtime environment so it runs the same way on any server. This guide is based on an Ubuntu server.
1. Install
Use Docker's official install script.
curl -fsSL https://get.docker.com | sudo sh
To avoid adding sudo every time, add your account to the docker group and reconnect.
sudo usermod -aG docker $USER
Check that it installed correctly.
docker run --rm hello-world
If you see "Hello from Docker!", it worked.
2. Try running a container
Let's start a web server (Nginx).
docker run -d --name web -p 80:80 --restart unless-stopped nginx
-dRun in the background-p 80:80Connect the server's port 80 to the container's port 80--restart unless-stoppedStart again automatically even after the server reboots
Go to http://server-ip in your browser and you'll see the Nginx page.
3. Common commands
docker ps # List running containers
docker logs -f web # View logs (Ctrl + C to exit)
docker restart web # Restart
docker stop web # Stop
docker rm web # Remove (after stopping)
docker images # List downloaded images
docker system df # Disk space used by Docker
4. Several at once with Docker Compose
You can manage several containers together with a single configuration file. Create a compose.yaml file.
services:
web:
image: nginx
ports:
- "80:80"
restart: unless-stopped
docker compose up -d # Start
docker compose down # Stop and remove
docker compose logs -f # View logs
⚠️ Firewall warning
Ports opened with Docker (-p) are opened to the outside, ignoring your ufw firewall settings. For containers that should only be used inside the server, like a DB, bind them only to 127.0.0.1 as shown below.
docker run -d --name db -p 127.0.0.1:3306:3306 -e MARIADB_ROOT_PASSWORD=long-password mariadb
For firewall settings, see Opening firewall ports.
My disk is full
Unused images and containers take up a lot of disk space as they pile up.
docker system prune
This cleans up containers that aren't running and images you don't use. Check that it's okay to delete them, then type y.