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

Avoid hardcoded UID and GID in docker build #67

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

ivan-aksamentov
Copy link
Member

@ivan-aksamentov ivan-aksamentov commented Aug 3, 2021

Description of proposed changes

The Dockerfile uses UID and GID args to create a user inside the container. Files are then written during build on behalf of this user, to avoid permission problems.

Mistakenly, these variables were both hardcoded to 1000, which is fine for a single-user Linux host, bot not for macOS.

This PR passes UID and GID from the host system as build args, so that the files are written on behalf of the the current user.

Related issue(s)

Amends #63

Testing

Works on my machine (tm)

01

The [Dockerfile](https://github.com/nextstrain/docs.nextstrain.org/blob/1da467787db9a8f9b43724f3c41264fa30ff4db6/Dockerfile) uses `UID` and `GID` args to create a user inside the container. Files are then written during build on behalf of this user, to avoid permission problems.

Mistakenly, these variables were both hardcoded to `1000`, which is fine for a single-user Linux host, bot not for macOS. 

This PR passes `UID` and `GID` from the host system as build args, so that the files are written on behalf of the the current user.
Makefile Outdated
@@ -22,7 +22,11 @@ help:
.ONESHELL:
docker-html:
set -euox
docker build -t nextstrain-docs-builder --network=host .
docker build -t nextclade-docs-builder \
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo here in the image name

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed!

Copy link
Contributor

@eharkins eharkins left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I get this error when I run make docker-html:

docker build -t nextstrain-docs-builder \
        --network=host \
        --build-arg UID=501 \
        --build-arg GID=20 \
        .
Sending build context to Docker daemon    142MB
Step 1/16 : FROM continuumio/miniconda3:4.9.2
 ---> 52daacd3dd5d
Step 2/16 : ARG DEBIAN_FRONTEND=noninteractive
 ---> Using cache
 ---> 30387e9a57da
Step 3/16 : ARG USER=user
 ---> Using cache
 ---> 45e964da4f9b
Step 4/16 : ARG GROUP=user
 ---> Using cache
 ---> e404ca0a624f
Step 5/16 : ARG UID
 ---> Running in 81e937f813c5
Removing intermediate container 81e937f813c5
 ---> e3fc6241cef1
Step 6/16 : ARG GID
 ---> Running in 5e75f0008202
Removing intermediate container 5e75f0008202
 ---> 0abdb7769eea
Step 7/16 : ENV TERM="xterm-256color"
 ---> Running in e366dce5f017
Removing intermediate container e366dce5f017
 ---> 843c7d6522e0
Step 8/16 : ENV HOME="/home/user"
 ---> Running in 3bfc15c2b77b
Removing intermediate container 3bfc15c2b77b
 ---> f4d853ad795c
Step 9/16 : RUN set -x   && mkdir -p ${HOME}/data   && addgroup --system --gid ${GID} ${GROUP}   && useradd --system --create-home --home-dir ${HOME}   --shell /bin/bash   --gid ${GROUP}   --groups sudo   --uid ${UID}   ${USER}   && touch ${HOME}.hushlogin
 ---> Running in 157c76e264da
+ mkdir -p /home/user/data
+ addgroup --system --gid 20 user
addgroup: The GID `20' is already in use.
The command '/bin/sh -c set -x   && mkdir -p ${HOME}/data   && addgroup --system --gid ${GID} ${GROUP}   && useradd --system --create-home --home-dir ${HOME}   --shell /bin/bash   --gid ${GROUP}   --groups sudo   --uid ${UID}   ${USER}   && touch ${HOME}.hushlogin' returned a non-zero code: 1
make: *** [docker-html] Error 1

The error was
```
addgroup: The GID `20' is already in use
```

Turns out nowadays Docker for Mac and Docker for Windows translate UID and GID automagically. And apparently it makes it so that the GID (and perhaps UID?) already exist in the container. This is not the case on Linux.

All Dockerfiles relying on consistent UID and GID across platforms are now broken. And it's the only good way (not mentioning half-baked rootles mode that noone ever bothers to confugure) to get docker containers to not run as root and to not create bunch of root-owned files in volumes. What a wonderful decision!

So maybe if we try to only create the group and the user if not already exist,  this will avoid errors? I cannot test it because I don't have a mac.

See: https://stackoverflow.com/questions/43097341/docker-on-macosx-does-not-translate-file-ownership-correctly-in-volumes
@ivan-aksamentov
Copy link
Member Author

ivan-aksamentov commented Aug 4, 2021

@eharkins

I get this error when I run make docker-html:

Can you retry with the new version? Please delete the build directory before running and check it's owner afterwards.

This version works on Linux okay, but Mac and Windows have some quirks (described in the commit message). And I don't have a Mac handy to test it.

P.S. Unless you are going to actually use the Dockerfile, I don't think we should dig too deeply into it though.

@eharkins
Copy link
Contributor

eharkins commented Aug 4, 2021

Thanks @ivan-aksamentov it works for me now!

@tsibley
Copy link
Member

tsibley commented Jan 30, 2024

Is any one using the image produced by the Dockerfile in this repo? My sense is not. If that's the case, then we should close this PR without merging and opt to delete the Dockerfile instead.

Otherwise, we should finalize this PR one way or the other.

@jameshadfield
Copy link
Member

Is any one using the image produced by the Dockerfile in this repo?

Not me. I vote to delete.

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.

None yet

4 participants