Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass file content to docker exec

I am learning with docker containers and I'd like to pass .sql file to database using docker exec. How can I do that? I am searching for about an hour now and found this:

cat file.sql | docker exec -it mariadb sh -c 'mysql -u<user> -p<pass>'

or this

docker exec -it mariadb sh -c 'mysql -u<user> -p<pass> "$(< /path/file.sql)"'

but neither of it worked. I think there is problem that I am passing it into sh -c and it tries to load that file from inside the container. How can I do it?

like image 952
TheSpixxyQ Avatar asked Dec 27 '18 21:12

TheSpixxyQ


People also ask

How do I use the Docker exec command?

To use the docker exec command, you will need a running Docker container. If you don’t already have a container, start a test container with the following docker run command: docker run -d --name container-name alpine watch "date >> /var/log/date.log" This command creates a new Docker container from the official alpine image.

How do I start a test container in Docker?

If you don’t already have a container, start a test container with the following docker run command: This command creates a new Docker container from the official alpine image. This is a popular Linux container image that uses Alpine Linux, a lightweight, minimal Linux distribution.

Why is there no such container error in Docker exec?

When using the docker exec command, you may encounter a few common errors: The No such container error means the specified container does not exist, and may indicate a misspelled container name. Use docker ps to list out your running containers and double-check the name.

How do I pass an environment variable to a docker container?

Sometimes you need to pass environment variables into a container along with the command to run. The -e flag lets you specify an environment variable: docker exec -e TEST= sammy container-name env This command sets the TEST environment variable to equal sammy, then runs the env command inside the container.


1 Answers

there's more than one way to do it, of course; most of your invocations are close, but if you execute docker with -t it will allocate a terminal for i/o and that will interfere with stream opearations.

My recent invocation from shell history was :

docker exec -i mysql mysql -t  < t.sql

in my case of course mysql is the running container name. You'll note that I do not pass -t to the docker exec - I do however pass it to mysql command line program that I exec on the docker host, so don't get confused there.

The shell that interprets that command and executes docker is also the one that opens t.sql and redirects that file descriptor to docker's stdin, so t.sql here is in the current working directory of the host shell, not the container shell.

That having been said, here's why yours didn't work. In the first place, as I said, the use of exec -it allocates a terminal that interferes with the stdin stream that the bash shell set up from cat. In the second place, you're really close, but path/file.sql wasn't on the docker image so I'm guessing that threw a 'no such file or directory' because file.sql is on the host, not the container, yet it's referenced within the -c parameter to the container's sh execution. Of course, that would also need -t to be removed; in neither case does a terminal need to be allocated (you already have one, and it will already be the stdout for the docker execution as it will inherit the shell's un-redirected stdout).

like image 84
Daniel Farrell Avatar answered Oct 09 '22 05:10

Daniel Farrell