Skip to content

Commit

Permalink
Create data volume with configuration seed data (#196)
Browse files Browse the repository at this point in the history
* Adds a seed container that we copy the default configurations onto. Then we mount a volume (data) onto the path where the seed data has been copied on this image. When the volume is created, this mount configuration will copy the contents from the seed container into the volume. On subsequent start ups it will be a no-op, which is exactly what we want.
  • Loading branch information
cgardens authored Sep 9, 2020
1 parent ee805e1 commit 9f71989
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .env
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ DATABASE_USER=docker
DATABASE_PASSWORD=docker
DATABASE_DB=dataline
DATABASE_URL=jdbc:postgresql://db:5432/dataline
CONFIG_ROOT=data/config
CONFIG_ROOT=/data
WORKSPACE_ROOT=/tmp/workspace
WORKSPACE_DOCKER_MOUNT=workspace
13 changes: 11 additions & 2 deletions Dockerfile.build
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,17 @@ EXPOSE 80

COPY --from=build /code/dataline-webapp/build /usr/share/nginx/html

####################
# Build seed image #
####################
FROM alpine:3.4 AS seed

WORKDIR /app

# the sole purpose of this image is to seed the data volume with the default data
# that the app should have when it is first installed.
COPY --from=build /code/dataline-config/init/src/main/resources/config seed/config

######################
# Build server image #
######################
Expand All @@ -66,7 +77,6 @@ WORKDIR /app
ADD https://github.com/ufoscout/docker-compose-wait/releases/download/${WAIT_VERSION}/wait wait
RUN chmod +x wait

COPY --from=build /code/dataline-config/init/src/main/resources/config data/config
COPY --from=build /code/${APPLICATION}/build/distributions/${APPLICATION}*.tar ${APPLICATION}.tar

RUN tar xf ${APPLICATION}.tar --strip-components=1
Expand All @@ -88,7 +98,6 @@ WORKDIR /app
ADD https://github.com/ufoscout/docker-compose-wait/releases/download/${WAIT_VERSION}/wait wait
RUN chmod +x wait

COPY --from=build /code/dataline-config/init/src/main/resources/config data/config
COPY --from=build /code/${APPLICATION}/build/distributions/${APPLICATION}*.tar ${APPLICATION}.tar

RUN tar xf ${APPLICATION}.tar --strip-components=1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
// we force all interaction with disk storage to be effectively single threaded.
public class DefaultConfigPersistence implements ConfigPersistence {

private static final String CONFIG_DIR = "config";

private static final Object lock = new Object();

private final JsonSchemaValidator jsonSchemaValidator;
Expand All @@ -51,7 +53,7 @@ public DefaultConfigPersistence(final Path storageRoot) {
}

public DefaultConfigPersistence(final Path storageRoot, final JsonSchemaValidator schemaValidator) {
this.storageRoot = storageRoot;
this.storageRoot = storageRoot.resolve(CONFIG_DIR);
jsonSchemaValidator = schemaValidator;
}

Expand Down
9 changes: 6 additions & 3 deletions docker-compose.build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ services:
build:
dockerfile: Dockerfile.database
context: .
seed:
image: dataline/seed:dev
build:
dockerfile: Dockerfile.build
target: seed
context: .
scheduler:
image: dataline/scheduler:dev
build:
Expand All @@ -25,9 +31,6 @@ services:
target: webapp
context: .

volumes:
workspace:

networks:
dataline:
driver: "bridge"
23 changes: 23 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,23 @@ services:
- POSTGRES_USER=${DATABASE_USER}
- POSTGRES_PASSWORD=${DATABASE_PASSWORD}
- POSTGRES_DB=db-${DATABASE_DB}
seed:
image: dataline/seed:${VERSION}
container_name: dataline-data-seed
environment:
- ENV=${ENV}
# When the volume is created for the first time it will be populated with the contents of /app/seed,
# which is prepopulated with the configs that we want config persistence to have access to upon
# installation. On subsequent compose up / down, this will get mounted, but not copy any data.
# Effectively it'll be a no-op on subsequent runs, though we may be able to use it to add new data
# to the volume if we need to as we release updates.
# reference:
# https://docs.docker.com/storage/volumes/#populate-a-volume-using-a-container
# https://docs.docker.com/compose/compose-file/#long-syntax-3
volumes:
- type: volume
source: data
target: /app/seed
scheduler:
image: dataline/scheduler:${VERSION}
container_name: dataline-scheduler
Expand All @@ -24,8 +41,10 @@ services:
volumes:
- /var/run/docker.sock:/var/run/docker.sock
- "${WORKSPACE_DOCKER_MOUNT}:${WORKSPACE_ROOT}"
- "data:${CONFIG_ROOT}"
depends_on:
- db
- seed
server:
image: dataline/server:${VERSION}
container_name: dataline-server
Expand All @@ -39,6 +58,8 @@ services:
- CONFIG_ROOT=${CONFIG_ROOT}
ports:
- 8001:8001
volumes:
- "data:${CONFIG_ROOT}"
depends_on:
- db
webapp:
Expand All @@ -52,3 +73,5 @@ services:
volumes:
workspace:
name: ${WORKSPACE_DOCKER_MOUNT}
data:
name: data

0 comments on commit 9f71989

Please sign in to comment.