Docker Community Forums

Share and learn in the Docker community.

How to copy script to container inside a docker-compose.yml

Hello All ,
My working environment is
windows10,
wsl2,
ubuntu 20.04 LTS
Docker Desktop set for wsl2

I wanted to run sql scripts right from a docker-compose.yml in order to populate a new postgres DB.None of the examples I found worked . Finally I solved my issue following an excellent post by Mr gforghetti(How to runnig sql script file - #3 by gforghetti).Since the topic has been closed for 3 years my question is the following ,

Is there now a way to include an initial sql script execution directly in a docker compose file or has it still to be a manual process as detailed by Mr gforghetti ?

Thanks

Hi :slight_smile:

Have you checked:
https://hub.docker.com/_/postgres

Under " Initialization scripts"

Thank you for the reference. I did not check it because l have never been able to understand an official documentation.However I just read it now and it seems the two test cases I followed were built on these premises. They did not work for me though. I can give you the 2 blog links tomorrow if you are interested…(I myself found them very interesting…if you are in this niche…)

Martin ,Good Morning…
Here are 2 excellent blog posts on the subject which all should work perfectly on Linux machines:
https://graspingtech.com/docker-compose-postgresql/

(As a new user I am not allowed a third link)

However they don’t on windows machines. My layman’s gut feeling tells me it has to do with file structure incompatibility between Linux and Windows file systems. In all cases the Linux execution windows mentions an issue with modification rights on the volumes path set in the compose file.

So I guess my question should be rephrased as: “have initialization scripts” ever worked on windows machines ? Just my two uneducated cents on this topic.

Olivier

Does mounting a windows folder to a container work ? Yes.
Eventhough that the first article uses a .sh script to create the database (and it should also work in your case) an normal .sql file is also supported.

What issues are you having when trying to map a local folder to the “/docker-entrypoint-initdb.d/” dir in the container?

Note that you in the docker application gui, need to give permission to the drive you want to use

Martin Thanks for the feed back !!

How do i do that ? ‘general settings’ ? I will then rerun the sql script test case and share the execution output if giving the permission does not solve it for me…

Olivier