Skip to content

1. Building MySQL images with DB

Maksym Zaporozhets edited this page Jun 19, 2023 · 2 revisions

How it works

build-db-image

Step 1. Push the DB image to AWS S3

CLI commands docker:mysql:import-db or docker:mysql:upload-to-aws push DB dump to the AWS S3 bucket. In addition to uploading a DB dump, these CLI commands get MySQL metadata (version, environment variables, my.cnf configuration) from the running Docker container. Later this metadata is used to generate a Docker image with the database.

Check documentation for these commands for more information about uploading dumps to AWS S3. See Using AWS credentials with Dockerizer to learn more about environment variables.

Step 2. Use AWS Lambda to trigger the pipeline

AWS Lambda is configured to listen to the bucket events. It triggers the GitLab CI/CD pipeline when the DB dump metadata is added or updated in the S3 bucket.

Step 3. Use a CI/CD pipeline to build an image

The pipeline runs the docker:mysql:reconstruct-db command to download files from the S3 bucket and to generate a DB image from metadata JSON file and a compressed database dump.

Conventions

You'll have to enter a Docker image name in order for the tool to know where to push the tagged image once it is ready. Example image name (target_path in the metadata file): registry.gitlab.com/namespace/project/database-prod. In this example, first three parts - domain, namespace, project - match the Git repository. This way you can push the image to the GitLab Container Registry in the same project.

Always use at least prod/dev/etc. suffix to separate databases.

Summary:

  • Example GitLab project: https://gitlab.com/namespace/project
  • Docker image name (e.g. target_image): registry.gitlab.com/namespace/project/database-prod
  • AWS S3 Bucket name: default-value-php-dept-namespace. It consists of DOCKERIZER_AWS_S3_BUCKET_PREFIX + project namespace. Can be explicitly passed to the command.
  • AWS S3 file paths are s3://default-value-php-dept-namespace/namespace/project/database-prod.json and s3://default-value-php-dept-namespace/namespace/project/database-prod.sql.gz for metadata and dump files respectively.

Tip

Use Docker label com.default-value.docker.registry.target-image to store the image name.

Example docker-compose.yaml:

services:
  mysql:
    labels:
      - com.default-value.docker.registry.target-image=registry.gitlab.com/namespace/project/database-prod

Note that labels are applied only after you restart the composition with the --force-recreate key.

Clone this wiki locally