Skip to content

Commit d4fc6f8

Browse files
committed
Added USERNAME environment option
1 parent 4cf62c0 commit d4fc6f8

File tree

4 files changed

+72
-21
lines changed

4 files changed

+72
-21
lines changed

Dockerfile.base

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
FROM ubuntu:24.04
44

5+
ARG USERNAME=user
56
ARG UID=1000
67
ARG GID=1000
78
ARG PYTHON_VENV_PATH=/opt/python/venv
@@ -134,10 +135,10 @@ RUN apt-get clean -y && \
134135
rm -rf /var/lib/apt/lists/*
135136

136137
# Create 'user' account
137-
RUN groupadd -g $GID -o user
138+
RUN groupadd -g $GID -o $USERNAME
138139

139-
RUN useradd -u $UID -m -g user -G plugdev user \
140-
&& echo 'user ALL = NOPASSWD: ALL' > /etc/sudoers.d/user \
141-
&& chmod 0440 /etc/sudoers.d/user
140+
RUN useradd -u $UID -m -g $USERNAME -G plugdev $USERNAME \
141+
&& echo $USERNAME ' ALL = NOPASSWD: ALL' > /etc/sudoers.d/$USERNAME \
142+
&& chmod 0440 /etc/sudoers.d/$USERNAME
142143

143144
USER root

Dockerfile.ci

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
ARG BASE_IMAGE
44
FROM ${BASE_IMAGE:-zephyrprojectrtos/ci-base:latest}
55

6+
ARG USERNAME=user
7+
68
ARG ZSDK_VERSION=0.17.1-rc4
79
ENV ZSDK_VERSION=$ZSDK_VERSION
810
ARG KITWARE_NINJA_VERSION=1.11.1.g95dee.kitware.jobserver-1
@@ -183,11 +185,11 @@ RUN apt-get clean -y && \
183185
# Run the Zephyr SDK setup script as 'user' in order to ensure that the
184186
# `Zephyr-sdk` CMake package is located in the package registry under the
185187
# user's home directory.
186-
USER user
188+
USER $USERNAME
187189

188190
RUN sudo -E -- bash -c ' \
189191
/opt/toolchains/zephyr-sdk-${ZSDK_VERSION}/setup.sh -c && \
190-
chown -R user:user /home/user/.cmake \
192+
chown -R $USERNAME:$USERNAME /home/$USERNAME/.cmake \
191193
'
192194

193195
USER root

Dockerfile.devel

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
ARG BASE_IMAGE
44
FROM ${BASE_IMAGE:-zephyrprojectrtos/ci:latest}
55

6+
ARG USERNAME=user
7+
68
# Install packages
79
RUN apt-get -y update && \
810
apt-get -y upgrade && \
@@ -13,24 +15,33 @@ RUN apt-get -y update && \
1315
x11vnc \
1416
xvfb \
1517
xterm \
16-
xz-utils
18+
xz-utils \
19+
usbutils \
20+
vim
1721

1822
# Clean up stale packages
1923
RUN apt-get clean -y && \
2024
apt-get autoremove --purge -y && \
2125
rm -rf /var/lib/apt/lists/*
2226

23-
# Add entrypoint script
24-
ADD ./entrypoint.sh /home/user/entrypoint.sh
25-
RUN dos2unix /home/user/entrypoint.sh
26-
ENTRYPOINT ["/home/user/entrypoint.sh"]
27+
# Add entrypoint script (it is in home because
28+
# I can't figure out how to get the $USERNAME
29+
# into the string.)
30+
ADD ./entrypoint.sh /home/entrypoint.sh
31+
RUN dos2unix /home/entrypoint.sh
32+
ENTRYPOINT ["/home/entrypoint.sh"]
2733

2834
# Add bash completion script
29-
ADD ./bash_completion /home/user/.bash_completion
30-
RUN mkdir -p /home/user/.bash_completion.d
35+
ADD ./bash_completion /home/$USERNAME/.bash_completion
36+
RUN mkdir -p /home/$USERNAME/.bash_completion.d
37+
38+
39+
# Adjust $USERNAME home directory permissions
40+
USER root
41+
RUN chown -R $USERNAME:$USERNAME /home/$USERNAME
3142

3243
# Switch to 'user' context
33-
USER user
44+
USER $USERNAME
3445

3546
# Configure environment variables
3647
ENV DISPLAY=:0
@@ -47,12 +58,5 @@ RUN mkdir ~/.vnc && x11vnc -storepasswd ${VNCPASSWD} ~/.vnc/passwd
4758
# Expose port 5900 for VNC
4859
EXPOSE 5900
4960

50-
# Adjust 'user' home directory permissions
51-
USER root
52-
RUN chown -R user:user /home/user
53-
54-
# Make 'user' default on launch
55-
USER user
56-
5761
# Launch bash shell by default
5862
CMD ["/bin/bash"]

README.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,50 @@ It can be used for building Zephyr samples and tests by mounting the Zephyr work
7777
docker run -ti -v <path to zephyr workspace>:/workdir zephyr-build:v<tag>
7878
```
7979

80+
#### Using SSH Agent with Docker Image
81+
82+
The docker images can be built to use the SSH agent on the host to provide authorization
83+
to assets like restricted git repos. To do this there are a few requirements. One of which
84+
is that the user name of the processes inside the docker container must match the real user
85+
name on the host. The USERNAME build argument can be passed into the build process to override
86+
the default user name. Note that all three images need to be built locally with this USERNAME
87+
argument set correctly.
88+
89+
```
90+
docker build -f Dockerfile.base \
91+
--build-arg UID=$(id -u) \
92+
--build-arg GID=$(id -g) \
93+
--build-arg USERNAME=$(id -u -n) \
94+
-t ci-base:<tag> .
95+
```
96+
```
97+
docker build -f Dockerfile.ci \
98+
--build-arg UID=$(id -u) \
99+
--build-arg GID=$(id -g) \
100+
--build-arg USERNAME=$(id -u -n) \
101+
--build-arg BASE_IMAGE=ci-base:v4.0-branch \
102+
-t ci:<tag> .
103+
```
104+
```
105+
docker build -f Dockerfile.devel \
106+
--build-arg UID=$(id -u) \
107+
--build-arg GID=$(id -g) \
108+
--build-arg USERNAME=$(id -u -n) \
109+
--build-arg BASE_IMAGE=ci:v4.0-branch \
110+
-t devel:<tag> .
111+
```
112+
113+
Then when running the ci or devel image there are additional command line arguments to
114+
connect the host ssh-agent ports to the ssh-agent ports inside the container.
115+
116+
```
117+
docker run -ti \
118+
-v $HOME/Work/zephyrproject:/workdir \
119+
--mount type=bind,src=$SSH_AUTH_SOCK,target=/run/host-services/ssh-auth.sock \
120+
--env SSH_AUTH_SOCK="/run/host-services/ssh-auth.sock" \
121+
devel:<tag>
122+
```
123+
80124
### Usage
81125

82126
#### Building a sample application

0 commit comments

Comments
 (0)