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:
- Which docker image to use as a base for the new image.
- Any files that need to be copied into the container.
- Commands to setup the docker container.
- 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:
- Uses the official python image as a base
- Copies everything in the current folder into
/app
in our container - Sets
/app
as the folder to run commands in - Installs dependencies via pip
- Says that container runs something on port
5000
- 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