Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
PostgreSQL import schema and data into docker container with gitlab ci
.docker_build_database:
extends: .docker_base
variables:
PGPASSWORD: postgres
POSTGRES_IMAGE: "${CI_REGISTRY}/${CI_PROJECT_PATH}/postgres:latest"
SPECIFIC_IMAGE_NAME: database
DOCKER_TAG: latest
before_script: &docker_build_database_before_script
- export DOCKER_IMAGE="${CI_REGISTRY}/${CI_PROJECT_PATH}/${SPECIFIC_IMAGE_NAME}"
- *docker_base_before_script
- apk add --no-cache postgresql-client
- docker run -p5432:5432 -ePGDATA=/var/lib/postgres/data_fixed -ePOSTGRES_PASSWORD=${PGPASSWORD} --name db -d ${POSTGRES_IMAGE}
- sleep 10
- docker ps
after_script: &docker_build_database_after_script
- export DOCKER_IMAGE="${CI_REGISTRY}/${CI_PROJECT_PATH}/${SPECIFIC_IMAGE_NAME}"
- docker stop db
- docker commit db ${DOCKER_IMAGE}:${VERSION}
- docker push ${DOCKER_IMAGE}:${VERSION}
- docker tag ${DOCKER_IMAGE}:${VERSION} ${DOCKER_IMAGE}:${DOCKER_TAG}
- docker push ${DOCKER_IMAGE}:${DOCKER_TAG}
.build-db-image-with-data:
extends: .docker_build_database
script:
- psql -v ON_ERROR_STOP=1 -U postgres -h localhost < ./test-data/init.sql
- psql -v ON_ERROR_STOP=1 -d <DATABASE_NAME> -U <USER> -h localhost < ./test-data/load.sql
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment