Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 292
irregular Avatar asked Sep 11 '17 03:09

irregular


People also ask

How can you persist the data when working with a database container?

With the database being a single file, if we can persist that file on the host and make it available to the next container, it should be able to pick up where the last one left off. By creating a volume and attaching (often called “mounting”) it to the directory the data is stored in, we can persist the data.

How do you store data persistently in Docker?

Volumes are the best way to persist data in Docker. Bind mounts may be stored anywhere on the host system. They may even be important system files or directories. Non-Docker processes on the Docker host or a Docker container can modify them at any time.

Which is used to persist the database container data?

You can use bind mounts to persist data, but it can also add more data into containers. When working on an application, you can use a bind mount to mount source code into the container to let it see code changes, respond, and let you see the changes right away.


1 Answers

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.

like image 112
Alfonso Tienda Avatar answered Oct 07 '22 14:10

Alfonso Tienda