l2t

Docker Cheatsheet

Learn about all docker useful commands

How to create docker image from Dockerfile?

To create an image from Dockerfile use following commands:

docker build -t user/app .​

How to list docker images?

To view list of all available docker images on your computer run:

# list all the docker images
docker images

# check the history of a specific image
docker history <image-name>

# tag an image
docker tag <image-name> <name:v1>

# push image to repository
docker push [registry/][username/]<image-name>[:tag]​

How to remove docker image?

To remove a docker image follow commands below:

# first, list all images
docker images

# find image id from above command
# now remove image using id
docker rmi <id>​

How to list current process in docker?

To list running images in docker run following commands:

# list all running images
docker ps

# list all running/exited processes
docker ps -a

# to kill a running process
# grab the id from above command
docker kill <id>​

How to create a container from image?

To create a new container without starting it using existing image run:

# creates a docker container but does not start
docker create <image-name>

# start an existing container
docker start <container-id>

# create and start a container
docker run <image-name>

# stop a container
docker stop <container-id>

# remove a container
docker rm <container-id>

# execute a new process in an existing container
docker exec -it <container-id> bash

# check the process log of a container
docker logs -f <container-id>

# list all the running containers
docker ps

# list all containers stopped/running
docker ps -a​

How to use docker hub?

Followings are some of the commands related to docker hub:

# login to docker hub
docker login

# pull a new image from docker hub
docker pull <image>

# push local image to docker hub
docker push <user/image>

# search for image on docker hub
docker search <term>​

How to clean up docker container?

To kill docker container run following commands:

# kill specific container
docker kill <containerID>

# kill all running containers
docker kill $(docker ps -q)

# remove a container that is not running
docker rm <containerID>

# delete containers that are not running
docker rm $(docker ps -a -q)​

Docker volumes

# create a new docker volume
docker volume create <name>

# list all the volumes
docker volume ls

# remove the volume
docker volume rm <name>

# inspect a volume info
docker volume inspect <name>

How to create multiple containers?

To create or destroy multiple containers run following docker compose commands:

# run container in forground
docker-compose up

# run container in background
docker-compose up -d

# destroy/stop running containers
docker-compose down

Dockerfile arguments

Argument Description
FROM Defines the base image for current build
MAINTAINER Name of the author
RUN list of commands to execute on top of the base image
CMD commands to run on container startup
EXPOSE port to expose to host
ENV sets the environment variable
ADD copy new files and directories or remote file
COPY copy files from host to destination on container working dir
USER Sets the username or UID to use when running the image
WORKDIR Sets the working directory for any RUN, CMD, ENTRYPOINT, COPY, and ADD commands
ARG Defines a variable that users can pass at build-time to the builder using --build-arg

Reference: https://design.jboss.org/redhatdeveloper/marketing/docker_cheatsheet/cheatsheet/images/docker_cheatsheet_r3v2.pdf