Issue
I tried to set a build argument in docker-compose.yml in order not to remove a file for a specific service, but it does not work: file is always removed.
I have got Dockerfile:
class="lang-yaml prettyprint-override">
FROM docker.io/python:3.11.4
ARG BASE_DIR=/opt/app
ARG REMOVE_POETRY_LOCK
WORKDIR ${BASE_DIR}
COPY ./pyproject.toml ./poetry.lock ./
RUN if [ $REMOVE_POETRY_LOCK==1 ]; then rm poetry.lock && echo condition_01 >condition_01.txt; else echo "poetry.lock is not removed" > condition_02.txt; fi
RUN echo "REMOVE_POETRY_LOCK: ${REMOVE_POETRY_LOCK}" >REMOVE_POETRY_LOCK_value.txt
I have got docker-compose.yml
version: '3'
services:
min_django_version:
build:
context: .
args:
- REMOVE_POETRY_LOCK=0
image: min_django_version:latest
command: tail -f /dev/null
I run:
docker compose up -d --build
docker compose exec -it min_django_version bash
ls
I see:
REMOVE_POETRY_LOCK_value.txt condition_01.txt pyproject.toml
I run:
cat REMOVE_POETRY_LOCK_value.txt
and I see:
REMOVE_POETRY_LOCK: 0
I expected that poetry.lock would not be removed because REMOVE_POETRY_LOCK: 0, but it is removed. Why? How should I change the condition in order not to remove the file?
Solution
From gnu.org official docs:
arg1 OP arg2
OP is one of ‘-eq’, ‘-ne’, ‘-lt’, ‘-le’, ‘-gt’, or ‘-ge’. These arithmetic binary operators return true if arg1 is equal to, not equal to, less than, less than or equal to, greater than, or greater than or equal to arg2, respectively. Arg1 and arg2 may be positive or negative integers. When used with the [[ command, Arg1 and Arg2 are evaluated as arithmetic expressions (see Shell Arithmetic).
Therefore you need to use if [ "$REMOVE_POETRY_LOCK" -eq 1 ]
.
Answered By - Kevin Kopf Answer Checked By - Candace Johnson (WPSolving Volunteer)