Running containers
docker run -d --name web -p 8080:80 nginx:1.27| Option | Meaning |
|---|---|
-d |
Detached: runs in the background |
--name web |
Name to refer to it (otherwise you get a random one) |
-p 8080:80 |
Publish port: host:container |
-e KEY=value |
Environment variable (--env-file .env to load a file) |
-v data:/var/lib/app |
Mount a volume or a host folder (see Volumes and networks) |
--rm |
Delete the container automatically when it stops |
-it |
Interactive with a terminal, for shells and one-off commands |
--restart unless-stopped |
Start it again after a crash or a reboot (Docker) |
One-off interactive container that disappears on exit:
docker run --rm -it ubuntu:24.04 bashdocker ps # running containersdocker ps -a # all, including stopped onesLook inside
Section titled “Look inside”docker logs web # output of the main processdocker logs -f --tail 100 web # follow, starting from the last 100 linesdocker exec -it web sh # open a shell in a running container (bash if the image has it)docker inspect web # full configuration and state (JSON)docker stats # live CPU, memory and network usagedocker top web # processes inside the containerCopy files in and out:
docker cp web:/etc/nginx/nginx.conf ./nginx.confdocker cp ./index.html web:/usr/share/nginx/html/Stop, start, remove
Section titled “Stop, start, remove”docker stop web # graceful stop (SIGTERM, then SIGKILL after 10 s)docker start webdocker restart webdocker rm web # remove a stopped containerdocker rm -f web # stop and remove in one goPodman extras
Section titled “Podman extras”podman ps --pod # show which pod each container belongs topodman pod create --name app -p 8080:80podman run -d --pod app nginx:1.27Rootless Podman can’t publish ports below 1024 by default: use something like -p 8080:80 instead of -p 80:80.