Skip to content

docker cmd

docker

Images

List all currently existing docker images

docker images
output
REPOSITORY              TAG                 IMAGE ID            CREATED             SIZE
docker101tutorial       latest              ef9eb5245be8        4 days ago          27.5MB
alpine/git              latest              94f8849864da        3 weeks ago         28.4MB
continuumio/anaconda3   latest              472a925c4385        7 weeks ago         2.73GB

Pull

Get a docker image from dockerhub

pull <containername>:<tag>
docker pull <containername>:<tag>

docker pull ubuntu:latest
docker pull ubuntu:20.10
docker pull ubuntu

Build

To build an image from a Dockerfile

build <imageName>
docker build
docker build -t <repoName>/<imageName>:<tagName> .

docker build -t local/app:latest .

Note

Please mind the dot . at the end.

As per the above command, Docker will start to build images automatically by reading the instructions from the Dockerfile saved in the current working directory.

build docker with a given name.

-t [<repoName>/]<imageName>[:<tagName>]
-f <path/to/dockerfile>

using dockerfile located at a different location.

docker build -f </path/to/a/Dockerfile> .

Remove dangling images

Docker images consist of multiple layers. Dangling images, are layers that have no relationship to any tagged images. They no longer serve a purpose and consume disk space.

docker rmi $(docker images -f dangling=true -q)

On Windows:

FOR /f "tokens=\*" %i IN ('docker images -f "dangling=true" -q')
DO docker rmi -f %i

List images and containers

docker image ls docker container ls docker container ls -a

Note

ls -a shows all containers with stopped ones.

To remove all stopped containers:

docker container prune

Run

Run a docker image already pulled or build.

run <imagename>
docker run
-it

Run docker in interactive mode

docker run -it ubuntu
-d

Run docker in deamonized

docker run -it -d ubuntu
-v <hostPath>:<containerPath>

Link host folder to container folder

# Windows
docker run -v //c/data:/data ubuntu
# Linux
docker run -v /data:/data    ubuntu
-p <hostPort>:<containerPort>

Forwared container port to host port

docker run -p 8080:80 ubuntu
--name <containername>

Use the --name flag to give the container a name. Otherwise it is generated randomly.

--entrypoint=<command>

overwrite the CMD command within the dockerfile.

Bypass the ENTRYPOINT and run a bash shell

docker run -it --entrypoint=/bin/bash myimage -i

Stop an remove a container

docker container stop docker container rm

Remove a container while running:

docker container rm -f

Exec

exec <imagename> <command>

Run a command inside the docker image

docker exec -it <dockername> <command>
docker exec -it ubuntu bash