Dit deel is nog niet vertaald. Je ziet de Engelse tekst.
$ als gewone gebruiker# als root (of met sudo)
The quick answer
docker run -d --name web -p 8080:80 --restart unless-stopped nginx:1.27
docker ps
CONTAINER ID IMAGE COMMAND STATUS PORTS NAMES
3f1c9e2b7a4d nginx:1.27 "/docker-entrypoint.…" Up 5 seconds 0.0.0.0:8080->80/tcp web
curl -sI http://127.0.0.1:8080 | head -1
HTTP/1.1 200 OK-p HOST:CONTAINER — port 8080 on the server goes to port 80 inside the container.
Step by step
- Pull a pinned image — never
latestin production (you do not know what you get next time):
- Choose who may reach the port:
| Option | Who can connect |
|---|---|
-p 8080:80 | Everyone who can reach the server (all interfaces). |
-p 127.0.0.1:8080:80 | Only the server itself — use this when Nginx or another proxy is in front. |
-p 10.0.1.5:8080:80 | Only via that one interface/IP. |
no -p | Only other containers in the same Docker network. |
- A full production-style run:
docker run -d --name api --restart unless-stopped -p 127.0.0.1:3000:3000 -e NODE_ENV=production --env-file /srv/api/api.env -v api-data:/app/data --memory 512m --cpus 1 --read-only --tmpfs /tmp registry.example.com/shop/api:1.4.2| Option | Why |
|---|---|
--restart unless-stopped | Starts again after a crash and after a reboot. |
--env-file | Settings and secrets from a file (600 rights), not in your shell history. |
-v api-data:/app/data | Data in a named volume — it stays when the container is replaced. |
--memory, --cpus | One container can not eat the whole server. |
--read-only --tmpfs /tmp | The app can not change its own files (safer). |
- Replace a container with a new version (containers are not "updated", they are replaced):
Then run the same docker run line with the new tag. (Compose does this for you — see the Compose how-to.)
- Look inside when something is wrong:
Check that it worked
From the server: curl -sI http://127.0.0.1:3000. From another machine — only if the port should be public: nc -zv SERVER 8080. A port bound to 127.0.0.1 must not answer from outside.
Common problems
| Problem | Cause | Fix |
|---|---|---|
port is already allocated | Another container or program uses that host port. | docker ps, ss -ltnp; choose another host port or stop the other one. |
curl: (52) Empty reply / connection reset | The app inside listens on 127.0.0.1 only. | Make the app listen on 0.0.0.0 inside the container. |
| Container stops right after start | The main process ended or crashed. | docker logs NAME; docker ps -a shows the exit code. |
| Port open to the internet although UFW/firewalld deny it | Docker's own rules come first. | Publish on 127.0.0.1: and put a proxy in front. |
| Data gone after replacing the container | Data was inside the container, not in a volume. | Use -v NAME:/path for everything that must stay. |
| Rootless/Podman: can not publish port 80 | Ports below 1024 need root. | Use a high port (8080) behind a proxy. |
Go further
- Command pages: docker, curl, ss.
- Next how-tos: See the logs of a container, Run an app with Docker Compose, Set up Nginx as a reverse proxy, Limit CPU and memory of a container.
- Learn it: Docker — Run, Docker — Limits, Docker — Ports, Docker — Volumes.
- Full guide: Install and set up Docker. Safer runtime: Docker — Runtime security.
1What is the difference between -p 8080:80 and -p 127.0.0.1:8080:80?
The first is open on all interfaces (often the internet); the second only on the server itself.
2Why should data live in a volume (-v)?
Containers are replaced on every update; data inside the container is lost, data in a volume stays.