Permission denied when trying to run an executable in a Docker container - docker

I'm trying to make an Express server with access to sqlPackage for DACPAC-deployments.
This is the final stage in my Docker build:
FROM node:16-alpine
WORKDIR /usr/app
ARG SQLPACKAGE_URL=https://download.microsoft.com/download/f/0/9/f091c731-45be-48fa-ae84-bc28388e3ef8/sqlpackage-linux-x64-en-16.0.6161.0.zip
# Install sqlPackage
RUN apk add --no-cache wget unzip
RUN wget -progress=bar:force -q -O sqlpackage.zip $SQLPACKAGE_URL \
&& unzip -qq sqlpackage.zip -d /usr/app/sqlpackage \
&& chmod 777 /usr/app/sqlpackage \
&& chown -R node.node /usr/app/sqlpackage
COPY --from=ts-remover /usr/app ./
USER node
CMD ["main.js"]
ARG PORT=8080
EXPOSE $PORT
My node project cannot run the executable though. I've tried running it manually from within the container, but I get permission denied there too.
/usr/app $ whoami
node
/usr/app $ ls -l
total 41272
drwxrwxrwx 2 node node 20480 Sep 12 14:12 sqlpackage
/usr/app $ ./sqlpackage
/bin/sh: ./sqlpackage: Permission denied
Why can I not execute this file, even though the permissions seem to be correct?

Related

composer install in Dockerfile not saving dependencies

I have some trouble to dockerize a Symfony project. At the first start from cloning from git repo the dependencies have to installed through composer.
I have read many questions with the same background but i cant get it working.
i show u first my Dockerfile:
ARG PHP_VERSION=8.1
ARG APP_ENV=dev
# Prod image
FROM php:${PHP_VERSION}-fpm-alpine AS app_php
# Update
RUN apk --no-cache update
RUN apk --no-cache add bash git
# Install Node
RUN apk --no-cache add --update nodejs npm
RUN apk --no-cache add --update python3
RUN apk --no-cache add --update make
RUN apk --no-cache add --update g++
# Install pdo
RUN docker-php-ext-install pdo_mysql
# Install Composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
# Symfony CLI
RUN curl -sS https://get.symfony.com/cli/installer | bash && mv /root/.symfony/bin/symfony /usr/local/bin/symfony
# WORK DIR
WORKDIR /var/www/html
# https://getcomposer.org/doc/03-cli.md#composer-allow-superuser
ENV COMPOSER_ALLOW_SUPERUSER=1
ENV PATH="${PATH}:/root/.composer/vendor/bin"
COPY --from=composer:2 /usr/bin/composer /usr/bin/composer
# prevent the reinstallation of vendors at every changes in the source code
COPY composer.* symfony.* ./
RUN set -eux; \
if [ -f composer.json ]; then \
composer install --prefer-dist --no-dev --no-autoloader --no-scripts --no-progress; \
composer clear-cache; \
fi
RUN set -eux; \
mkdir -p var/cache var/log; \
if [ -f composer.json ]; then \
composer dump-autoload --classmap-authoritative --no-dev; \
composer dump-env prod; \
composer run-script --no-dev post-install-cmd; \
chmod +x bin/console; sync; \
fi
# copy sources
COPY . /var/www/html
RUN rm -Rf docker/
# Start Symfony server on Port 8000
EXPOSE 8000
#RUN symfony console doctrine:migrations:migrate
i can see that the packages were installed through the build process, but after docker-compose up the vendor folder isnt set.
Do u have an idea to solve this?
Running it for you, indeed there is no vendors files where you woudl expect them.
if you run a shell on your container you would see what's really happening:
Get your created container id or tag with docker image ls
And run it:
docker run --rm -it <CONTAINER_ID> /bin/bash
bash-5.1# ls /var/www/html/
Dockerfile composer.1 composer.2 symfony.1 var
bash-5.1# ls -al /root/.composer/
total 8
drwxr-xr-x 1 root root 50 Aug 14 15:47 .
drwx------ 1 root root 16 Aug 14 15:48 ..
-rw-r--r-- 1 root root 799 Aug 14 15:47 keys.dev.pub
-rw-r--r-- 1 root root 799 Aug 14 15:47 keys.tags.pub
bash-5.1# ls /usr/bin/composer
/usr/bin/composer
bash-5.1# ls /usr/local/bin/composer
/usr/local/bin/composer
bash-5.1# which composer
/usr/local/bin/composer
bash-5.1# which symfony
/usr/local/bin/symfony
bash-5.1#
The which command would make you realize:
you don't need to copy composer when you already install it
/usr/local/bin is already part of the PATH
The current ENV command is not necessary plus pointing to a non existing folder.
FYI to keep it slim I have created fake symfony.* and composer.* files and have no composer.json (not shared here).
I hope this helps you solve it.

Cannot COPY from previous stage in DockerFIle

This looks like a common issue so I checked a few SO posts but none of them solved my problem.
Here is my Dockerfile:
# MkDocs container
FROM python:3-alpine AS build-env
RUN apk add bash
RUN pip install --upgrade pip
RUN pip install pymdown-extensions \
&& pip install mkdocs \
&& pip install mkdocs-material \
&& pip install mkdocs-rtd-dropdown \
&& pip install mkdocs-git-revision-date-plugin \
&& pip install mkdocs-git-revision-date-localized-plugin \
&& pip install mkdocs-redirects
# executed at ~/Developer/MkDocs
RUN mkdir -p /home/mkdocs/
WORKDIR /home/mkdocs/
COPY . .
RUN mkdocs build -s
WORKDIR /
# Nginx container
FROM nginx:1.21.6-alpine
RUN apk add bash
EXPOSE 80
RUN cat /etc/nginx/nginx.conf
COPY nginx.conf /etc/nginx/nginx.conf
WORKDIR /
RUN mkdir -p /home/mkdocs
COPY --from=build-env /home/mkdocs/site/ /home/mkdocs/
RUN mv /home/mkdocs/* /usr/share/nginx/html/
RUN chown nginx:nginx /usr/share/nginx/html/*
USER nginx:nginx
And here is the command to run docker:
docker run -it --name mkdocs -p 8789:80 nginx
Running localhost:8789 only shows the default nginx homepage, not the built one of MkDocs. I also run docker exec -it --user root <PID> bash to check the directory /usr/share/nginx/html/ but the copied files are not there.
My other checks:
First, I'm 100% sure that the files built in the first stage works and exists
Second, this is what completely frustrated me out. If I run the docker using docker run -it --entrypoint=/bin/bash mkdocs:v1, I can actually see the built MkDocs files:
bash-5.1$ ls /usr/share/nginx/html
404.html 50x.html assets index.html search sitemap.xml sitemap.xml.gz
bash-5.1$

Dockerfile user creation permission not granted

i am learning docker, i have created a Dockerfile like this :
FROM node:alpine
RUN addgroup -g 1001 -S appuser && adduser -u 1001 -S appuser -G appuser
RUN apk update && apk add bash
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY ./wait-for-it.sh /usr/wait-for-it.sh
RUN chmod +x /usr/wait-for-it.sh
RUN chmod ugo+rwx /usr
RUN chown -R appuser:appuser /usr/src/app
USER appuser
COPY . .
And i have a docker-compose.override.yml like this
version: '3'
services:
main:
command: bash -c "/usr/src/app/wait-for-it.sh --timeout=0 mongo:27017 && npm run dev"
volumes:
- ./api/src:/usr/src/app/src
this is giving error in main container:
bash: /usr/src/app/wait-for-it.sh: Permission denied
Please help how can give permission to appuser. if i remove user creation everything works fine.
In docker it is common to run application containers as the root user. Remember this is the root user of the container not the host machine. If you do want to use a separate user to run the application within the container, I suggest you move the USER statement to the end of the Dockerfile and before that add a chown to update the ownership

Docker Toolbox not updating changes even after machine remove

Using DockerToolbox, I've been trying for the past few days to update my container to run in heroku.
I cant seem to update the code in the container.
Here are some of things I've tried:
in Docker file change COPY . /app to ADD . /app
Removed docker machine and create a virtualbox
`docker-machine rm default`
`docker-machine create --drive virtualbox default`
build/run docker image
`docker build --no-cache -t appname`
`docker run -it -p 8888:8080 appname`
Also tried docker build --no-cache .
Docker File
FROM python:3.6
# create and set working directory
RUN mkdir /app
WORKDIR /app
# Add current directory code to working directory
ADD . /app/
# set default environment variables
ENV PYTHONUNBUFFERED 1
ENV LANG C.UTF-8
ENV DEBIAN_FRONTEND=noninteractive
ENV PORT 8080
RUN wget http://prdownloads.sourceforge.net/ta-lib/ta-lib-0.4.0-src.tar.gz && \
tar -xvzf ta-lib-0.4.0-src.tar.gz && \
cd ta-lib/ && \
./configure --prefix=/usr && \
make && \
make install
COPY . /app
WORKDIR /app
COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt
RUN pip install ta-lib
EXPOSE 8080
CMD gunicorn appname.wsgi:application --bind 0.0.0.0:$PORT

How to change permission of a folder to 777 in Dockerfile?

I have a project directory like this:
|-var/www
|-docker-compose.yml
|-app
|--uploads
|---photos
|-Dockerfile
This is my docker-compose.yml file:
myapp:
build:
context: myfolder
dockerfile: Dockerfile
container_name: flask
image: api/v1:01
restart: unless-stopped
environment:
APP_ENV: "prod"
APP_DEBUG: "False"
APP_PORT: 5000
volumes:
- appdata:/var/www
What I want:
I want to change app/uploads/photos this folder's permission to 777.This is an upload folder,so user can upload to this folder.
My Dockerfile now is look like this:
FROM python:3.6.8-alpine3.9
ENV GROUP_ID=1000 \
USER_ID=1000
WORKDIR /var/www/
ADD . /var/www/
RUN apk add --no-cache build-base libffi-dev openssl-dev ncurses-dev
RUN pip install --upgrade pip
RUN pip install -r requirements.txt
RUN addgroup -g $GROUP_ID www
RUN adduser -D -u $USER_ID -G www www -s /bin/sh
USER www
EXPOSE 5000
After looking in this question,In order to achieve what I want,I tried below:
FROM python:3.6.8-alpine3.9
ENV GROUP_ID=1000 \
USER_ID=1000
WORKDIR /var/www/
ADD . /var/www/
RUN apk add --no-cache build-base libffi-dev openssl-dev ncurses-dev
RUN pip install --upgrade pip
RUN pip install -r requirements.txt
RUN addgroup -g $GROUP_ID www
RUN adduser -D -u $USER_ID -G www www -s /bin/sh
RUN chown -R www:www /var/www
RUN chmod -R 777 /var/www/uploads
RUN chmod -R 777 /var/www/uploads/photos
USER www
EXPOSE 5000
But seems like the chmod command in my dockerfile is not taking effect.Cause whenever I upload some files to app/uploads/photos in my code,my nginx server keep getting this error:
PermissionError: [Errno 13] Permission denied: '/var/www/uploads/photos/myfilename.png'
Somebody please help.Please provide a solution for how to change the permission of a folder in Dockerfile.
UPDATE:
I tried to change the permission of /var/www/uploads after build the container and the container is running by doing below:
docker exec -it myapp /bin/sh
then run
chmod -R 777 /var/www/uploads
What I get is chmod: /var/www/uploads: Operation not permitted
Therefore I suspect the this error will also happened when the docker is building,then according to this answer from serverfault, I tried to modify the dockerfile to this:
FROM python:3.6.8-alpine3.9
ENV GROUP_ID=1000 \
USER_ID=1000
WORKDIR /var/www/
ADD . /var/www/
USER root
RUN chmod -R 777 /var/www/uploads
RUN addgroup -g $GROUP_ID www
RUN adduser -D -u $USER_ID -G www www -s /bin/sh
USER www
RUN apk add --no-cache build-base libffi-dev openssl-dev ncurses-dev
RUN pip install --upgrade pip
RUN pip install -r requirements.txt
EXPOSE 5000
But it still doesnt work.Look like my above approach is also wrong.Cause I already run in root in the dockerfile.But at the same time,when I access the container in host using docker exec,also getting Operation not permitted.
I am very new in Docker.Just cant figure it out how to get this done.
What I hope to know:
1) How to change the folder var/www/uploads to permission 777?
2) What is the problem causing I cant change the permission from my approach?
3) What is the better way to achieve this? (If any)

Resources