Docker permission denied when writing files inside container - docker

I'm trying to download a PDF and save it inside my application which is dockerized. The problem seems to be that I'm not allowed to write files inside the container. Here are the steps I took:
I first created a named volume myvol: docker volume create myvol. I then updated my docker-compose.yml file as follows:
version: "3"
services:
webpp:
container_name: webapp
image: webapp:latest
build: .
ports:
- "8000:8000"
volumes:
- myvol:/app
volumes:
myvol:
And for reference, my Dockerfile:
FROM python:3.7
WORKDIR /app
ADD ./requirements.txt .
RUN pip install -r requirements.txt
ADD . .
RUN groupadd -g 999 appuser && \
useradd -r -d /app -u 999 -g appuser appuser
RUN chown -R appuser:appuser /app
USER appuser
RUN mkdir webapp/_tmp; chmod a+rwx webapp/_tmp/
EXPOSE 8000
ENTRYPOINT ["python", "main.py"]
When I run my code, I get a permission denied. The same happens if I exec into the container and try to write something, or for example wget http://duck.com also gives me a Permission denied error.
I'm unsure what's wrong, as even docker inspect seems correct...
"Mounts": [
{
"Type": "volume",
"Name": "myvol",
"Source": "/var/lib/docker/volumes/myvol/_data",
"Destination": "/app",
"Driver": "local",
"Mode": "rw",
"RW": true,
"Propagation": ""
}
],
...
"Volumes": {
"/app": {}
},
"WorkingDir": "/app",
"Entrypoint": [
"python",
"main.py"
],
...

the volumes section in your compose - myvol:/app will overwrite the permission and all what it contains in the /app folder , you may delete it from your compose.
if you want to use volumes to presist data , create another mount as /app and point to it in your python code

Related

Nestjs Docker Debug

I have a simple Dockerfile
FROM node:12.13-alpine As development
WORKDIR /usr/src/app
COPY package*.json ./
RUN apk add --no-cache bash && npm install --only=development
COPY . .
RUN npm run build
FROM node:12.13-alpine as production
ARG NODE_ENV=production
ENV NODE_ENV=${NODE_ENV}
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install --only=production
COPY . .
COPY --from=development /usr/src/app/dist ./dist
CMD ["node", "dist/main"]
my docker-compose is:
version: '3.7'
services:
main:
container_name: main
build:
context: .
target: development
volumes:
- .:/usr/src/app
- /usr/src/app/node_modules
ports:
- ${SERVER_PORT}:${SERVER_PORT}
- 9229:9229
command: npm run start:debug
env_file:
- .env
Inside my package.json and script, I have:
"start:debug": "nest start --debug 0.0.0.0:9229 --watch"
I added the conf in my visual Studio Code (.vscode/launch.json)
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "attach",
"name": "Debug",
"address": "127.0.0.1",
"port": 9229,
"sourceMaps": true,
"restart": true,
"localRoot": "${workspaceFolder}",
"remoteRoot": "/usr/src/app",
"skipFiles": ["<node_internals>/**"]
}
]
}
After that I can run docker-compose up --build and start my debugger but the code doesn't stop at my breakpoint.
I use Mac 12.6 and Docker version 20.10.13

Debugging with 'Docker .NET Core Attach' not working anymore

I have several ASP.NET Core (6.0) WebApi projects that are dockerized using docker-compose. For local development, I use a docker-compose file which references Dockerfiles that build / publish the projects in Debug mode. Then in order to debug, I use the 'Docker .NET Core Attach (Preview)' launch configuration and select the corresponding docker container, which then prompts me about copying the .NET Core debugger into the container.
Until recently, this always worked and I could debug inside the container. Now suddenly, after being prompted and trying to copy the debugger into the container, I always get the following error:
Starting: "docker" exec -i web_roomservice /remote_debugger/vsdbg
--interpreter=vscode
Error from pipe program 'docker': FATAL ERROR: Failed to initialize dispatcher with error 80131534
The pipe program 'docker' exited unexpectedly with code 255.
I tried re-installing the Docker Engine + docker-compose (with the latest version), re-installing VS Code + the 'Docker' and 'C#' extensions, migrating from ASP.NET Core 5.0 to 6.0 (since 5.0 is not supported anymore) and obviously rebuilding my images multiple times, but nothing seems to work and I can't find anything online. Any help with this would be greatly appreciated, since as of now I can't debug which sucks.
These are my docker-compose, Debug-Dockerfile and launch config (for one project / service):
version: "3.7"
services:
roomservice:
image: web_roomservice
container_name: web_roomservice
build:
context: ./
dockerfile: Dockerfile.RoomService.Debug
expose:
- "5011"
volumes:
- /etc/localtime:/etc/localtime:ro
environment:
- ASPNETCORE_ENVIRONMENT=Development
user: "root:root"
logging:
driver: "json-file"
options:
max-size: "5m"
(There's more but I only included the section with this one service)
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
#EXPOSE 5011
ENV ASPNETCORE_URLS=http://+:5011
# Install netpbm which is used for .pgm to .png file conversion for map images
RUN apt-get -y update --silent
RUN apt-get -y install netpbm --silent
# Creates a non-root user with an explicit UID and adds permission to access the /app folder
# For more info, please refer to https://aka.ms/vscode-docker-dotnet-configure-containers
RUN adduser -u 5678 --disabled-password --gecos "" appuser && chown -R appuser /app
USER appuser
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["RoomService/RoomService.csproj", "./RoomService/"]
COPY ["EventBusRabbitMQ/EventBusRabbitMQ.csproj", "./EventBusRabbitMQ/"]
COPY ["Common/Common.csproj", "./Common/"]
RUN dotnet restore "RoomService/RoomService.csproj"
COPY RoomService ./RoomService
COPY EventBusRabbitMQ ./EventBusRabbitMQ
COPY Common ./Common
WORKDIR "/src/RoomService"
RUN dotnet build "RoomService.csproj" -c Debug -o /app/build
FROM build AS publish
RUN dotnet publish "RoomService.csproj" -c Debug -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "RoomService.dll"]
(This Dockerfile is placed in the workspace folder (parent of the actual RoomService project folder) in order to include the Common project)
{
"version": "0.2.0",
"configurations": [
{
"name": "Docker .NET Core Attach (Preview)",
"type": "docker",
"request": "attach",
"platform": "netCore",
"sourceFileMap": {
"/src/RoomService": "${workspaceFolder}"
}
}
]
}
(This launch configuration is placed in the actual RoomService project folder's .vscode subfolder)
Had the same problem today. Try deleting the ~/vsdbg directory, if you are on MacOS.
David is right. I was having the same issue. I am running WSL2 (Ubuntu) and when I deleted the ~/.vsdbg directory that fixed the issue for me.
This configuration below works perfectly in .NET6.
content of .vscode/launch.json file:
{
"version": "0.2.0",
"configurations": [
{
"name": ".NET Core Docker Attach",
"type": "coreclr",
"request": "attach",
"processId": "${command:pickRemoteProcess}",
"pipeTransport": {
"pipeCwd": "${workspaceRoot}",
"pipeProgram": "docker",
"pipeArgs": [ "exec", "-i", "your-container-name" ],
"debuggerPath": "/root/vsdbg/vsdbg",
"quoteArgs": false
},
"sourceFileMap": {
"path/in/container": "${workspaceRoot}/local/path"
}
}
]
}
I have also installed dotnet debugger in my Dockerfile as follow:
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS base
...
FROM base AS debug
RUN apt-get update
RUN apt-get install -y procps
RUN apt-get install -y unzip
RUN curl -sSL https://aka.ms/getvsdbgsh | /bin/sh /dev/stdin -v latest -l ~/vsdbg
...
Full instruction is covered in this YouTube tutorial: Debugging .NET Core in Docker with VSCode

vscode Docker debugging csharp.listRemoteProcess failed

I have a .Net 5 WebApi that i want to host and debug in Docker.
when i run the docker-compose the project launches correctly.
When attaching the debugger i get following error:
Running the contributed command csharp.listRemoteProcess failed
my docker file:
FROM mcr.microsoft.com/dotnet/sdk:5.0 as debug
ENV DOTNET_USE_POLLING_FILE_WATCHER=true
ENV ASPNETCORE_URLS=http://+:5000
EXPOSE 5000/tcp
WORKDIR /app
COPY . /app
RUN apt-get update
RUN apt-get install -y unzip
RUN curl -sSL https://aka.ms/getvsdbgsh | /bin/sh /dev/stdin -v latest -l ~/vsdbg
ENTRYPOINT ["dotnet", "watch", "run", "--project myproject.csproj"]
the docker-compose file
version: '3.4'
services:
server:
build:
context: ./myproject
target: debug
ports:
- "5000:5000"
container_name: myproject
volumes:
- ./myproject:/app
launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": ".NET Core Docker Attach",
"type": "coreclr",
"request": "attach",
"processId": "${command:pickRemoteProcess}",
"pipeTransport": {
"pipeProgram": "docker",
"pipeArgs": [ "exec", "-i", "csharp" ],
"debuggerPath": "/root/vsdbg/vsdbg",
"pipeCwd": "${workspaceRoot}",
"quoteArgs": false
},
"sourceFileMap": {
"/work": "${workspaceRoot}/myproject/"
}
}
]
}

dockerfile cant find entrypoint.sh

My filesystem:
Dockerfile
entrypoint.sh
package.json
/shared_volume/
Dockerfile
FROM node:8
# Create and define the node_modules's cache directory.
RUN mkdir /usr/src/cache
WORKDIR /usr/src/cache
COPY . .
RUN npm install
# Create and define the application's working directory.
RUN mkdir /usr/src/app
WORKDIR /usr/src/app
# entrypoint to copy the node_modules and root files into /usr/src/app, to be shared with my local volume.
ENTRYPOINT ["/usr/src/cache/entrypoint.sh"]
package.json
{
"name": "test1",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "echo hello world start"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"rimraf": "^3.0.1"
}
}
entrypoint.sh
#!/bin/bash
cp -r /usr/src/cache/. /usr/src/app/.
command line - bash script
If I run this code (note: using windows 10 with cmder, hence %cd% not pwd):
docker run -it --rm -v %cd%/shared_volume:/app --privileged shared-volume-example bash
Error
standard_init_linux.go:211: exec user process caused "no such file or directory"
If I take out the reference to entrypoint, then the code works, so what is going on with the entrypoint?
Any suggestions.
thanks
ok someone else posted an answer regarding line endings. I used atom line-ending-selector and set it to LF in replacement, and resaved the file and now it works.

docker-compose volume not synced

PROBLEM
I use docker-compose for development with Python/Flask. I want my host codebase to sync with one inside docker container but not...
SITUATION
My working directory structure is below:
.
├── Dockerfile
├── docker-compose.yml
├── app.py
└── requirements.txt
I made bind mount from host's current directory to container's /app.
Dockerfile:
FROM python:3.7.3-alpine3.9
WORKDIR /app
COPY requirements.txt requirements.txt
RUN pip install --upgrade pip && \
pip install -r requirements.txt
COPY . .
CMD gunicorn -b 0.0.0.0:9000 -w 4 app:app
docker-compose.yml:
version: '3'
services:
web:
build: .
ports:
- "4649:9000"
volumes:
- .:/app
When I access http://localhost:4649 I can see correct response so Docker container is working well. However response don't update when I change app.py.
I inspected the container and the result is below
"Mounts": [
{
"Type": "bind",
"Source": "/Users/emp-mac-zakiooo/dev/jinja-pwa",
"Destination": "/app",
"Mode": "rw",
"RW": true,
"Propagation": "rprivate"
}
],
It looks very right so I have no idea about this problem 😇
OMG I found my files are correctly synced but gunicorn cached them so added --reload to CMD in Dockerfile, and finally it fixed.
Thank you for helping and soooo sorry for my foolishness...!
You could achieve that by doing the following:
volumes:
mount:
driver: local
driver_opts:
type: nfs
o: addr=host.docker.internal,rw,nolock,hard,nfsvers=3
device: ":${PWD}/path"
In your volume declaration within a service, you can do the following:
version: '3'
services:
web:
build: .
ports:
- "4649:9000"
volumes:
- mount:/app
Add the following to /etc/exports to enable docker access to your nfs volumes
<path to $WORKSPACE> 127.0.0.1 -alldirs -mapall=0:80 once it's there you need to run sudo nfsd restart to include your changes.
if ever docker-compose stops responding when using NFS, restarting docker usually fixes it
I hope that this helps!

Resources