Skip to content

Commit ca5de8f

Browse files
committed
feat: more effectively step-down from root in entrypoint
1 parent b249ded commit ca5de8f

File tree

3 files changed

+38
-13
lines changed

3 files changed

+38
-13
lines changed

Dockerfile

+13-13
Original file line numberDiff line numberDiff line change
@@ -9,34 +9,34 @@ ENV PYTHONUNBUFFERED=1 \
99
# Create app directory
1010
RUN set -eux; \
1111
mkdir /app
12-
WORKDIR /app
1312

14-
# Create user for ssh
13+
# Create ansible user with explicit uid
1514
RUN <<EOF
1615
set -eux
17-
useradd -m ansible
16+
groupadd -r ansible --gid=1000
17+
useradd -m -u 1000 -g 1000 ansible
1818
mkdir -p /home/ansible/.ssh
19-
chown -R ansible:ansible /home/ansible/.ssh
19+
chown -R ansible:ansible /home/ansible
2020
EOF
2121

22-
# Install runtime dependencies
22+
# Install system runtime dependencies
2323
RUN <<EOF
2424
set -eux
2525
apt-get update
26-
apt-get install -y --no-install-recommends libssh-dev
26+
apt-get install -y --no-install-recommends libssh-dev gosu
2727
rm -rf /var/lib/apt/lists/*
2828
EOF
2929

30-
# Install python dependencies
30+
# Install python runtime dependencies
3131
COPY overlay/ /
3232
RUN <<EOF
3333
set -eux
3434
pip install -r /opt/buildpack/requirements.txt
35+
su -c "ansible-galaxy collection install -r /opt/buildpack/requirements.yaml" ansible
3536
EOF
3637

37-
# Install ansible dependencies
38-
USER ansible
39-
RUN <<EOF
40-
set -eux
41-
ansible-galaxy collection install -r /opt/buildpack/requirements.yaml
42-
EOF
38+
VOLUME /app
39+
VOLUME /home/ansible/.ssh
40+
WORKDIR /app
41+
ENTRYPOINT ["/docker-entrypoint.sh"]
42+
CMD ["/bin/bash"]

README.md

+9
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ This project builds a docker image with all of the dependencies required to run
44

55
## Image Details
66

7+
### Environment Variables
8+
9+
| Environment Variable | Description |
10+
| -------------------- | -------------------------------------- |
11+
| `PUID` | User ID of the primary ansible user |
12+
| `PGID` | Group ID for the priamry ansible group |
13+
714
### Users
815

916
| User | Description |
@@ -26,6 +33,8 @@ docker run \
2633
--rm -it \
2734
--pull always \
2835
--network host \
36+
-e PUID=${id -u} \
37+
-e PGID=${id -g} \
2938
--mount type=bind,source=".",target=/app \
3039
--mount type=bind,source="${HOME}/.ssh",target=/home/ansible/.ssh,readonly \
3140
ghcr.io/gamersoutreach/ansible-runner:latest \

overlay/docker-entrypoint.sh

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/usr/bin/env sh
2+
set -e
3+
4+
PUID="${PUID:-1000}"
5+
PGID="${PGID:-1000}"
6+
7+
# Set UID/GID of ansible user
8+
sed -i "s/^ansible\:x\:1000\:1000/ansible\:x\:$PUID\:$PGID/" /etc/passwd
9+
sed -i "s/^ansible\:x\:1000/ansible\:x\:$PGID/" /etc/group
10+
11+
# Set permissions on home folder, excluding .ssh mount
12+
chown $PUID:$PGID /home/ansible
13+
find /home/ansible -mindepth 1 -maxdepth 1 -not -name ".ssh" -exec chown -R $PUID:$PGID {} \;
14+
15+
# Step-down from root
16+
exec gosu ansible "${@}"

0 commit comments

Comments
 (0)