4. Dockerfile#

A Dockerfile is used to write instructions to build an image.

A Dockerfile must start with a FROM instruction

4.1. Environment Variables#

Environment variables are declared with the ENV statement. Environment variables are initialised and referred to using the following syntax.

ENV x=1
RUN echo $x
RUN echo ${x}

Curly braces are optional when referring to an environment variable.

4.2. Creating Users#

Usually, it is recommended to execute commands in a docker container as the root user. Creating and managing users can differ between operating systems, so for the time being, we limit ourselves to Linux.

To create a new user in Linux, we use the useradd command.

FROM python:3.10-slim-buster

# Create user called dev_user
RUN useradd --create-home --shell /bin/bash dev_user

# Switch to dev_user
USER dev_user
WORKDIR /home/dev_user