0

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.

Zeth
  • 2,273
  • 4
  • 43
  • 91
  • You can use a static docker process name instead of `CONTAINER_ID` that changes every time. – anubhava Oct 20 '22 at 08:31
  • @anubhava - That sounds smart! Do you have a link to a place where I can read on how to do that? If I search for `how to use static docker process`, then I get a lot about how to assign static IP's to docker containers. – Zeth Oct 20 '22 at 08:39
  • Just use `--name=mysqldb` in `docker run` command – anubhava Oct 20 '22 at 09:21

1 Answers1

0

There is a docker-compose exec command that will automatically do this lookup for you.

importDb: ./dumps/existingDbDump.sql
        docker-compose exec mysql mysql -usomeuser -psomepassword local_db_name < $<

This would probably be my third-choice way to do the database load, though. If you have the mysql CLI tool on your host and your database container has published ports: then you can just run that directly, without doing anything Docker-specific.

importDb: ./dumps/existingDbDump.sql
        mysql -h127.0.0.1 -usomeuser -psomepassword local_db_name < $<

Or you can docker-compose run a temporary container to do the load:

importDb: ./dumps/existingDbDump.sql
        docker-compose run mysql \
            mysql -hmysql -usomeuser -psomepassword local_db_name < $<

If your application framework has a database migration system, you could similarly docker-compose run the migrations.

David Maze
  • 130,717
  • 29
  • 175
  • 215