I want to write a docker file where my container can load db file from a directory and past it to application directory and on exit wrtie it back

huangapple go评论83阅读模式
英文:

I want to write a docker file where my container can load db file from a directory and past it to application directory and on exit wrtie it back

问题

我正在使用保存信息到sqlite文件的Golang应用程序,并且该文件位于与Docker文件相同的目录下的data/sqlite.db中。我的Docker文件如下所示:

p.s:这是我第一个Docker文件,请对我友善一点 I want to write a docker file where my container can load db file from a directory and past it to application directory and on exit wrtie it back

FROM golang:1.16.4

ENV GIN_MODE=release
ENV PORT=8081

ADD . /go/src/multisig-svc

WORKDIR /go/src/multisig-svc

RUN go mod download

RUN go build -o bin/multisig-svc cmd/main.go

EXPOSE $PORT
ENTRYPOINT ./bin/multisig-svc

我将这个应用程序部署到了Google Cloud平台,但不知何故容器在那里重新启动后,我的数据库就消失了。所以我进行了一些研究并尝试使用卷(volumes)。
我使用以下命令构建容器:docker build -t svc .,然后使用docker run -p 8080:8081 -v data:/var/dump -it svc运行它,但我发现data文件夹没有被复制到/var/dump目录中。我的基本想法是,每当容器启动时,它会从dump目录加载db文件,然后将其复制到data目录供应用程序使用,当容器退出时,将其复制回dump目录。我不知道我是否正确,任何帮助将不胜感激。

#编辑
问题是当15分钟内没有请求到达时,GPC会关闭容器,并在再次有请求时启动它。现在的问题是,如何从dump目录中获取db文件,更新它,并在容器关闭时将其写回dump目录以供将来使用。

英文:

I am working with Golang application that saves the information inside sqlite file and that resideds inside the data/sqlite.db same directory as docker file. My docker file is something like this

p.s: guys it's my very first docker file please be kind to me I want to write a docker file where my container can load db file from a directory and past it to application directory and on exit wrtie it back

FROM golang:1.16.4

ENV GIN_MODE=release
ENV PORT=8081

ADD . /go/src/multisig-svc

WORKDIR /go/src/multisig-svc

RUN go mod download

RUN go build -o bin/multisig-svc cmd/main.go

EXPOSE $PORT
ENTRYPOINT ./bin/multisig-svc

I deployed this application to the Google cloud plateform but somehow the container gets restarted there and after that my db is vanished. So i researched and try to use volumes.
I build the container using this command docker build -t svc . and then run it with docker run -p 8080:8081 -v data:/var/dump -it svc but i can not see the data folder is getting copied to /var/dump directory. My basic idea is , Whenever the container start it loads the db file from dump and then past it to data directory so application can use it and when it exits it copy it back to dump directory. I don't know if i am on right track any help would really be appreciated.

#Edit
The issue is when no request arrives for 15 minutes GPC shut down the container and start it when there comes a request again. Now the issue is to somehow fetch the db file from dump directory update it and write it back to the dump dir when container goes down for future use.

答案1

得分: 3

对于本地运行和在虚拟机上运行的情况,您需要指定要作为绑定挂载点挂载到目录中的目录的绝对路径。在这种情况下,类似以下的命令应该可以工作:

docker run -p 8080:8081 -v $(pwd)/data:/var/dump -it svc

当您不指定绝对路径时,您挂载到正在运行的容器的卷是由Docker守护程序管理的命名卷。它不位于与当前工作目录相关的路径中。您可以在此处找到有关Docker卷工作原理的更多信息:https://docs.docker.com/storage/volumes/

然而,在GCP上有多个环境(应用引擎、Kubernetes、虚拟机),因此根据您的环境,您可能需要调整此解决方案。

英文:

For a local run and if you are running on a VM, you need to specify the absolute path of the directory you want to mount as a bind mount into your directory. In this case something like that should work

docker run -p 8080:8081 -v $(pwd)/data:/var/dump -it svc

When you don't specify the absolute path, the volume you're mounting to your running container is a named volume manage by the docker daemon. And it is not located in a path related to your current working directory. You can find more information about how work docker volumes here https://docs.docker.com/storage/volumes/

However there are multiple environment on GCP (app engine, kubernetes, VMs), so depending on your environment you may need to adapt this solution.

huangapple
  • 本文由 发表于 2022年7月22日 06:10:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/73073176.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定