Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Dockerfile ARG scope #47

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

rominf
Copy link

@rominf rominf commented Jun 27, 2024

After applying changes from #46, Docker does not build still.

$ podman-compose  -f docker-compose.dev.yml  -f docker-compose.yml --podman-build-args="--security-opt label=disable" up --build --build-arg INSTALL_DEV_DEPS=0 --build-arg BUILD_ID=0
[4/4] STEP 15/32: RUN groupadd -g ${app_user_gid} user && useradd --no-log-init -m -d /home/user -g ${app_user_gid} -u ${app_user_uid} -r user
groupadd: invalid group ID 'user'
Error: building at STEP "RUN groupadd -g ${app_user_gid} user && useradd --no-log-init -m -d /home/user -g ${app_user_gid} -u ${app_user_uid} -r user": while running runtime: exit status 3

The reason is incorrect scope for multiple ARGs:

An ARG instruction goes out of scope at the end of the build stage where it was defined. To use an argument in multiple stages, each stage must include the ARG instruction.

(see: https://docs.docker.com/reference/dockerfile/#scope)

The change fixes the scope for all ARGs.

Note that I ran podman-compose because Fedora comes with podman, not docker, yet here the behavior of podman and docker are identical.

rominf added 2 commits June 27, 2024 19:11
Currently, docker build fails because `pip` is not found:
```
$ podman-compose  -f docker-compose.dev.yml  -f docker-compose.yml --podman-build-args="--security-opt label=disable" up --build --build-arg INSTALL_DEV_DEPS=0 --build-arg BUILD_ID=0
...
[3/4] STEP 13/13: RUN --mount=type=cache,target=${XDG_CACHE_HOME}/pip     if [ ! -z "${INSTALL_DEV_DEPS}" ] ; then ${VENV_PATH}/bin/pip install -r ${app_path}/requirements-dev.txt ; fi
/bin/sh: 1: /bin/pip: not found
Error: building at STEP "RUN --mount=type=cache,target=${XDG_CACHE_HOME}/pip if [ ! -z "${INSTALL_DEV_DEPS}" ] ; then ${VENV_PATH}/bin/pip install -r ${app_path}/requirements-dev.txt ; fi": while running runtime: exit status 127
```

The reason for this is that `VENV_PATH` is defined below it is used. The
change leverages that `VIRTUAL_ENV` is already defined, and the fact that:

> A stage inherits any environment variables that were set using ENV by
> its parent stage or any ancestor.

(see: https://docs.docker.com/reference/dockerfile/#env)

The line with `PATH` is deleted because it is unnecessary because of the
same fact.

Note that I ran `podman-compose` because Fedora comes with `podman`, not
`docker`, yet here the behavior of `podman` and `docker` are identical.
After applying changes from
kausaltech#46, Docker does not
build still.

```
$ podman-compose  -f docker-compose.dev.yml  -f docker-compose.yml --podman-build-args="--security-opt label=disable" up --build --build-arg INSTALL_DEV_DEPS=0 --build-arg BUILD_ID=0
[4/4] STEP 15/32: RUN groupadd -g ${app_user_gid} user && useradd --no-log-init -m -d /home/user -g ${app_user_gid} -u ${app_user_uid} -r user
groupadd: invalid group ID 'user'
Error: building at STEP "RUN groupadd -g ${app_user_gid} user && useradd --no-log-init -m -d /home/user -g ${app_user_gid} -u ${app_user_uid} -r user": while running runtime: exit status 3
```

The reason is incorrect scope for multiple `ARG`s:

> An ARG instruction goes out of scope at the end of the build stage
> where it was defined. To use an argument in multiple stages, each stage
> must include the ARG instruction.

(see: https://docs.docker.com/reference/dockerfile/#scope)

The change fixes the scope for all `ARG`s.

Note that I ran `podman-compose` because Fedora comes with `podman`, not
`docker`, yet here the behavior of `podman` and `docker` are identical.
rominf added a commit to rominf/kausal-paths that referenced this pull request Jun 27, 2024
On docker build after changes from:
kausaltech#46
and
kausaltech#47
I am getting:
```
$ podman-compose  -f docker-compose.dev.yml  -f docker-compose.yml --podman-build-args="--security-opt label=disable" up --build --build-arg INSTALL_DEV_DEPS=0 --build-arg BUILD_ID=0
...
Execution of msgfmt failed: msgfmt: error while opening "/venv/lib/python3.12/site-packages/django_filters/locale/fi/LC_MESSAGES/django.mo" for writing: Permission denied
...
CommandError: compilemessages generated one or more errors.
```

The change fixes permission errors by adding required dirs to `chown -R`
call.

After this build finally succeeds.

Note that I ran `podman-compose` because Fedora comes with `podman`, not
`docker`, yet here the behavior of `podman` and `docker` are identical.
rominf added a commit to rominf/kausal-paths that referenced this pull request Jun 27, 2024
On docker build after changes from:
kausaltech#46
and
kausaltech#47
there is a build error still:
```
$ podman-compose  -f docker-compose.dev.yml  -f docker-compose.yml --podman-build-args="--security-opt label=disable" up --build --build-arg INSTALL_DEV_DEPS=0 --build-arg BUILD_ID=0
...
Execution of msgfmt failed: msgfmt: error while opening "/venv/lib/python3.12/site-packages/django_filters/locale/fi/LC_MESSAGES/django.mo" for writing: Permission denied
...
CommandError: compilemessages generated one or more errors.
```

The change fixes permission errors by adding required dirs to `chown -R`
call.

After this build finally succeeds.

Note that I ran `podman-compose` because Fedora comes with `podman`, not
`docker`, yet here the behavior of `podman` and `docker` are identical.
@rominf rominf marked this pull request as ready for review June 27, 2024 16:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant