Docker: How can I have sqlite db changes persist to the db file?

后端 未结 1 967
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-20 12:49
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/beg         


        
1条回答
  •  别那么骄傲
    2021-02-20 13:25

    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.

    0 讨论(0)
提交回复
热议问题