containers / containers/podman-compose

Same USER as the host that can read/write to ~/.

Open
#395 21 comments 1 reaction 0 assignees View on GitHub
Support
Dominant language
Python
Stars
6.2k
Forks
622
PR merge metrics
No merged PRs in 30d

Description

So I am trying to build a container that has [chezmoi](https://www.chezmoi.io) and [Vault](https://www.vaultproject.io) in it. I need my `$USER` to be the same and `$HOSTNAME` in the container and have read/write access to my home directory. Chezmoi reads [templates](https://www.chezmoi.io/docs/reference/#template-variables) that specify both the hostname and user for template logic.

I started with: [Running Docker Containers as Current Host User](https://jtreminio.com/blog/running-docker-containers-as-current-host-user/), but found that if I had the UID 1000:1000 in the container this wasn't mapped in my container. Perhaps I need to set this to a different UID? My subuid/subgid is:

#### /etc/subuid:
```
daniel:100000:65536
```
#### /etc/subgid:
```
daniel:100000:65536
```

I then found [User IDs and (rootless) containers with Podman](https://blog.christophersmart.com/2021/01/26/user-ids-and-rootless-containers-with-podman/), but it seems that `--user` has to be set with `podman run`. [Should you use the --user flag in rootless containers?](https://www.redhat.com/sysadmin/user-flag-rootless-containers), mentioned `--userns=keep-id` but I'd like to keep it in my compose file if possible. Apparently that can't be specified https://github.com/containers/podman-compose/issues/166, one solution there mentions specifying this in .bashrc/.zshrc, but I'd really rather use a local `.env` file in my docker-compose directory.

I'm not really sure if those features are what I need to solve this, so any advice would be welcome. This is what I have so far:

#### .env:
```bash
COMPOSE_PROJECT_NAME=chezmoi
COMPOSE_HTTP_TIMEOUT=300
COMPOSE_FILE=chezmoi-compose.yml
USER_ID=0
GROUP_ID=0
PODMAN_USERNS=keep-id
```

#### chezmoi-compose.yml:
```yaml
services:
chezmoi:
container_name: chezmoi
hostname: ${HOSTNAME}
build:
context: .
dockerfile: ./chezmoi/Dockerfile
security_opt:
label: disable
args:
USER_ID: ${USER_ID:-0}
GROUP_ID: ${GROUP_ID:-0}
USER: ${USER:-0}
volumes:
- ${HOME}:/home/${USER}
```

#### chezmoi/Dockerfile:
```Dockerfile
FROM fedora

ARG USER_ID
ARG GROUP_ID
ARG USER

RUN latestChezmoi=$(curl -s https://api.github.com/repos/twpayne/chezmoi/releases/latest | grep -P '^(?=.*browser_download_url.*rpm)(?=.*x86_64)' | cut -d '"' -f 4) && dnf -y install $latestChezmoi

RUN dnf install -y dnf-plugins-core; dnf config-manager --add-repo https://rpm.releases.hashicorp.com/fedora/hashicorp.repo; dnf -y install vault

RUN if [ ${USER_ID:-0} -ne 0 ] && [ ${GROUP_ID:-0} -ne 0 ]; then \
groupadd -g ${GROUP_ID} ${USER} &&\
useradd -l -u ${USER_ID} -g ${USER} ${USER} &&\
install -Z -d -m 0755 -o ${USER} -g ${USER} /home/${USER} ;fi

USER $USER
WORKDIR $USER
ENTRYPOINT ["tail", "-f", "/dev/null"]
```

I compiled this with (Incidentally I ran into https://github.com/containers/podman-compose/issues/371) :smile:

```
podman-compose -f chezmoi-compose.yml up
```

I used this to get a shell:
```
podman container exec -it chezmoi bash
```

**Output:**
```
$ podman-compose version
['podman', '--version', '']
using podman version: 3.4.4
podman-composer version 0.1.8
podman --version
podman version 3.4.4
exit code: 0
```

**Environment:** Fedora Silverblue 35

```
podman version
Version: 3.4.4
API Version: 3.4.4
Go Version: go1.16.8
Built: Thu Dec 9 08:15:07 2021
OS/Arch: linux/amd64
```

**Additional context**

Also something else would be nice if there was a `support` label. I think this would be useful as some people have asked questions and they weren't really bugs or new features.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.