Docker cheat sheet

What is Docker?

Docker is a tool used to deploy software across different platform without running into compatibility issues.

A Docker image is a lightweight, standalone, executable package of software that includes everything needed to run an application (code, libraries, settings, etc…).

A running instance of an image is called a container. A container is a standard unit of software that packages up code and all its dependencies so the application runs quickly and reliably from one computing environment to another.

Cheat sheet

Check Docker installation

docker version
docker run hello-world

List docker images

docker images

Download a Docker image

docker pull IMAGE_NAME

List running containers

docker ps
docker container ls

List all containers

docker ps -a

Stop a container

docker stop CONTAINER_ID

Remove a container

docker rm CONTAINER_ID

Run a docker image

docker run -it --rm IMAGE_ID

Options of the run command

-u $(id -u):$(id -g)       # assign a user and a group ID
--gpus all                 # allow GPU support
-it                        # run an interactive container inside a terminal
--rm                        # automatically remove the container after exiting
--name my_container        # give it a friendly name
-v ~/docker_ws:/notebooks  # share a directory between the host and the container
-p 8888:8888               # define port 8888 to connect to the container (for Jupyter notebooks)

Open a new terminal in a running container

docker exec -it CONTAINER_ID bash

Create a new Docker image

There are 2 main methods.

Method 1 : Using Dockerfile

Create a file called Dockerfile from your host machine

vim Dockerfile
# getting base Ubuntu image
FROM ubuntu

# file author / maintainer
LABEL maintainer=<your_email_address>

# update the repository sources list
RUN apt-get update

# print Hello World
CMD ["echo", "Hello World"]

Build the image

docker build -t myimage:0.1 .

Run the image

docker run myimage:0.1

Method 2 : Commit a Docker image from a running container

Modify a running container and run this in another terminal

docker commit CONTAINER_ID my_new_image

Useful links

Leave a Reply