2

I have docker-compose.yml file and I start a container with DB via docker-compose up -d db command.

I need to execute script by host machine that, briefly speaking, export dump to db in container.

So, now it looks like:

docker-compose up -d db
./script.sh

But I want to combine these two commands into one. My question is "Is it possible?"

I found out that Docker Compose doesn't support this feature. I know that I can create another script with these commands in it, but I want to leave only docker-compose up -d db

UPD: I would like to mention that I am using mcr.microsoft.com/mssql/server:2017-latest image

Also, have to say one more time that I need to execute script exactly on host machine

5
  • This heavily depends on the exact image used. The official postgres docker image, for example, executes all *.sql, *.sql.gz, and *.sh scripts in directory /docker-entrypoint-initdb.d. Commented Mar 20, 2022 at 12:43
  • @Turing85 Yes, I am going to mention that I am using mcr.microsoft.com/mssql/server:2017-latest image Commented Mar 20, 2022 at 12:47
  • My last comment will self-destruct shortly. Commented Mar 20, 2022 at 12:51
  • soooooo sh -c "docker-compose up -d db ; script.sh"? Commented Mar 20, 2022 at 13:05
  • This question is similar to: Docker run script in host on docker-compose up. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. Commented Oct 9, 2024 at 15:19

1 Answer 1

4

You can't use the Docker tools to execute commands on the host system. A general design point around Docker is that containers shouldn't be able to affect the host.

Nothing stops you from writing your own shell script that runs on the host and does the steps you need:

#!/bin/sh
docker-compose -d up
./wait-for.sh localhost 1433
./script.sh

(The wait-for.sh script is the same as described in the answers to Docker Compose wait for container X before starting Y that don't depend on Docker health checks.)

For your use case it may be possible to run the data importer in a separate container. A typical setup could look like this; note that the importer will run every time you run docker-compose up. You may want to actually build this into a separate image.

version: '3.8'
services:
  db: { same: as you have currently }
  importer:
    image: mcr.microsoft.com/mssql/server:2017-latest
    entrypoint: ./wait-for.sh db 1433 -- ./script.sh
    workdir: /import
    volumes: [.:/import]

The open-source database containers also generally support putting scripts in /docker-entrypoint-initdb.d that get executed the first time the container is launched, but the SQL Server image doesn't seem to support this; questions like How can I restore an SQL Server database when starting the Docker container? have a complicated setup to replicate this behavior.

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

1 Comment

Thanks a lot! Probably, the way of using another container is appropriate for me!

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.