I am newbie with docker. I am trying to run my application in a docker container.
Dockerfile
FROM node:14.16.0-alpine
ARG SSH_KEY
RUN apk update && apk add openssh-client git \
&& addgroup myuser \
&& adduser myuser -G myuser -D /home/myuser -u 999 \
&& mkdir -p /home/myuser/.ssh \
&& chmod 0700 /home/myuser/.ssh \
&& ssh-keyscan github.com > /home/myuser/.ssh/known_hosts \
&& chown -R myuser:myuser /home/myuser
USER myuser:myuser
RUN mkdir -p /home/myuser/app/spa-proxy && chown -R myuser:myuser /home/myuser/app/spa-proxy
COPY package.json /home/myuser/app/spa-proxy
COPY package-lock.json /home/myuser/app/spa-proxy
WORKDIR /home/myuser/app/spa-proxy
ARG INSTALL_ENV="--production"
RUN echo "$SSH_KEY" > /home/myuser/.ssh/id_rsa && chmod 0600 /home/myuser/.ssh/id_rsa \
&& npm install ${INSTALL_ENV} \
&& rm /home/myuser/.ssh/id_rsa
COPY . /home/myuser/app/spa-proxy
ENV PATH $PATH:./node_modules/.bin/
RUN tsc
EXPOSE 8001
CMD ["node", "./.build/src/index.js"]
I created a group & user called myuser. Also tried to provide the ownership to myuser. When i tried to build the image, I am getting the following error.
Build Command:
sudo docker build --build-arg SSH_KEY="$(cat ~/.ssh/id_rsa)" -t testfolder .
Error:
error TS5033: Could not write file '/home/myuser/app/spa-proxy/.build/src/types/index.js': EACCES: permission denied, open '/home/myuser/app/spa-proxy/.build/src/types/index.js'.
The command /bin/sh -c tsc returned a non-zero code: 2
The error happens when docker runs the command RUN tsc. It is trying to write the build file into the working directory created, but not able to create due to permission issue.
Can somebody help me with this as I am eventually trying to learn the docker. Any help would be really appreciated.
Note: Please correct me if we can write a better Dockerfile or docker-compose.yml.
CodePudding user response:
COPYed files are always owned by root by default. Add --chown=
to your COPY statement to have the copied files be owned by 'myuser' like this
COPY --chown=myuser:myuser package.json /home/myuser/app/spa-proxy
COPY --chown=myuser:myuser package-lock.json /home/myuser/app/spa-proxy
and
COPY --chown=myuser:myuser . /home/myuser/app/spa-proxy