The Basic Command
docker run -d --name web -p 8080:80 nginx:stableThis starts Nginx in the background, names the container web, and makes its port 80 reachable on port 8080 of the server. If the image is not on the server yet, Docker pulls it first. Options always go before the image name; anything after the image name is the command the container runs instead of its default.
Without -d, the container runs in the foreground and its output fills your terminal. Ctrl+C then stops it.
The Options You Use Most
| Option | What it does |
|---|---|
-d | Runs in the background |
--name web | Sets a fixed name |
-p 8080:80 | Publishes a port |
-v data:/data | Mounts a volume or folder |
-e KEY= | Sets a variable |
--env- | Reads variables from a file |
--rm | Removes the container on exit |
-it | Opens an interactive terminal |
--restart | Sets the restart policy |
-w /app | Sets the working folder |
--user 1000 | Runs as another user ID |
A Realistic Server Example
docker run -d --name db \
-p 127.0.0.1:5432:5432 \
-v pgdata:/var/lib/postgresql/data \
-e POSTGRES_PASSWORD=STRONG_PASSWORD \
--restart unless-stopped \
postgres:16The mount path /var/lib/postgresql/data is right for PostgreSQL 17 and older; the official image for version 18 and later expects the volume at /var/lib/postgresql. Three details matter here. The port is bound to 127.0.0.1, because a port published by Docker is reachable from the internet even when UFW denies it, and firewalld does not stop it either, since Docker adds its own zone and forwarding policy; see Docker port mapping. The data lives in the named volume pgdata, so it survives when the container is replaced; see Docker volumes vs bind mounts. And the restart policy brings the database back after a reboot; see Docker restart policies.
Environment Variables
Pass single values with -e and many values with --env-file, a file with one KEY=value per line. Values set this way are visible to anyone who can run docker inspect on the server, so protect the env file with chmod 600 and keep it out of Git. The general rules are in how to set environment variables in Linux.
One-Off and Interactive Containers
docker run --rm -it ubuntu:24.04 bash
docker run --rm alpine cat /etc/os-releaseThe first command opens a shell in a fresh Ubuntu container; when you leave the shell, --rm deletes the container. The second runs one command and exits. Both are handy for testing without leaving stopped containers behind.
Limit Memory and CPU
docker run -d --name worker --memory 512m --cpus 1.5 myimage:1.0Without limits, one container can use all the memory of the server. With --memory, a container that goes over its limit has a process inside it ended by the kernel's OOM killer, instead of a process elsewhere on the server; see the OOM killer. docker stats shows the live use of each container.
run vs start vs exec
| Command | What it does |
|---|---|
docker run | Creates a new container |
docker start web | Starts a stopped one again |
docker exec -it web sh | Runs a command in a running one |
Settings such as ports, volumes, and variables are fixed when the container is created; only a few, such as the limits and the restart policy, can be changed later with docker update. To change the others, remove the container and run it again with the new options; the data in volumes stays. A shell inside a running container is covered in docker exec.
Common Errors
- The container name is already in use: a container with that name exists, perhaps stopped. Start it, or remove it as in Docker remove container.
- Port is already allocated: another container or a service on the server uses that host port. Pick another port or stop the other service.
- The container exits at once: its main process ended or failed. Read the reason with docker logs.
- Invalid reference format: a typo or a capital letter in the image name; see Docker invalid reference format.
When a docker run command grows past a few lines, move it into a compose.yaml file and start it with docker compose up -d.
Updated:
Linux VPS with Full Root Access
MUVCloud cloud servers run on KVM with Enterprise NVMe SSD storage and full root access, in Turkey, the Netherlands, and the USA.
More Guides
No Limits on Service or Support
You are not on your own as your projects grow. Our support engineers are ready 24/7, on several channels, whenever you need them.

- 100% customer satisfaction goal
- Our own hardware
- 24/7 ticket and email support
- 4 countries with locations