Docker Common Commands
Most common Docker commands.
Docker Commands
Command | Description |
---|---|
docker build -t image_name directory_path | build a Docker image from Dockerfile |
docker run image_name | run a container from image |
docker ps | list running containers |
docker ps -a | list all containers |
docker stop container_name/container_id | stop a container |
docker kill container_name/container_id | kill a container |
docker restart container_name/container_id | restart a container |
docker rm container_name/container_id | remove a container |
docker container prune | remove all stopped container |
docker exec -it container_name/container_id /bin/bash | create bash session for a running container |
docker images | list images |
docker pull name_name | Pull an image or a repository from a registry |
docker rmi image_name/image_id | remove image by name or id |
use --help
option to search for help. If you need to find the usage for docker run
. execute docker run --help
.
Docker run options
docker run
is the most used command. Here are the common options for docker run
.
Option | Description |
---|---|
--rm | Automatically remove the container when it exits |
-d | Run container in background |
-it | interactive mode |
-p host_port:container_port | bind container port to host port |
Example to run nginx container with name ‘nginx-test’ on host’s port 8080
1 | docker run --rm -d -p 8080:80 --name nginx-test nginx:alpine |
Example to start an alpine container in interactive mode
1 | docker run --name alpine-test --rm -it alpine /bin/sh |
It is also very common to use docker exec
to connect to a running container.
1 | docker exec -it fd8a3021e976 /bin/sh |
Reference