Commands#
Images#
- images#
List all currently existing docker images
docker images
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#
- pull <containername>:<tag>#
Get a docker image from dockerhub
docker pull <containername>:<tag>
docker pull ubuntu:latest
docker pull ubuntu:20.10
docker pull ubuntu
Build#
- build <imageName>#
To build an image from a Dockerfile
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.
- -t [<repoName>/]<imageName>[:<tagName>]#
build docker with a given name.
- -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 <imagename>#
Run a docker image already pulled or build.
- -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 <container-name or container-id>
docker container rm <container-name or container-id>
Remove a container while running:
docker container rm -f <container-name or container-id>
Exec#
- exec <imagename> <command>#
Run a command inside the docker image
docker exec -it <dockername> <command>
docker exec -it ubuntu bash
Save and Load#
- save <image>#
Save docker image
docker save busybox > busybox.tar
docker save myimage:latest | gzip > myimage_latest.tar.gz
- load <image>#
Load docker image
docker load < busybox.tar.gz