Skip to content

Commit

Permalink
Merge pull request #1 from pagemachine/init
Browse files Browse the repository at this point in the history
Initialize setup
  • Loading branch information
mbrodala authored Jun 28, 2024
2 parents 3a04f67 + 69d4cae commit ff1534e
Show file tree
Hide file tree
Showing 6 changed files with 161 additions and 0 deletions.
41 changes: 41 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: CI

on:
push:
branches:
- master
tags:
- '*'
pull_request:
branches:
- master
schedule:
- cron: '0 7 * * *'

jobs:
build:
runs-on: ubuntu-latest

defaults:
run:
working-directory: ./test

env:
BUILD_IMAGE_TAG: ${{ github.run_id }}

steps:
- uses: actions/checkout@v4

- name: Build & Start
run: docker compose up --detach --wait mysql

- name: Dump & Diff
run: >
docker compose exec mysql
mysqldump --compact --skip-extended-insert db
|
diff --unified --color expected.sql -
- name: Cleanup
if: ${{ always() }}
run: docker compose down --rmi=all --volumes
28 changes: 28 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
ARG MYSQL_IMAGE_VERSION
FROM mysql:${MYSQL_IMAGE_VERSION} as base

ARG MYSQL_DATABASE
ENV MYSQL_DATABASE=${MYSQL_DATABASE}
ARG MYSQL_USER
ENV MYSQL_USER=${MYSQL_USER}
ARG MYSQL_PASSWORD
ENV MYSQL_PASSWORD=${MYSQL_PASSWORD}
ARG MYSQL_ROOT_PASSWORD
ENV MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}

FROM base as init

COPY --from=data * /docker-entrypoint-initdb.d

RUN mkdir --parents /var/lib/mysql-data \
&& chown mysql:mysql /var/lib/mysql-data

RUN sed --in-place 's/exec "$@"//' /usr/local/bin/docker-entrypoint.sh

RUN docker-entrypoint.sh --datadir /var/lib/mysql-data

FROM base

COPY --from=init /var/lib/mysql-data /var/lib/mysql

RUN chown mysql:mysql /var/lib/mysql
55 changes: 55 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,58 @@

This repository provides a `Dockerfile` to build [MySQL](https://hub.docker.com/_/mysql)
Docker images with data.

## Usage

Adjust your Docker Compose MySQL service by adding a `build` section and changing
the `image`.

Before:

```yaml
services:
mysql:
image: mysql:8.1.0
healthcheck:
test: ["CMD", "mysqladmin" ,"ping", "-h", "localhost"]
interval: 10s
timeout: 5s
retries: 10
```
After:
```yaml
services:
mysql:
build:
context: https://github.com/pagemachine/docker-mysql-data.git#0.0.1
additional_contexts:
data: ./data
args:
MYSQL_IMAGE_VERSION: 8.1.0
MYSQL_DATABASE: database_name
MYSQL_USER: user_name
MYSQL_PASSWORD: user_password
MYSQL_ROOT_PASSWORD: root_password
healthcheck:
test: ["CMD", "mysqladmin" ,"ping", "-h", "localhost"]
interval: 10s
timeout: 5s
retries: 10
image: registry.example/my-project/mysql:latest
```
- `build.context` points to the URL of this GitHub repository, the fragment refers
to a release, `0.0.1` in this case, try to always use the
[latest release](https://github.com/pagemachine/docker-mysql-data/releases).
- `build.additional_contexts.data` must be defined and point to a local directory
with `*.sql`, `.sql.gz` or `.sh` files to use for populating the database on build.
- `build.args.MYSQL_IMAGE_VERSION` must be a valid tag of the `mysql` Docker image.
- `build.args.MYSQL_DATABASE` is the name of the database to create.
- `build.args.MYSQL_USER` is the username to set up for the database.
- `build.args.MYSQL_PASSWORD` is the password to set for the database user.
- `build.args.MYSQL_ROOT_PASSWORD` is the password of the `root` user.
- `image` should set a persistent image name/tag and a registry to push this image
to, this way other users will automatically pull the prebuilt image instead of
building it again.
7 changes: 7 additions & 0 deletions test/data/test_table.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CREATE TABLE test_table (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255)
);

INSERT INTO test_table VALUES (1, "foo");
INSERT INTO test_table VALUES (2, "bar");
20 changes: 20 additions & 0 deletions test/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
services:
mysql:
build:
context: ..
additional_contexts:
data: ./data
args:
MYSQL_IMAGE_VERSION: 8.1.0
MYSQL_DATABASE: db
MYSQL_USER: test
MYSQL_PASSWORD: test
MYSQL_ROOT_PASSWORD: root
environment:
MYSQL_PWD: root
healthcheck:
test: ["CMD", "mysqladmin" ,"ping", "-h", "localhost"]
interval: 10s
timeout: 5s
retries: 10
image: docker-mysql-data:${BUILD_IMAGE_TAG:?}
10 changes: 10 additions & 0 deletions test/expected.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE `test_table` (
`id` int NOT NULL AUTO_INCREMENT,
`title` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
/*!40101 SET character_set_client = @saved_cs_client */;
INSERT INTO `test_table` VALUES (1,'foo');
INSERT INTO `test_table` VALUES (2,'bar');

0 comments on commit ff1534e

Please sign in to comment.