Geavanceerd
Zoeken inHow-to-gidsen
Zo zoek je
  • Typ de taak in je eigen woorden (poort vinden, firewall openen, schijf vol) of plak een foutmelding tussen aanhalingstekens.
  • Kies "Snel antwoord" om alleen de commando's te zien, of een gebied om in één vakgebied te blijven.
  • "twee woorden" vindt precies die zin.
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken
Probeer:

How-to 24Containers · Docker

How to run a container and publish a port

Running a container is one line — running it right needs a few more options: a pinned image tag, a name, a restart policy, environment, a volume for data, and a published port that is open only where it should be. This how-to builds that line step by step, explains -p (and why -p 8080:80 can bypass your firewall), and shows how to check and fix "I can not reach my container".

  • 5 Lessen
  • 4 Commando's

Dit deel is nog niet vertaald. Je ziet de Engelse tekst.

$ als gewone gebruiker# als root (of met sudo)

24.1

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.

24.2

Step by step

  1. Pull a pinned image — never latest in production (you do not know what you get next time):
docker pull nginx:1.27
docker image inspect nginx:1.27 --format '{{.RepoDigests}}'
  1. Choose who may reach the port:
OptionWho can connect
-p 8080:80Everyone who can reach the server (all interfaces).
-p 127.0.0.1:8080:80Only the server itself — use this when Nginx or another proxy is in front.
-p 10.0.1.5:8080:80Only via that one interface/IP.
no -pOnly other containers in the same Docker network.
  1. 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
OptionWhy
--restart unless-stoppedStarts again after a crash and after a reboot.
--env-fileSettings and secrets from a file (600 rights), not in your shell history.
-v api-data:/app/dataData in a named volume — it stays when the container is replaced.
--memory, --cpusOne container can not eat the whole server.
--read-only --tmpfs /tmpThe app can not change its own files (safer).
  1. Replace a container with a new version (containers are not "updated", they are replaced):
docker pull registry.example.com/shop/api:1.4.3
docker stop api && docker rm api

Then run the same docker run line with the new tag. (Compose does this for you — see the Compose how-to.)

  1. Look inside when something is wrong:
docker port api
docker logs --tail 50 api
docker exec -it api sh
docker inspect api --format '{{.State.Status}} {{.RestartCount}}'

24.3

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.

Commando's in deze les

24.4

Common problems

ProblemCauseFix
port is already allocatedAnother 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 resetThe 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 startThe main process ended or crashed.docker logs NAME; docker ps -a shows the exit code.
Port open to the internet although UFW/firewalld deny itDocker's own rules come first.Publish on 127.0.0.1: and put a proxy in front.
Data gone after replacing the containerData was inside the container, not in a volume.Use -v NAME:/path for everything that must stay.
Rootless/Podman: can not publish port 80Ports below 1024 need root.Use a high port (8080) behind a proxy.

24.5

Go further

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.

Alle commando's in dit hoofdstuk