Docker Community Forums

Share and learn in the Docker community.

Create a Docker Image of a sql file

build

(Raghu) #1

Hi,

It’s the first time I am working on docker and I am not sure of how I can create a Docker image for a
SQL script.

I believe we have to create a docker file for this although I am not sure of I can make it work.

Appreciate for any quick response.

Thanks in advance


(Bryce Ryan) #2

If you’re looking to dockerize a configuration file or script. you may want to look into volumes and configs. Both have their advantages and disadvantages, depending on what you’re trying to do with the script. Have a look at the docs I linked to, and you can make your own decision as to which is most appropriate.


(Raghu) #3

Its not a configuration file. I want to make a docker image for a DML/DDL Sql file. So it can be used in any machine by pulling the image.


(Martin Terp) #4

Hi :slight_smile:

So you want to create a database image/container, where the sql file is imported, so when a guy pulls the image, he will have a database with content?

is it mysql or ?


(Raghu) #5

Hi Martin,

Exactly. I have a sql file dump for which i am planning to create a docker image. So when anyone pulls that image, he should get the database and data content imported into his local machine.

Yeah i am using mysql for this.

Do you have any solution for it?

Appreciate for your help :slight_smile:


(Martin Terp) #6

Sorry for the delayed answer, i have been off the grid the whole weekend :slight_smile:

but this should work (havnt tested)

FROM mysql
ADD sqldump.sql /docker-entrypoint-initdb.d/

ENV MYSQL_ROOT_PASSWORD yourpassword

(Raghu) #7

Hi Martin,

Thanks for the reply. Now I am able to create a docker image for my sql script and able to execute it as well. My another requirement is like apart from this image, i have another image of a DDL script which should be executed after image i have created. Is there any way which can be done in a single yml or a docker file?

Both the sql’s will be pointing to same db, port.so i would like to execute on same container.


(Martin Terp) #8

Depends on how your script works.
If you have 2 images, and you need to have the database up BEFORE you execute the script container, you can checkout https://docs.docker.com/compose/startup-order/

But if its a simple script (depending on what it does) you can add it to the database image