Pipe stdin to docker exec on Windows - docker

I'm trying to import SQL data to a docker container. Using git bash / mintty:
> docker exec -it 79c5c16acca0 mysql -uusername -ppassword dbname
the input device is not a TTY. If you are using mintty, try prefixing the command with 'winpty'
> winpty docker exec -it 79c5c16acca0 mysql -uusername -ppassword dbname
stdin is not a tty
I have also tried Powershell:
> Get-Content powo.sql | docker exec -it 79c5c16acca0 mysql -uusername -ppassword dbname
the input device is not a TTY. If you are using mintty, try prefixing the command with 'winpty'
FWIW, running bash in the container works fine:
> docker exec -it 79c5c16acca0 bash
root#79c5c16acca0:/#

This will work if you remove -t.
$ docker run --rm --name example alpine sleep 100
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
49968909e3e4 alpine "sleep 100" 8 seconds ago Up 7 seconds example
$ echo "hi" | docker exec -it example cat
the input device is not a TTY. If you are using mintty, try prefixing the command with 'winpty'
$ echo "hi" | docker exec -i example cat
hi
This is true for bash, ps, and Git bash.
This answer is the best reference I can find on what -i and -t do (in language that I can understand).
From that, my guess is that you can't use -t when piping in content as -t implies the input is from a terminal device, but in this case the input is the pipe (which itself is from the terminal device).

Related

docker exec error "/data # ^[[2;9R" when try get shell or ash

when I'm executing a command in docker to get container bash, I'm faced with such a message and I can not enter any commands
docker exec -u 0 -t my_local_redis ash
and error is:
/data # ^[[2;9R
but when I run the below command it seems ok
docker exec -u 0 -t my_local_redis ls
Try using interactive terminal mode
docker exec -u 0 -it my_local_redis ash

how to run command in docker using a script outside thecontainer

in my .mk file I have done as following
.PHONY : Test
Test:
#echo Starting Docker container
docker container start XXX;
docker exec -it -w /home/TA/G/T/P XXX bash
docker exec /home/CO/CO_ANAlysis/bin/c-build --dir results--encoding UTF-8 make
and then I want to execute that it execute the first two and when docker container starts the command "docker exec /home/CO/CO_ANAlysis/bin/c-build --dir results--encoding UTF-8 make" doesn't execute
and if I run this out of docker:
docker exec -it -w /home/TA/G/T/P SDIO bash /home/CO/CO_ANAlysis/bin/c-build --dir results--encoding UTF-8 make
it shows me :
/home/CO/CO_ANAlysis/bin/c-build: /home/CO/CO_ANAlysis/bin/c-build: cannot execute binary filehow can I, in a mk file, run commands inside a docker?
Thanks
Problem Solved
docker container start XXX;
docker exec -it -w /home/TA/G/T/P XXX /home/CO/CO_ANAlysis/bin/c-build --dir results--encoding UTF-8 make

how to pass output of a command to another in shell script

I am trying to ssh into server, and into a docker container to run the service. however I am not able to store containerId into a variable to pass it to enter the container.
#!/bin/bash
ssh test_server << EOF
ls
sudo docker ps | grep 'tests_service_image' | colrm 13 # This command works
containerId=$(sudo docker ps | grep 'tests_service_image' | colrm 13) # This doesn't
sudo docker exec -i "$containerId" bash # Throws error since containerId is empty
./run.sh
EOF
exit
The problem is that you are doing variable/function expansions on your own side. You need to escape those so that those expansions happen on the server side.
#!/bin/sh
ssh test_server << EOF
containerId=\$(sudo docker ps | grep 'tests_service_image' | colrm 13)
sudo docker exec -i "\$containerId" bash
./run.sh
EOF
exit
Edit:
Pass it directly to docker exec command like so
sudo docker exec -i $(sudo docker ps | grep 'tests_service_image' | colrm 13) bash
Original Answer:
This is written assuming that the script execution is done post sshing into the server. but modified the answer to above based on the specific query
container ID is stored in variable containerId, you are getting the error Error: No such container: because you are passing a different variable $container instead of $containerId to docker exec command.

How to check mysql version inside mysql docker container

Is it possible to check it without getting inside the container?
something like
docker container exec -it <container name> ....
You can check the version using Docker inspect.
docker inspect mysql8 | grep MYSQL_MAJOR
#Or to print version plus major both
docker inspect mysql8 | grep MYSQL_
or without running the container
docker run -it mysql8 bash -c "printenv | grep MYSQL_VERSION"
Or if the container is already running
docker exec mysql bash -c "mysql -V"
You can use exec to run commands against a container:
docker exec -it <container_name> bash -c "mysql -V"
get Container ID:
docker ps
Ex image: enter image description here
Remote to docker & using bash in it:
docker exec -it <container_name> bash
Input command:
mysql -V

How to take Oracle-xe-11g backup from running Docker container

I am running oracle-xe-11g on rancher os. I want to take the data backup of my DB. When I tried with the command
docker exec -it $Container_Name /bin/bash
then I entered:
exp userid=username/password file=test.dmp
It is working fine, and it created the test.dump file.
But I want to run the command with the docker exec command itself. When I tried this command:
docker exec $Container_Name sh -C exp userid=username/password file=test.dmp
I am getting this error message: sh: 0: Can't open exp.
The problem is:
When running bash with -c switch it is not running as interactive or a login shell so bash won't read the same startup scripts. Anything set in /etc/profile, ~/.bash_profile, ~/.bash_login, or ~/.profile would be skipped.
Workaround:
run your container with following command:
sudo docker run -d --name Oracle-DB -p 49160:22 -p 1521:1521 -e ORACLE_ALLOW_REMOTE=true -e ORACLE_HOME=/u01/app/oracle/product/11.2.0/xe -e PATH=/u01/app/oracle/product/11.2.0/xe/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin -e ORACLE_SID=XE -e SHLVL=1 wnameless/oracle-xe-11g
What I'm doing is specifying the environment variables set in the container using docker.
Now for generating the backup file:
sudo docker exec -it e0e6a0d3e6a9 /bin/bash -c "exp userid=system/oracle file=/test.dmp"
Please note the file will be created inside the container, so you need to copy it to docker host via docker cp command
This is how I did it. Mount a volume to the container e.g. /share/backups/ then execute:
docker exec -it oracle /bin/bash -c "ORACLE_HOME=/u01/app/oracle/product/11.2.0/xe ORACLE_SID=XE /u01/app/oracle/product/11.2.0/xe/bin/exp userid=<username>/<password> owner=<owner> file=/share/backups/$(date +"%Y%m%d")_backup.dmp"

Resources