-2

I am trying to create a .bat file that will back-up the database inside the container. After the first command (that is used to enter the container) the next one is ignored.

docker exec -it CONTAINER_NAME /bin/bash
cd /var/opt/mssql/data

Any ideas why? If I'm trying to manually write cd /var/opt/mssql/data in the opened cmd it works.

late1
  • 62
  • 9
  • 4
    `docker exec -it CONTAINER_NAME /bin/bash -c 'cd /var/opt/mssql/data'` Where `-c` issues commands internal to the docker itself. – Gerhard Jun 29 '22 at 06:25

1 Answers1

1

When running docker exec -it CONTAINER_NAME /bin/bash, you are opening a bash-shell INSIDE the docker container.

The next command, i.e. cd /var/opt/mssql/data, is only executed, if the previous command, docker exec -it CONTAINER_NAME /bin/bash has exited successfully, which means that the shell on the docker container has been closed/exited.

This means that cd /var/opt/mssql/data is then executed on the local machine and not inside the docker container.

To run a command inside the docker container, use the following command

docker exec -it CONTAINER_NAME /bin/bash -c "<command>"

Although it may be better to create a script inside the container during the build process or mount a script inside the docker container while starting the container and then simply call this script with the above mentioned command.

Mime
  • 1,142
  • 1
  • 9
  • 20