Skip to content

Commit b42248f

Browse files
Copilotthawn
andcommitted
Add security hardening for Podman/Docker compatibility with non-root user
Co-authored-by: thawn <1308449+thawn@users.noreply.github.com>
1 parent f2dffc8 commit b42248f

File tree

2 files changed

+156
-11
lines changed

2 files changed

+156
-11
lines changed

build/docker/Dockerfile

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,31 @@ COPY . /app/
3535
# For production builds with proper certificate setup, these flags can be removed.
3636
RUN pip install --no-cache-dir --trusted-host pypi.org --trusted-host files.pythonhosted.org -e .
3737

38+
# Create a non-root user for running the application
39+
# Using UID/GID 1000 which is common for the first user on most Linux systems
40+
# This allows for better compatibility with Podman's user namespace mapping
41+
RUN groupadd -g 1000 ttmp32gme && \
42+
useradd -r -u 1000 -g ttmp32gme -m -d /home/ttmp32gme -s /bin/bash ttmp32gme
43+
3844
# Set up directories and copy config
45+
# Use proper permissions instead of 777 for security
3946
ENV APPDATA=/var/lib/
4047
RUN mkdir -p ${APPDATA}/ttmp32gme/library /mnt/tiptoi && \
4148
cp /app/src/ttmp32gme/config.sqlite ${APPDATA}/ttmp32gme/ && \
42-
chmod -R 777 ${APPDATA}/ttmp32gme/library
49+
chown -R ttmp32gme:ttmp32gme ${APPDATA}/ttmp32gme /mnt/tiptoi && \
50+
chmod -R 775 ${APPDATA}/ttmp32gme && \
51+
chmod 664 ${APPDATA}/ttmp32gme/config.sqlite
52+
53+
# Declare volumes for persistent data
54+
# This helps Podman/Docker users understand which directories should be mounted
55+
VOLUME ["${APPDATA}/ttmp32gme", "/mnt/tiptoi"]
4356

4457
# Expose port 8080
4558
EXPOSE 8080
4659

60+
# Switch to non-root user
61+
USER ttmp32gme
62+
4763
# Run the Python backend
4864
CMD ["python", "-m", "ttmp32gme.ttmp32gme", "--host=0.0.0.0", "--port=8080", "--database=/var/lib/ttmp32gme/config.sqlite", "--library=/var/lib/ttmp32gme/library"]
4965

build/docker/README.md

Lines changed: 139 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,144 @@
1-
A docker image for ttmp32gme.
1+
# ttmp32gme Docker/Podman Container
22

3-
Set the environment variable `HOST=0.0.0.0` to make ttmp32gme accessible to other computers in your network.
3+
A containerized version of ttmp32gme that converts MP3/audio files into TipToi GME files.
44

5+
## Features
6+
7+
- **Security**: Runs as non-root user (UID/GID 1000)
8+
- **Podman Compatible**: Works seamlessly with Podman's user namespace mapping
9+
- **Persistent Storage**: Separate volumes for configuration and TipToi device
10+
11+
## Quick Start
12+
13+
### Using Docker
14+
15+
```bash
16+
docker run -d \
17+
--rm \
18+
--name ttmp32gme \
19+
--publish 8080:8080 \
20+
--volume ttmp32gme-data:/var/lib/ttmp32gme \
21+
--volume /path/to/tiptoi:/mnt/tiptoi \
22+
thawn/ttmp32gme:latest
23+
```
24+
25+
### Using Podman
26+
27+
```bash
28+
podman run -d \
29+
--rm \
30+
--name ttmp32gme \
31+
--publish 8080:8080 \
32+
--volume ttmp32gme-data:/var/lib/ttmp32gme \
33+
--volume /path/to/tiptoi:/mnt/tiptoi:Z \
34+
thawn/ttmp32gme:latest
35+
```
36+
37+
**Note for Podman users**: The `:Z` flag on the TipToi volume enables proper SELinux labeling for shared access.
38+
39+
## Volume Mounts
40+
41+
- **`/var/lib/ttmp32gme`**: Application data (database, generated library files)
42+
- First run: Container initializes this directory with default config
43+
- Subsequent runs: Uses existing data for persistence
44+
45+
- **`/mnt/tiptoi`**: TipToi pen mount point
46+
- Mount your TipToi device here to directly write generated files
47+
48+
## User Permissions
49+
50+
The container runs as user `ttmp32gme` (UID 1000, GID 1000).
51+
52+
### For Podman Users
53+
54+
Podman automatically maps the container user to your host user, so permissions work seamlessly:
55+
56+
```bash
57+
# Your files will be owned by your user on the host
58+
podman run --rm \
59+
--volume ./my-data:/var/lib/ttmp32gme \
60+
--volume /media/tiptoi:/mnt/tiptoi:Z \
61+
--publish 8080:8080 \
62+
thawn/ttmp32gme:latest
563
```
64+
65+
### For Docker Users
66+
67+
If you encounter permission issues, you can use these options:
68+
69+
1. **Option 1**: Match your user ID (recommended)
70+
```bash
71+
docker run --user $(id -u):$(id -g) \
72+
--volume ./my-data:/var/lib/ttmp32gme \
73+
--volume /media/tiptoi:/mnt/tiptoi \
74+
--publish 8080:8080 \
75+
thawn/ttmp32gme:latest
76+
```
77+
78+
2. **Option 2**: Adjust host directory permissions
79+
```bash
80+
sudo chown -R 1000:1000 ./my-data
81+
```
82+
83+
## Accessing the Application
84+
85+
Once running, open your web browser to:
86+
- **Local access**: http://localhost:8080
87+
- **Network access**: http://your-server-ip:8080
88+
89+
## Environment Variables
90+
91+
| Variable | Default | Description |
92+
|----------|---------|-------------|
93+
| HOST | 0.0.0.0 | Server bind address |
94+
| PORT | 8080 | Server port |
95+
96+
Example with custom settings:
97+
98+
```bash
699
docker run -d \
7-
--rm \
8-
--publish 8080:8080 \
9-
--volume ${ttmp32gmeStorage}:/var/lib/ttmp32gme \
10-
--volume </path/to/tiptoi:/mnt/tiptoi \
11-
-env HOST=127.0.0.1 \
12-
-env PORT=8080 \
13-
--name ttmp32gme \
14-
thawn/ttmp32gme:latest
100+
--env HOST=127.0.0.1 \
101+
--env PORT=9000 \
102+
--publish 9000:9000 \
103+
--volume ttmp32gme-data:/var/lib/ttmp32gme \
104+
thawn/ttmp32gme:latest
105+
```
106+
107+
## Security Features
108+
109+
This container implements security best practices:
110+
111+
-**Non-root user**: Runs as dedicated `ttmp32gme` user
112+
-**Minimal permissions**: Files owned by user with 775/664 permissions
113+
-**Explicit volumes**: Declared volume mount points
114+
-**Health checks**: Automatic container health monitoring
115+
-**Podman ready**: Full compatibility with Podman's rootless mode
116+
117+
## Troubleshooting
118+
119+
### Permission denied errors
120+
121+
If you get "permission denied" errors when accessing volumes:
122+
123+
**For Podman**:
124+
```bash
125+
# Add :Z flag for SELinux systems
126+
podman run --volume /path/to/data:/var/lib/ttmp32gme:Z ...
127+
```
128+
129+
**For Docker**:
130+
```bash
131+
# Match your user ID
132+
docker run --user $(id -u):$(id -g) ...
133+
```
134+
135+
### Cannot write to TipToi device
136+
137+
Ensure your TipToi device is mounted with write permissions:
138+
```bash
139+
# Check current permissions
140+
ls -la /media/tiptoi
141+
142+
# If needed, adjust permissions
143+
sudo chmod 775 /media/tiptoi
15144
```

0 commit comments

Comments
 (0)