I have a docker-compose.yaml
-file, that spins up a couple of containers: nginx
, php
, mysql
.
I'm trying to automate a setup-process, that imports a database to the mysql
-container, as part of a make-target.
The make-target looks like this:
startLocalEnv:
docker-compose up -d # Build containers
# A lot of other stuff happens here, that is omitted to keep it simple
importDb:
# THIS IS THE COMMAND I'M TRYING TO MAKE
docker exec -i CONTAINER_ID mysql -usomeuser -psomepassword local_db_name < ./dumps/existingDbDump.sql
How can I make this command: docker exec -i CONTAINER_ID mysql -usomeuser -psomepassword local_db_name < ./dumps/existingDbDump.sql
so that it doesn't become several steps, copying and pasting?
Currently
This is how it's done today:
Step 1: Do a docker ps
and copy the Container ID. Let's say: 41e8203b54ea
.
Step 2: Insert that into above-written command and run it. Example: docker exec -i 41e8203b54ea mysql -usomeuser -psomepassword local_db_name < ./dumps/existingDbDump.sql
It's not super painful, but it's something quite rudimentary, that I'm assuming (and hoping) can be made into one step fairly easily.
Solution attempt 1: Pipe the shit out of it!
I found this SO-article here: Get Docker container id from container name. Where I found this to output the Container ID: docker container ls | grep mysql | awk '{print $1}'
.
So I imagine, that fiddling around with this, that maybe I can get a one-liner, that runs this import.
But it seems excessive. And if I have another project that also has a container called mysql
(fairly possible!), that I have forgotten to stop, then this solution will target that.