irregular
irregular

Reputation: 1587

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

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

Upvotes: 16

Views: 33282

Answers (2)

diman82
diman82

Reputation: 738

In Docker windows, you can use relative path from project directory like this (where 'app' is a project directory and 'mnt' is a sub-directory holding some file/s to be mount/ed):

docker run -v %cd%\mnt:/app/mnt

Upvotes: 0

Alfonso Tienda
Alfonso Tienda

Reputation: 3689

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.

Upvotes: 21

Related Questions