Basic Dockerfiles

Docker lets you create and run containers. Containers are similar to Virtual Machines but use less resources. They are less secure than VMs.

Docker creates containers from images. You can create a docker image using a Dockerfile. This file contains the following:

  1. Which docker image to use as a base for the new image.
  2. Any files that need to be copied into the container.
  3. Commands to setup the docker container.
  4. A single command to run when the container starts.

Example

# Dockerfile
FROM python

COPY . /app

WORKDIR /app

RUN pip install -r /app/requirements.txt

EXPOSE 5000

CMD waitress-serve --listen=*:5000 wsgi:application

The above is a simple example to run a python webserver:

  1. Uses the official python image as a base
  2. Copies everything in the current folder into /app in our container
  3. Sets /app as the folder to run commands in
  4. Installs dependencies via pip
  5. Says that container runs something on port 5000
  6. Tells docker to run a waitress server when the container starts

Commands

Each line of a Dockerfile is a command, there are many different commands but these are some of the most common.

FROM
the docker image to base the new image on
WORKDIR
the directory to run any commands in
COPY
copy files or folders into the container
RUN
a shell command to help setup the container
CMD
the shell command to run when the container is started
EXPOSE
any ports the container wants to expose