Joona Viertola

Learning journal

Docker cheat sheet

What is it

Closest similiar technology is virtual machines running operating systems. Docker instead of running whole operating systems, Docker runs just Linux kernel and on top of that containers (apps).

Sales pitch

Much easier way to handle environments. Development environment can be build with configuration file and easily transformed into production environment. This reduces risk of environment differences.


  • Image
    Package of pieces needet to run software. Includes code, a runtime, libraries, environment variables, and config files.
  • Container
    Runtime instance of image in memory.
  • Service
    Container in production. A service runs only one container, but it controls how it's run. This includes how many replicas of container is run and what happens if container fails. This vehavior is configured with docker-compose.yml file.
  • Swarm
    “Dockerized” cluster. Running multiple containers in multiple machines.
  • Node
    After machine (physical or virtual) has been joined into swarm, it's called node.
  • Swarm manager
    Machine in swarm (node) which has ability to run command and authorize other machines to join.
  • Worker
    Machine in swarm which has no permissions like swarm manager.
  • Stack
    A group of interrelated services that share dependencies, and can be orchestrated and scaled together. A single stack is capable of defining and coordinating the functionality of an entire application.
  • Docker registery / hub
    Users can push their images to public and share them.

How-to use container

Container build

To build your own container, you need to write Dockerfile. Example from official Docker manual:

# Use an official Python runtime as a parent image
FROM python:2.7-slim

# Set the working directory to /app

# Copy the current directory contents into the container at /app
ADD . /app

# Install any needed packages specified in requirements.txt
RUN pip install -r requirements.txt

# Make port 80 available to the world outside this container

# Define environment variable

# Run when the container launches
CMD ["python", ""]

To build this container, go to Dockerfile's directory and execute next command:
docker build -t {container name} .

Dockerfile commands

  • FROM
    Define base image to use, like Ubuntu.
    Official reference
    Set metainformation about container like name, vendor, version & license.
    Official reference
  • RUN
    Commands which will be run when building a container image. Example. apt-get commands are ran here.
    Official reference
  • CMD
    Commands run after container runs.
    Official reference
    Configures a container that will run as an executable.
    Official reference
    Define ports which will be available for container.
    Official reference
  • ADD / COPY
    Both commands have same purposes with different possibilities. Copy is couraged to use.
    ADD Official reference
    COPY Official reference
    Used to expose container's database storage, configuration storage or files/folders.
    Official reference
  • USER
    Change user. Good practice when there is no need for root priviledges. This usage still needs users and groups to be created with RUN command.
    Official reference
    Working directory inside the container. If path doesn't already exist, it's created.
    Official reference
    Runs following Docker commands after current image has build. (Dockerfile got read.)
    Official reference


  • docker run hello-world Download hello world container if not already and run it.
  • docker build -t friendlyhello . Build an app with a tag (-t) friendlyhello fromn current directory. Directory has configuration file named Dockerfile.
  • docker image ls List of machines local Docker image registery.
  • docker images Alias for docker image ls.
  • docker container stop ${DOCKER ID} Stop container.
  • docker run -p 4000:80 friendlyhello Run friendlyhello app. Forward hosts 4000 port to app's 80 port.
  • docker run -d friendlyhello Run app in backround (detached mode).
  • docker login Login into service.