Dockerfile

Introduction

A Dockerfile is used to build images.

If you have used make or batch files, you may notice some analogies

FROM

The FROM instruction is needed to use other images. If you need Python you can take a ready-made image

FROM python:3.9

LABEL

The LABEL instruction is needed to specify the author

LABEL author="andreyolegovich"

RUN

The RUN instruction is needed to run commands. For example, commands Bash

RUN python -m pip install --upgrade pip # or RUN npm install

COPY

When the code is ready for production, the COPY instruction copies it into the container.

COPY src/ $YOUR_APP

ENTRYPOINT

The instruction ENTRYPOINT points to the entry point in the container.

It is better to write it as a JSON array.

ENTRYPOINT ["node", "server.js"]

WORKDIR

The instruction WORKDIR points to the working directory. This is where the given container will run.

example

WORKDIR /var/app

EXPOSE

The instruction EXPOSE opens a specific port

For example, to open port 5000 for Flask

EXPOSE 5000

ENV

With ENV you can set environment variables.

For example, you can set the working directory with ENV and then reuse it

ENV APP_DIR=/home/app/python RUN mkdir -p $APP_DIR WORKDIR $APP_DIR COPY src/ $APP_DIR

VOLUME

With VOLUME you can specify in which directory to create an external volume.

VOLUME ["/var/app"]

or several volumes

VOLUME ["/var/app", "/var/log]

Example

FROM python:3.9 ENV MICRO_SERVICE=/home/app/webapp # set work directory RUN mkdir -p $MICRO_SERVICE # where your code lives WORKDIR $MICRO_SERVICE # set environment variables ENV PYTHONDONTWRITEBYTECODE 1 ENV PYTHONUNBUFFERED 1 # install dependencies RUN python -m pip install --upgrade pip # copy project COPY src/ $MICRO_SERVICE RUN python -m pip install -r ./requirements.txt EXPOSE 8501 CMD streamlit run app.py

Article author: Andrei Olegovich