Skip to content

Running containers

Terminal window
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:

Terminal window
docker run --rm -it ubuntu:24.04 bash
Terminal window
docker ps # running containers
docker ps -a # all, including stopped ones
Terminal window
docker logs web # output of the main process
docker logs -f --tail 100 web # follow, starting from the last 100 lines
docker 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 usage
docker top web # processes inside the container

Copy files in and out:

Terminal window
docker cp web:/etc/nginx/nginx.conf ./nginx.conf
docker cp ./index.html web:/usr/share/nginx/html/
Terminal window
docker stop web # graceful stop (SIGTERM, then SIGKILL after 10 s)
docker start web
docker restart web
docker rm web # remove a stopped container
docker rm -f web # stop and remove in one go
Terminal window
podman ps --pod # show which pod each container belongs to
podman pod create --name app -p 8080:80
podman run -d --pod app nginx:1.27

Rootless Podman can’t publish ports below 1024 by default: use something like -p 8080:80 instead of -p 80:80.