简体   繁体   中英

File permissions problems in Dockerfile for a ruby application

Hello I'm trying to create a ruby app using the user id and group id in the container as in the host, ie. 1000.

I run into permissions problems but I can't figure out why.

Here is the error that I get:

There was an error while trying to write to `/home/appuser/myapp/Gemfile.lock`.
It is likely that you need to grant write permissions for that path.
The command '/bin/sh -c bundle install' returned a non-zero code: 23

Here is my Dockerfile:

# Dockerfile
FROM ruby:2.5

RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs

RUN groupadd -r -g 1000 appuser
RUN useradd -r -m -u 1000 -g appuser appuser
USER appuser

RUN mkdir /home/appuser/myapp

WORKDIR /home/appuser/myapp

COPY Gemfile Gemfile.lock ./

RUN bundle install

COPY . ./

If you want a pure docker solution, try this:

FROM ruby:2.5

RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs

# Reduce layers by grouping related commands into single RUN steps
RUN groupadd -r -g 1000 appuser && \
useradd -r -m -u 1000 -g appuser appuser

# Setting workdir will also create the dir if it doesn't exist, so no need to mkdir
WORKDIR /home/appuser/myapp

# Copy everything over in one go
COPY . ./

# This line should fix your issue 
# (give the user ownership of their home dir and make Gemfile.lock writable)
# Must still be root for this to work
RUN chown -R appuser:appuser /home/appuser/ && \
chmod +w /home/appuser/myapp/Gemfile.lock

USER appuser

RUN bundle install

Might be a better idea to fix the permissions on your host system with something like this:

sudo chmod g+w Gemfile.lock

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM