Docker:如何使sqlite数据库更改持久保存到db文件? [英] Docker: How can I have sqlite db changes persist to the db file?
问题描述
FROM golang:1.8
ADD . /go/src/beginnerapp
RUN go get -u github.com/gorilla/mux
RUN go get github.com/mattn/go-sqlite3
RUN go install beginnerapp/
VOLUME /go/src/beginnerapp/local-db
WORKDIR /go/src/beginnerapp
ENTRYPOINT /go/bin/beginnerapp
EXPOSE 8080
sqlite db文件位于 local-db
目录中,但我似乎没有正确使用 VOLUME
命令。
The sqlite db file is in the local-db
directory but I don't seem to be using the VOLUME
command correctly. Any ideas how I can have db changes to the sqlite db file persisted?
我不介意如何对sqlite db文件进行数据库更改吗?
I don't mind if the volume is mounted before or after the build.
我还尝试运行以下命令
user@cardboardlaptop:~/go/src/beginnerapp$ docker run -p 8080:8080 -v ./local-db:/go/src/beginnerapp/local-db beginnerapp
docker:来自守护程序的错误响应:create ./local-db:./local-db包括本地卷名的无效字符,只有 [a-zA-Z0-9]允许[a-zA-Z0-9 _.-]。如果要传递主机目录,请使用绝对路径。
docker: Error response from daemon: create ./local-db: "./local-db" includes invalid characters for a local volume name, only "[a-zA-Z0-9][a-zA-Z0-9_.-]" are allowed. If you intended to pass a host directory, use absolute path.
编辑:可以使用 / absolutepath / local-db
而不是相对路径。/local-db
Works with using /absolutepath/local-db
instead of relative path ./local-db
推荐答案
您不是在Dockerfile中挂载卷。
VOLUME告诉docker这些目录中的内容可以通过docker run --volumes-from
You are not mounting volumes in a Dockerfile. VOLUME tells docker that content on those directories can be mounted via docker run --volumes-from
来安装。 Docker不允许在命令行上使用卷上的相对路径。
You're right. Docker doesn't allow relative paths on volumes on command line.
使用绝对路径运行docker:
Run your docker using absolute path:
docker run -v / host / db / local-db:/ go / src / beginnerapp / local-db
您的数据库将保留在主机文件 / host / db / local-db
Your db will be persisted in the host file /host/db/local-db
中使用相对路径,可以使其与带有 volumes标记的docker-compose一起使用:
If you want to use relative paths, you can make it work with docker-compose with "volumes" tag:
volumes:
- ./local-db:/go/src/beginnerapp/local-db
您可以尝试此配置:
- 将Dockerfile放在目录中(例如
/ opt / docker / myproject
) - 在同一路径中创建
docker-compose.yml
文件:
- Put the Dockerfile in a directory, (e.g.
/opt/docker/myproject
) - create a
docker-compose.yml
file in the same path like this:
version: "2.0"
services:
myproject:
build: .
volumes:
- "./local-db:/go/src/beginnerapp/local-db"
- 在同一路径中执行
docker-compose up -d myproject
。 / li>
- Execute
docker-compose up -d myproject
in the same path.
您的数据库应存储在
/ opt / docker / myproject / local-db
只是一个评论。 local-db(如果有)的内容将替换为
./ local-db
路径(空)的内容。如果容器具有任何信息(初始化的数据库),则最好使用docker cp
复制它,或者在入口点或命令shell脚本中包含任何初始化逻辑。Just a comment. The content of local-db (if any) will be replaced by the content of
./local-db
path (empty). If the container have any information (initialized database) will be a good idea to copy it withdocker cp
or include any init logic on an entrypoint or command shell script.这篇关于Docker:如何使sqlite数据库更改持久保存到db文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- Execute