Docker:如何让 sqlite db 更改持久保存到 db 文件? [英] Docker: How can I have sqlite db changes persist to the db file?

查看:39
本文介绍了Docker:如何让 sqlite db 更改持久保存到 db 文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自 golang:1.8添加 ./go/src/beginnerapp运行 go get -u github.com/gorilla/mux运行去获取 github.com/mattn/go-sqlite3运行安装beginnerapp/卷/go/src/beginnerapp/local-dbWORKDIR/go/src/beginnerapp入口点/go/bin/beginnerapp曝光 8080

sqlite db 文件位于 local-db 目录中,但我似乎没有正确使用 VOLUME 命令.有什么想法可以保留对 sqlite db 文件的 db 更改吗?

我不介意卷是在构建之前还是之后安装的.

我也试过运行以下命令

user@cardboardlaptop:~/go/src/beginnerapp$ docker run -p 8080:8080 -v ./local-db:/go/src/beginnerapp/local-db

docker:来自守护进程的错误响应:创建 ./local-db:./local-db"包括本地卷名的无效字符,仅[a-zA-Z0-9][a-zA-Z0-9_.-]"是允许的.如果您打算传递主机目录,请使用绝对路径.

使用 /absolutepath/local-db 而不是相对路径 ./local-db

解决方案

您没有在 Dockerfile 中挂载卷.VOLUME 告诉 docker 可以通过 docker run --volumes-from 挂载这些目录上的内容

你是对的.Docker 不允许在命令行上使用相对路径.

使用绝对路径运行你的 docker:

docker run -v/host/db/local-db:/go/src/beginnerapp/local-db

您的数据库将被持久化在主机文件 /host/db/local-db

如果你想使用相对路径,你可以使用带有volumes"标签的docker-compose:

卷:- ./local-db:/go/src/beginnerapp/local-db

你可以试试这个配置:

  • 将 Dockerfile 放在一个目录中,(例如 /opt/docker/myproject)
  • 在同一路径中创建一个 docker-compose.yml 文件,如下所示:
<块引用>

版本:2.0"服务:我的项目:建造: .卷:-./local-db:/go/src/beginnerapp/local-db"

  • 在同一路径下执行docker-compose up -d myproject.

您的数据库应该存储在 /opt/docker/myproject/local-db

只是一个评论.local-db 的内容(如果有)将替换为 ./local-db 路径的内容(空).如果容器有任何信息(初始化数据库),最好使用 docker cp 复制它,或者在入口点或命令 shell 脚本中包含任何初始化逻辑.

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

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?

I don't mind if the volume is mounted before or after the build.

I also tried running the following command

user@cardboardlaptop:~/go/src/beginnerapp$ docker run -p 8080:8080 -v ./local-db:/go/src/beginnerapp/local-db beginnerapp

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.

EDIT: Works with using /absolutepath/local-db instead of relative path ./local-db

解决方案

You are not mounting volumes in a Dockerfile. VOLUME tells docker that content on those directories can be mounted via docker run --volumes-from

You're right. Docker doesn't allow relative paths on volumes on command line.

Run your docker using absolute path:

docker run -v /host/db/local-db:/go/src/beginnerapp/local-db

Your db will be persisted in the host file /host/db/local-db

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

You can try this configuration:

  • 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"

  • Execute docker-compose up -d myproject in the same path.

Your db should be stored in /opt/docker/myproject/local-db

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 with docker cp or include any init logic on an entrypoint or command shell script.

这篇关于Docker:如何让 sqlite db 更改持久保存到 db 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆