Share folders and files between host and Docker as persistent data

36 Views Asked by At

I have a team of Python developers, and we want to have the same environment so we decided to use Docker and be able to create any file in host folder and Docker folder so it can be reflected in real time without re-running the docker, the first approach was to use Volumes to share files between the host and the Docker so data will be available after stopping or deleting the image. It worked as root user, but when i moved to non-root user it did not work, now i can create docs or folders from the host and is shown in the docker but when i try to create from the docker i get this error

Unable to write file 'vscode-remote://attached-container+7b22636f6e7461696e65724e616d65223a222f616970726f6a6563742d636f6e7461696e6572227d/home/dsteam/app/dscode/src/test int.txt' (NoPermissions (FileSystemError): Error: EACCES: permission denied, open '/home/dsteam/app/dscode/src/test int.txt')

When i check the permission

dsteam@636434b7593e:~/app$ ls -l
total 28
-rw-rw-r--  1 1000 1000  617 Mar 26 19:34 Dockerfile
-rw-rw-r--  1 1000 1000  908 Mar 26 19:34 docker-compose.yml
drwxrwxr-x  6 1000 1000 4096 Mar 20 08:13 dscode
drwxrwxr-x 14 1000 1000 4096 Mar 20 07:57 legacy
-rw-rw-r--  1 1000 1000 8930 Mar 20 08:24 requirements.txt

dsteam@636434b7593e:~/app/dscode$ touch testint.txt
touch: cannot touch 'testint.txt': Permission denied


I am using Visual Studio Code to work with Docker

I am using Dockerfile and compose-docker.yml

here is the content

FROM python:3.10

# Create non-root user with bash as the default shell
RUN groupadd -r dsteam && useradd -r -g dsteam -m -s /bin/bash dsteam

# Set the working directory
WORKDIR /home/dsteam/app

# Copy and install requirements
COPY ./requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt

# Change directory ownership
RUN chown -R dsteam:dsteam /home/dsteam/app

# Expose the port
EXPOSE 5001

# Switch to non-root user
USER dsteam

# Run your command
# CMD [ "uvicorn", "src.server.app:app", "--host", "0.0.0.0", "--port", "80", "--reload" ]

# Specify the Docker Compose version
version: '3'

# Define the services (containers) to be created and managed by Docker Compose
services:
  # Define a service named "aiproject"
  aiproject:
    # Build configuration for the service
    build: .
    # Image name to tag the built image
    image: aiproject-image
    # Name of the container created from the image
    container_name: aiproject-container
    # Port mapping: map port 80 on the host to port 80 in the container
    ports:
      - "80:80"
    # Volume mounting: mount the current directory on the host to /app in the container
    volumes:
      - ".:/home/dsteam/app"  #  to represent the current working directory
    # Open stdin (standard input stream) for the container
    stdin_open: true
    # Allocate a pseudo-TTY (terminal) for the container
    tty: true
    # Override the default command with bash
    command: ["bash"]
0

There are 0 best solutions below