Skip to content

Commands

.. program:: docker

Images

.. option:: 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

.. option:: pull :

Get a docker image from dockerhub

docker pull <containername>:<tag>

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

Build

.. option:: build

To build an image from a Dockerfile

.. program:: 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.

.. option:: -t [/][:]

build docker with a given name.

.. option:: -f

using dockerfile located at a different location.

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

.. program:: docker

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

.. option:: run

Run a docker image already pulled or build.

.. program:: docker run

.. option:: -it

Run docker in interactive mode

docker run -it ubuntu

.. option:: -d

Run docker in deamonized

docker run -it -d ubuntu

.. option:: -v :

Link host folder to container folder

# Windows
docker run -v //c/data:/data ubuntu
# Linux
docker run -v /data:/data    ubuntu

.. option:: -p :

Forwared container port to host port

docker run -p 8080:80 ubuntu

.. option:: --name

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

.. option:: --entrypoint=

overwrite the CMD command within the dockerfile.

Bypass the ENTRYPOINT and run a bash shell

docker run -it --entrypoint=/bin/bash myimage -i
.. program:: docker

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

.. option:: exec

Run a command inside the docker image

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

Save and Load

.. option:: save

Save docker image

docker save busybox > busybox.tar
docker save myimage:latest | gzip > myimage_latest.tar.gz
.. option:: load

Load docker image

docker load < busybox.tar.gz