Skip to content

Commit eed1a23

Browse files
authored
🐛 fix(permissions): Change user permissions to map current user (#19)
1 parent 2fe6c30 commit eed1a23

File tree

7 files changed

+97
-12
lines changed

7 files changed

+97
-12
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
log/mosquitto.log
22
config/mosquitto.conf
3-
config/password.txt
3+
config/password.txt
4+
docker-compose.override.yml

Dockerfile

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
FROM eclipse-mosquitto:2
2+
3+
ARG UID
4+
ARG GID
5+
ARG USER
6+
7+
RUN if [ ${USER} != "root" ]; then \
8+
apk add shadow && \
9+
groupadd -f -g ${GID} ${USER} && \
10+
useradd -m -g ${USER} -u ${UID} ${USER}; \
11+
fi
12+
13+
RUN chown -R ${USER}:${USER} /mosquitto
14+
15+
USER ${USER}

Makefile

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Executables: local only
2+
DOCKER_COMP = docker compose
3+
DOCKER_COMP_EXEC = $(DOCKER_COMP) exec mosquitto
4+
5+
# Misc
6+
.DEFAULT_GOAL = help
7+
8+
help: ## Outputs this help screen
9+
@grep -E '(^[a-zA-Z0-9_-]+:.*?##.*$$)|(^##)' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}{printf "\033[32m%-30s\033[0m %s\n", $$1, $$2}' | sed -e 's/\[32m##/[33m/'
10+
11+
alias: ## Displays all alias of the package manager
12+
$(DOCKER_COMP_EXEC) npm run
13+
14+
## —— Docker 🐳 ————————————————————————————————————————————————————————————————
15+
build: ## Builds the Node image
16+
$(DOCKER_COMP) build
17+
18+
up: ## Starts the docker hub
19+
$(DOCKER_COMP) up -d
20+
21+
down: ## Stops the docker hub
22+
$(DOCKER_COMP) down --remove-orphans
23+
24+
restart: ## Restarts the docker hub
25+
$(DOCKER_COMP) restart
26+
27+
sh: ## Connects to the application container
28+
$(DOCKER_COMP_EXEC) sh
29+
30+
logs: ## Displays the logs of the application container
31+
$(DOCKER_COMP) logs -f mosquitto
32+
33+
## —— Project 🐝 ———————————————————————————————————————————————————————————————
34+
setup-project: ## Initializes configuration
35+
bin/setup
36+
make build

README.md

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,20 @@ This is a simple [Mosquitto](https://mosquitto.org) broker to quickly initialize
1111

1212
## How to use
1313

14-
To start the container, just :
14+
At first startup, you need to setup the project:
1515

1616
```bash
17-
UID=$UID GID=$GID docker compose up -d
17+
make setup-project
1818
```
1919

20+
Then, and for other startup, you just have to run:
21+
22+
```bash
23+
make up
24+
```
25+
26+
> Find other `make` helper command in the [Makefile](./Makefile)
27+
2028
The Mosquitto broker is now available on localhost. You can test it easily (require Mosquitto client):
2129

2230
| In one shell:
@@ -48,13 +56,13 @@ In the config file, set the value `allow_anonymous` to `false`, then uncomment t
4856
### Change user password / create a new user
4957

5058
```bash
51-
docker-compose exec mosquitto mosquitto_passwd -b /mosquitto/config/password.txt user password
52-
docker-compose restart
59+
docker compose exec mosquitto mosquitto_passwd -b /mosquitto/config/password.txt user password
60+
make restart
5361
```
5462

5563
### Delete user
5664

5765
```bash
58-
docker-compose exec mosquitto mosquitto_passwd -D /mosquitto/config/password.txt user
59-
docker-compose restart
66+
docker compose exec mosquitto mosquitto_passwd -D /mosquitto/config/password.txt user
67+
make restart
6068
```

bin/setup

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/bin/bash
2+
3+
projectRootPath=$( cd "$(dirname "${BASH_SOURCE[0]}")" ; pwd -P )/..
4+
5+
if [ ! -f ${projectRootPath}/docker-compose.override.yml ]; then
6+
cp ${projectRootPath}/docker-compose.override.yml.dist ${projectRootPath}/docker-compose.override.yml
7+
8+
sed -i'' -e "s/\[GID\]/$(id -g)/g" ${projectRootPath}/docker-compose.override.yml;
9+
sed -i'' -e "s/\[UID\]/$(id -u)/g" ${projectRootPath}/docker-compose.override.yml;
10+
11+
username=$(id -un)
12+
13+
if [ "$username" != "root" ]; then
14+
username="unicorn"
15+
fi
16+
17+
sed -i'' -e "s/\[USER\]/${username}/g" ${projectRootPath}/docker-compose.override.yml;
18+
19+
rm -f docker-compose.override.yml-e # Hack for MAC OS
20+
fi

docker-compose.override.yml.dist

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
services:
2+
mosquitto:
3+
build:
4+
args:
5+
GID: [GID]
6+
UID: [UID]
7+
USER: [USER]
8+
user: [UID]:[GID]

docker-compose.yaml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
---
2-
version: '3.7'
3-
41
services:
52
mosquitto:
6-
image: eclipse-mosquitto:2
7-
user: mosquitto
3+
build:
4+
context: .
85
volumes:
96
- type: bind
107
source: ./config/

0 commit comments

Comments
 (0)