Is there a way to automate the creation of a file needed by a container in VS Code Remote-Containers before it runs?

This relates specifically to Remote-Containers with VS Code using a docker-compose file to define the services. One of those services in this case is MariaDB which takes an environment variable to specify the location of a file which contains the password for the DB user. The file must exist when the container starts or it will throw an error which could be difficult for a novice to diagnose. There is a default value which the file can contain but if I commit it to the public repository developers can no longer change it locally without the risk of the change being committed. When a developer builds a local environment from the repository, it’s okay to use the default value and they shouldn’t be required to create files in special locations before opening the container.

According to the documentation, inside devcontainer.json there is a property called initializeCommand which seems like it would be the place to run some code to copy a default file to the right location, but the developer could be running VS Code in Mac or on Windows. Is there a cross-platform way of making sure the environment is prepared before it gets launched? I want this automated so developers don’t have to remember or know to do this before getting started.

Basically, it should automatically test if secrets/sql-password.txt exists and copy secrets/sql-default-password.txt there only if the file is missing.

Any suggestions?

Source: Docker Questions

LEAVE A COMMENT