Docker Compose MySql Entry-Point

In this article, you will learn how to do MySql seeding using Docker Compose.

Image for post

Video Link

Let’s Start Creating Docker Seeding

Hope you are familiar with “Docker-Compose”

Folder structure

Project
├── docker-compose.yml (File)
├── init (Directory)
|-- 01.sql (File)
Image for post

Create a new file docker-compose.yml

Above file, we have created a MySql Docker container default Port No: 3306

As well as set environment variables such as user, password of MySql container.

Docker Entry-Point

if you check the entire file we have also created volumes that point to the `./init:` directory

volumes:
- ./init:/docker-entrypoint-initdb.d

The script inside `/init` is “01.sql” file which creates a new table student and inserts the records upon container startup

When you run the following command in the root directory for the project.

docker-compose up

then you will see in the console running /docker-entrypoint-initdb.d/01.sql script will be executed as the following screenshot

Image for post

that means your script executed successfully.

How to test

For testing, we used Adminer configured already in the docker-compose file.

Image for post

Try to run the following URL in the browser

http://localhost:8080

and enter username, password, Server, and Database as the following screenshot.

server: db
username: user
password: user
database: mydb
Image for post

Click on the login button. Then you will see there is one student table under Database: mydb as shown below

Image for post

If you click on student you will see the 3 records has been created as shown below

Image for post

Github

Image for post
If you liked this post, please support our work!

Thank you!

Written by

We teach & develop for startups

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store