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

Fixes for issues 12, 13, 14 #15

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

Conversation

jamielinux
Copy link

@jamielinux jamielinux commented Mar 21, 2023

Run containers as the uid and gid of the user

There are two main reasons why we don't want to run as root inside the containers:

  1. Running as root is bad practice and a security risk.
  2. All files generated within the container (eg __pycache__ and artifacts etc) are owned by the root user on the host, resulting in file ownership issues in the user's project directory.

My approach in this commit is to create Docker images that execute commands as the user running hatch.

In the vast majority of cases the user will be the only user on the system, but on shared machines we need to handle this gracefully. My solution is to treat every UID:GID combination as a unique identity and create a Docker image with a name that looks like this:

python_3.11-alpine_1000_1000

In this case the UID and GID are both 1000. Every time this particular user executes hatch run for this environment, hatch-containers will select this container (if it already exists).

This fixes:

Use shtuil.move() instead of os.replace()

Fixes:

Discussion

Let me know what you think about my approach. Happy to rework the PR as needed.

What operating systems does hatch-containers currently support? If it's not just Unix then I've got some rethinking to do as I've paid no attention to Windows (and have no idea how Docker and file permissions work there).

There are two main reasons why we don't want to run as root inside the
containers:

1. Running as root is bad practice and a security risk.
2. All files generated within the container (eg `__pycache__` and
   artifacts etc) are owned by the root user on the host, resulting in
   file ownership issues in the user's project directory.

My approach in this commit is to create Docker images that execute
commands as the user running `hatch`.

In the vast majority of cases the user will be the only user on the
system, but on shared machines we need to handle this gracefully. My
solution is to treat every UID:GID combination as a unique identity and
create a Docker image with a name that looks like this:

  python_3.11-alpine_1000_1000

In this case the UID and GID are both `1000`. Every time this particular
user executes `hatch run` for this environment, `hatch-containers` will
select this container (if it already exists).

This fixes:

- ofek#12
- ofek#13
@jamielinux jamielinux changed the title Fixes for issues 12, 13 14 Fixes for issues 12, 13, 14 Mar 21, 2023
@jamielinux
Copy link
Author

Ah, sorry, tests were passing locally but I didn't run them again after re-preparing the commits for the PR.

@jamielinux
Copy link
Author

The last commit is to fix this lint error.

The other three commits before that fix some differences between my local environment (where tests were passing) and the GitHub runner environment (where tests were failing).

Everything passes now: https://github.com/jamielinux/hatch-containers/actions/runs/4488195949

In hindsight, I should have tested in the GitHub runner environment from the beginning!

@omercnet omercnet mentioned this pull request Oct 10, 2024
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