Variabelen in Dockerfiles

Introductie tot Docker

Tim Sangster

Software Engineer @ DataCamp

Variabelen met de instructie ARG

Variabelen maken in een Dockerfile

ARG <var_name>=<var_value>

Bijv. ARG path=/home/repl

Gebruiken in de Dockerfile

$path

Bijv. COPY /local/path $path

Introductie tot Docker

Use-cases voor de instructie ARG

De Python-versie instellen

FROM ubuntu
ARG python_version=3.9.7-1+bionic1
RUN apt-get install python3=$python_version
RUN apt-get install python3-dev=$python_version

Een map configureren

FROM ubuntu
ARG project_folder=/projects/pipeline_v3
COPY /local/project/files $project_folder
COPY /local/project/test_files $project_folder/tests
Introductie tot Docker

ARG-variabelen instellen bij build-time

FROM ubuntu
ARG project_folder=/projects/pipeline_v3
COPY /local/project/files $project_folder
COPY /local/project/test_files $project_folder/tests

Een variabele zetten in de build-opdracht

docker build --build-arg project_folder=/repl/pipeline .

ARG wordt overschreven en bestanden komen in:

COPY /local/project/files /repl/pipeline
COPY /local/project/test_files /repl/pipeline/tests
Introductie tot Docker

Variabelen met ENV

Variabelen maken in een Dockerfile

ENV <var_name>=<var_value>

Bijv. ENV DB_USER=pipeline_user

Gebruiken in de Dockerfile of runtime

$DB_USER

Bijv. CMD psql -U $DB_USER

Introductie tot Docker

Use-cases voor de instructie ENV

Een directory instellen voor runtime

ENV DATA_DIR=/usr/local/var/postgres
ENV MODE production

Een variabele instellen of vervangen bij runtime

docker run --env <key>=<value> <image-name>

docker run --env POSTGRES_USER=test_db --env POSTGRES_PASSWORD=test_db postgres

1 https://hub.docker.com/_/postgres
Introductie tot Docker

Geheimen in variabelen zijn niet veilig

docker history <image-name>

ARG DB_PASSWORD=example_password

Wordt getoond in docker history:

IMAGE          CREATED        CREATED BY                          SIZE      ...
cd338027297f   2 months ago   ARG DB_PASSWORD=example_password    0B        ...
Introductie tot Docker

Samenvatting

Gebruik Dockerfile-instructie
Variabele alleen tijdens build ARG <name>=<value>
Variabele maken ENV <name>=<value>

Gebruik Shell-opdracht
ARG overschrijven in docker build docker build --build-arg <name>=<value>
ENV overschrijven in docker run docker run --env <name>=<value> <image-name>
Instructies zien voor een image docker history <image-name>
Introductie tot Docker

Laten we oefenen!

Introductie tot Docker

Preparing Video For Download...