0

EDIT:

It took me a day to realise it but this is actually an antipattern. Composer should not run docker scripts.

My solution is this:

 "scripts": {
    "php bin/console/doctrine.php orm:schema-tool:create",
    "php bin/console/doctrine.php orm:schema-tool:update --force",
    "php bin/console/fixtures.php"
  },

---

Is it possible to pass variables to composer scripts?

I have this respository that runs in docker. When I need to update fixtures or database schema or pretty much anything else I need to run docker exec I would like to make that at least semi-automatic. So that I can type something like composer run refresh_app and everything that needs to be done gets done.

With scripts like these:

  "scripts": {
    "db:create": "docker exec -i my_container_name bash -c \"cd /www; php bin/console/doctrine.php orm:schema-tool:create\"",
    "db:update": "docker exec -i my_container_name bash -c \"cd /www; php bin/console/doctrine.php orm:schema-tool:update --force\"",
    "db:fixtures": "docker exec -i my_container_name bash -c \"cd /www; php bin/console/fixtures.php\""
  },

I want to pass the my_container_name in variable. Possibly do something like this:

  "scripts": {
    "db:create": "docker exec -i $CONTAINER_ID bash -c \"cd /www; php bin/console/doctrine.php orm:schema-tool:create\"",
    "db:update": "docker exec -i $CONTAINER_ID bash -c \"cd /www; php bin/console/doctrine.php orm:schema-tool:update --force\"",
    "db:fixtures": "docker exec -i $CONTAINER_ID bash -c \"cd /www; php bin/console/fixtures.php\""
  },

2 Answers 2

1

Maybe use docker-compose with env variables? https://docs.docker.com/compose/environment-variables/

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for that link. It does not solve my problem though. I would like to have docker running in the background and communicate with it using composer / npm scripts. It works well hard-coded but for multiple instances of my image users will need to type the commands by hand.
0

Have these scripts in composer.json:

 "scripts": {
    "php bin/console/doctrine.php orm:schema-tool:create",
    "php bin/console/doctrine.php orm:schema-tool:update --force",
    "php bin/console/fixtures.php"
  },

Call them inside docker container bash.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.