Docker containers management
How to run and manage docker containers
✔️ Create container from an image
docker container run <image>
# remove container after command exits
docker container run --rm <image>
# detach (run in background) and map host port 3000 to container port 80
docker container run -d -p 3000:80 <image>
# name container and override CMD/ENTRYPOINT Dockerfile directive with interactive shell/pseudo tty (-i -t)
docker container run --name <name> -i -t <image> /bin/sh
✔️ List all containers (running and stopped)
docker container ls -a
✔️ Stop container
# normal container exit code is zero
docker container stop <container>
✔️ Restarts stopped container (run in background, interactive, attach : overrides interactive)
# start in background
docker container start <container>
# start in interactive mode
docker container start -i <container>
# attach container stdout / stderr to current shell
docker container start -a <container>
✔️ Print container logs
# prints CMD/ENTRYPOINT Dockerfile directive output since container startup
docker logs <container id>
✔️ Enter a running container (shell may vary)
# switch the container to interactive mode and start a shell
docker exec -i -t <container> /bin/bash
✔️ Gracefully stop container
# graceful shutdown (SIGTERM)
docker container stop <container>
# forced shutdown (SIGKILL)
docker container kill <container>
✔️ Delete a stopped container (specified, all stopped)
# delete a specific container
docker container rm <container>
# delete all stopped containers
docker container prune -f