#Saving files to Docker Volumes

7 messages · Page 1 of 1 (latest)

oblique violet
#

So I have this little mockup application that allows a user to upload profile pictures, resizes them to 100x100 pixel jpeg, then displays that resized profile picture and allows them to download it. I guess what I don't understand is how to make this application work with docker, using docker volumes (which is how I think I'm supposed to get it to work).

In my project directory, I have a directory called uploads which is where the resized profile picture is supposed to be saved to using the following code: dst, err := os.Create("./uploads/pfp.jpeg") and then if _, err = dst.Write(jpegData); err != nil { /* handle error */ }. This works just fine when not containerized, but when it is containerized, I simply don't understand where I'm going wrong.

This is what my dockerfile currently looks like:

# syntax=docker/dockerfile:1.0

FROM golang:1.21.6 AS build-stage

WORKDIR /build

COPY go.mod go.sum ./

RUN go mod download && go mod verify

COPY /static ./static
COPY /templates ./templates
COPY *.go ./

COPY /uploads /uploads

RUN CGO_ENABLED=0 GOOS=linux go build -o ./docker_volumes

FROM build-stage AS test-stage

RUN go test ./...

FROM gcr.io/distroless/base-debian11 AS release-stage

WORKDIR /app

COPY --from=build-stage /build/static ./static
COPY --from=build-stage /build/templates ./templates
COPY --from=build-stage /build/docker_volumes ./docker_volumes

VOLUME [ "/app/uploads" ]

EXPOSE 8080/tcp

USER nonroot:nonroot

ENTRYPOINT [ "./docker_volumes" ]

And the command I use to run it is docker run -v my_volume:/app/uploads -p 8080:8080 my_app.

When my server navigates to localhost:8080/upload which is the path where the uploading happens, I get the following error: error opening destination file, err:open ./uploads/pfp.jpeg: permission denied. Any help at all is greatly appreciated, thank you! ❤️

solemn nimbus
#

chown the dir/file(s) .. the user "nonroot" doesn't have access to them as the previous build steps are running as root

pseudo prism
#

I usually run the container with the uid and gid passed in the docker run command

oblique violet
oblique violet
pseudo prism
#

Well you're modifying files on the mounted filesystem that have permissions for a different user. Passing the uid and gid from id to docker for your created user will give your container user the right permissions to edit those files

oblique violet