Docker容器中文件的Golang路径[关闭]

I have a .pem file which I can access with no problem using filepath.Abs when my application is not running inside docker. When the app is running inside docker, golang cant find the file anymore.

How can I access the file with golang running inside a docker container?

Thanks

You should use volumes to share your pem file between your host and your container, something like :

docker run -v /path/to/my/file.pem:/app/file.pem <your_image>

The application inside the Docker container will then be able to access the file file.pem located in /app/ folder.

you should set the workdir in Dockerfile

RUN mkdir -p $GOPATH/code
WORKDIR $GOPATH/code  /your-code-directory

Or for example write your Dockerfile like that example:

FROM golang:latest

ADD . /app
WORKDIR /app

RUN go get -d -v ./...
RUN go build -o main .
RUN chmod a+x /docker_entrypoint.sh
ENTRYPOINT ["/docker_entrypoint.sh"]

In docker_entrypoint.sh you can prepare your code, copy or create files and start your project exec run server.go