Commands

Docker CLI

Container Status

Command
Description

docker ps

List all the running containers in a machine with details like container id, command being executed, start time etc.

docker ps --all

List all the containers ever ran on a machine.

Container Creation

Command
Description

docker run <image-name>

Creates a docker container with default command as per image.

docker run <image-name> <command>

Overrides default command of an image.

docker run -it <image-name> <command>

Runs a container and attaches a psuedo terminal to the container, replacing the default command in the image. -i tag attaches STDIN to the container, when not specified, it only attaches STDOUT of the container.

docker run -p <port of host>:<port of container> <image-name>

Runs the docker image with port mapping.

docker create <image-name>

Create a container from an image.

docker start <container-id>

Start a container with the given container id.

docker start -a <container-id>

Start a container with the given container id and attach the STDOUT/STDERR and also can forward the signals.

docker exec -it <container-id> <command>

Execute an additional command inside a container, -i opens an STDIN of the command and -t allocates a pseudo terminal (TTY), <command> could also be shell like sh, zsh, bash etc which will open a shell directly into the container.

Container/s Stop

Command
Description

docker stop <container-id>

Stops the running docker container, by sending a signal SIGTERM and gracefully terminate the container, if the container does not stop in 10 second, docker falls back to docker kill like behavior.

docker kill <container-id>

Kills the running docker container, by sneding a signal SIGKILL and abruptly terminate the container.

docker system prune

Remove all the stopped containers, unused network, dangling images and build cache.

Container Debugging

Command
Description

docker logs <container-id>

Prints out all the logs emitted by the container.

docker attach <container-id>

Attaches STDIN, STDOUT, STDERR of the running container to the terminal.

Container Volume

Command
Description

docker run -v <volume in the host>:<volume in the container> <image-id>

This command will set up a volume reference in the host to that of a container.

docker run -v <volume in the container> <image-id>

This command will set up a volume reference but not map to any volume in the host and thereby not overriding the contents of the same folder in the container.

Image Creation

Command
Description

docker build <docker-file-path>

Builds a docker image from a given docker file. By default this command will look for a file named Dockerfile.

DOCKER_BUILDKIT=0 docker build <docker-file-path>

Builds a docker image from a given docker file while docker BuildKit is disabled.

docker build -t <tag-name> <docker-file-path>

Builds a docker image from a docker file and adds a tag of the standard form docker-id/project-name:version.

docker tag <container-id> <tag-name>

Creates a tag for a given container id.

docker commit -c 'CMD [ "<command>" ]' <container-id>

Creates a docker image from a container and adds a default command.

docker commit -c "CMD '<command>'" <container-id>

(only windows) Creates a docker image from a container and adds a default command.

Docker Compose

Container Status

Command
Description

docker compose ps

List all the running containers in a compose project with details like container id, command being executed, start time etc.

Container Operations

Command
Description

docker-compose up

Creates and starts containers defined in docker-compose.yml file located in the current working directory and also sets up a default network and join the containers in docker-compose together.

docker-compose up --build

Builds images before running them, typically used to perform a rebuild.

docker-compose up --d

Run containers in background.

docker-compose down

Stops running container and removes them.

Docker Hub

Command
Description

docker login -u <username> -p <password>

To login to docker hub through command line

docker login -u <username> --password-stdin <password>

To login to docker hub through command line, password will be obtained from standard input

docker push <image-id>/<image-tag>

To push image to docker hub

Reference

Last updated