Cookie Consent by Free Privacy Policy Generator Update cookies preferences 📌 SQL Server and Flyway: a Docker approach

🏠 Team IT Security News

TSecurity.de ist eine Online-Plattform, die sich auf die Bereitstellung von Informationen,alle 15 Minuten neuste Nachrichten, Bildungsressourcen und Dienstleistungen rund um das Thema IT-Sicherheit spezialisiert hat.
Ob es sich um aktuelle Nachrichten, Fachartikel, Blogbeiträge, Webinare, Tutorials, oder Tipps & Tricks handelt, TSecurity.de bietet seinen Nutzern einen umfassenden Überblick über die wichtigsten Aspekte der IT-Sicherheit in einer sich ständig verändernden digitalen Welt.

16.12.2023 - TIP: Wer den Cookie Consent Banner akzeptiert, kann z.B. von Englisch nach Deutsch übersetzen, erst Englisch auswählen dann wieder Deutsch!

Google Android Playstore Download Button für Team IT Security



📚 SQL Server and Flyway: a Docker approach


💡 Newskategorie: Programmierung
🔗 Quelle: dev.to

For a personal project I wanted to realise a development environment with the following characteristics: easy to setup, easy to maintain and easy to share with my teammates. With this problem to solve, three tools immediately came to my mind: Docker, SQL Server and Flyway.

In this article I will describe how I glued them together in order to create a flexible development environment.

Let's dive into it!

TLTR

If you like to setup SQL Server and Flyway with Docker:

  1. Clone this repository;
  2. Create an .env file at root level. In it define the environmental variables that will be used in the docker-compose.yaml file. Note: The keys you use must match the ones used in the docker-compose.yaml file.
  3. Modify Flyway configuration file in flyway/conf folder with your parameters. Note: The database host name must match the SQL Server service name in the docker-compose.yaml file.
  4. Put your migration files in flyway/sql folder.
  5. Open a terminal window and execute docker compose up -d.

Project structure

The structure of the project is the following:

project-folder/
├─ db/
│  ├─ scripts/
│  │  ├─ entrypoint.sh
│  │  ├─ db-init.sh
│  │  ├─ init.sql
├─ flyway/
│  ├─ conf/
│  │  ├─ flyway.conf
│  ├─ sql/
│  │  ├─ <migrations goes here>
├─ docker-compose.yaml
  • db/scripts folder contains a set of shell and SQL scripts used in SQL Server container initialisation phase.
  • flyway folder contains the information Flyway needs: configuration and migration files.
  • docker-compose.yaml is the Docker compose file where SQL Server and Flyway containers are defined and linked together.

The compose file

The docker-compose.yaml file is where the development environment is formally described and it looks like the following code snippet:

version: '3'
services:
  db:
    image: mcr.microsoft.com/mssql/server:2022-latest
    container_name: sqlserver-2022-database 
    command: /bin/bash /scripts/entrypoint.sh
    volumes:
      - dbdata:/var/opt/mssql
      - ./db/scripts:/scripts
    environment:
      - ACCEPT_EULA=Y
      - MSSQL_SA_PASSWORD=${DB_PASSWORD}
    ports:
      - 1433:1433
  flyway:
    image: redgate/flyway
    container_name: flyway
    command: migrate -user=${DB_USER} -password=${DB_PASSWORD} -connectRetries=60
    volumes:
      - ./flyway/conf/:/flyway/conf
      - ./flyway/sql/:/flyway/sql
    depends_on:
      - db
volumes:
  dbdata:

There are two services defined in the file, one for the SQL Server container (db) and the other for Flyway container (flyway). They both use volumes to persist data and to copy files from the host machine into the containers themselves. In particular, SQL Server service copies scripts folder; while the Flyway one binds the conf and sql directories.

Until here, nothing special.

Before launching our services it must be considered that, in order to apply migrations, Flyway looks for an existing database. Unluckily, this condition is not satisfied the first time our containers are executed. Therefore, to fully automate the development environment, an initialisation procedure is needed.

The initialisation procedure

The desired order of operations is the following:

  1. Launch SQL Server container;
  2. Create the target database and schema (if they not exist);
  3. Launch Flyway container and apply migrations to the database.

Everything begins with the entrypoint.sh file, which is executed when the db service starts (see the command tag defined for it in the compose file).

It is defined below:

chmod +x /scripts/db-init.sh & \
/scripts/db-init.sh & \
/opt/mssql/bin/sqlservr

Here three things are happening. First of all, execution permission is granted to db-init.sh file (we will dive into it in a second). Then, db-init.sh is executed and eventually SQL Server is started.

It is important to notice that db-init.sh and SQL Server are executed in parallel.

At this point, it is time to initialise our database! And this is exactly what db-init.sh does.

sleep 30s

/opt/mssql-tools/bin/sqlcmd -S localhost -U sa -P ${MSSQL_SA_PASSWORD} -d master -i /scripts/init.sql

The script waits a fixed amount of time (30 seconds in this case) to be sure that SQL Server is up and running. Then, it runs the SQL script defined in init.sql file. The script is quite simple, indeed it checks if the database exists and creates it if it doesn't:

IF DB_ID('<YOUR_DB_NAME>') IS NULL
  CREATE DATABASE [YOUR_DB_NAME];
  GO
  USE [YOUR_DB_NAME];
  GO
  CREATE SCHEMA YOUR_SCHEMA;
  GO

And this is the initialisation procedure. Thanks to it when SQL Server container is launched for the first time everything is automatically setup.

At this point, our containers can finally be executed with the docker compose up command. Once they are up, if you look into the Flyway container's logs you can notice that at the beginning it fails to connect to the database: this is because SQL Server container is still starting. Despite this, after a while, the migration scripts are successfully executed: the initialisation procedure did its job. You can verify yourself by connecting to your database!

Conclusion

Putting SQL Server and Flyway together in a Docker friendly way is nothing special. At the same time, it is a delicate operation which requires SQL Server container to be ready for Flyway execution. With a bunch of scripts and a bit of synchronisation among containers, your development environment is ready for your awesome projects.

...



📌 SQL Server and Flyway: a Docker approach


📈 54.9 Punkte

📌 Secure Schema Migrations With Flyway and CockroachDB Dedicated


📈 29.9 Punkte

📌 Github Actions: how to deploy application to AWS ECS and migrate database with Flyway


📈 29.9 Punkte

📌 Flyway Migrations Naming Strategy in a Big Project


📈 28.37 Punkte

📌 How to enable DEBUG logs in Flyway?


📈 28.37 Punkte

📌 Database Migrations : Flyway for Spring Boot projects


📈 28.37 Punkte

📌 Flyway e Spring Boot em cenário de banco de dados não vazio.


📈 28.37 Punkte

📌 A better, faster approach to downloading docker images without docker-pull: Skopeo


📈 26.57 Punkte

📌 Docker users unhappy with latest forced login to download Docker and Docker Store images


📈 26.28 Punkte

📌 Mehrere Probleme in containerd, docker-runc, go1.11, go1.12, golang-github-docker-libnetwork, go und docker (SUSE)


📈 24.75 Punkte

📌 Security: Mehrere Probleme in containerd, docker-runc, go1.11, go1.12, golang-github-docker-libnetwork, go und docker (SUSE)


📈 24.75 Punkte

📌 Mehrere Probleme in containerd, docker-runc, golang-github-docker-libnetwork und docker (SUSE)


📈 24.75 Punkte

📌 Mangelnde Rechteprüfung in containerd, docker-runc, golang-github-docker-libnetwork und docker (SUSE)


📈 24.75 Punkte

📌 Preisgabe von Informationen in containerd, docker-runc, golang-github-docker-libnetwork und docker (SUSE)


📈 24.75 Punkte

📌 Preisgabe von Informationen in containerd, docker-runc, golang-github-docker-libnetwork und docker (SUSE)


📈 24.75 Punkte

📌 Denial of Service in containerd, docker-runc, golang-github-docker-libnetwork und docker (SUSE)


📈 24.75 Punkte

📌 Docker Stack Tutorial | Docker Stack Deploy Docker-Compose.yml


📈 24.75 Punkte

📌 Preisgabe von Informationen in containerd, docker-runc, golang-github-docker-libnetwork und docker (SUSE)


📈 24.75 Punkte

📌 Ausführen von Code mit höheren Privilegien in docker-runc, golang-github-docker-libnetwork, docker und containerd (SUSE)


📈 24.75 Punkte

📌 Mehrere Probleme in docker-runc, golang-github-docker-libnetwork, docker und containerd (SUSE)


📈 24.75 Punkte

📌 Security: Mehrere Probleme in docker-runc, golang-github-docker-libnetwork, docker und containerd (SUSE)


📈 24.75 Punkte

📌 Implementación de una Aplicación SpringBoot Utilizando Docker y Jenkins (Docker-outside-of-Docker - DooD)


📈 24.75 Punkte

📌 Cyber Risk Management: The Right Approach Is a Business-Oriented Approach


📈 20.14 Punkte

📌 Death to SQL Server! Long live SQL Server! How containerized SQL Server makes development easier


📈 20.04 Punkte

📌 Optimizing Docker Images with Multi-Stage Builds and Distroless Approach


📈 19.85 Punkte

📌 Docker einrichten auf Server oder Entwickler-PC | Docker-Tutorial 2/6


📈 19.56 Punkte

📌 Docker 101: How to install Docker on Ubuntu Server 22.04


📈 19.56 Punkte

📌 Automate Docker Image Builds and Push to Dual Container Registries (ECR and Docker Hub) In One Push Using GitHub Actions


📈 19.56 Punkte

📌 Meet Depot: A Developer Focused Startup with an AI-Powered Approach to Faster Docker Builds


📈 18.32 Punkte

📌 Canonical and Docker Partner to Distribute Docker Releases as Snaps on Ubuntu


📈 18.03 Punkte

📌 Docker 1.13 Officially Released, Docker for AWS and Azure Ready for Production


📈 18.03 Punkte

📌 Using Podman and Docker Compose - Podman 3.0 now supports Docker Compose


📈 18.03 Punkte











matomo