0

I have an interesting problem with node, docker-compose and local development using bind mounts.

I have a bind mount volume, called www-public, owned by my host node user (I created a node user and chowned the dir to node:node). The container (official node) is set up so it also run as node user. At some point in my node app, i need to write to this folder and i get the permission error. This seems strange at first, but it makes sense, because i figured then, that my host nodeuser has an uid of 1001 while my container node user has an id of 1000. If i just add this line to my Dockerfile:

RUN usermod -u 1001 node && groupmod -g 1001 node

The permission problem goes away.

How do i fix this in a way, that I don't hard code the container node user id to match the node user id on the host like i did with the upper RUN command.

2 Answers 2

1

In your Dockerfile:

ARG UID=1000
ARG GID=1000

RUN usermod -u $UID node && groupmod -g $GID node

Then using docker build:

docker build --build-arg UID=$(id -u) --build-arg GID=$(id -g) .

The ARG lines provide defaults. If you leave off the --build-arg flags, they will be used. If you do use the --build-arg flags values those will be used instead.

1
  • Thx, both answers are useful. I will add args with defaults to Dockerfile, then override them in compose file for local dev. Cheers!
    – U2ros
    Commented Sep 25, 2020 at 7:39
0

The Compose syntax allows you to specify user: <uid>:<gid> in the service's configuration block.

For example:

version: "3"
services:
  node-service:
    image: node
    user: 1001:1001

This will run the container as UID 1001 and GID 1001.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.