Piped command is failing inside docker container - docker

I'm executing this command from docker host which is finally not giving me any error on stdout. And completes successfully on prompt but doesn't executes what it is supposed to do inside container.
Can someone please help me identify what am i doing wrong?
docker exec -dt SPSSC /bin/bash -c "grep -ril 'LOCALIZATION_ENABLED="false"' /opt/tpa/confd/config/* | grep -v 'diameter' | xargs sed -i 's/LOCALIZATION_ENABLED="false"/LOCALIZATION_ENABLED="true"/g'"

Related

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.

Docker run exits immediately despite --detach, --interactive, --tty, and providing a command

On a Windows 10 host, Docker version 19.03.13, build 4484c46d9d, I tried running a docker image with all possible combinations of --tty, --interactive, and --detach, but none of them brings me to a bash prompt, always exiting immediately. /bin/bash is present in the image. The Dockerfile is from https://hub.docker.com/r/astj/centos5-vault/dockerfile
I ran:
docker run <switches> astj/centos5-vault /bin/bash
where <switches> had been exercised with the full set of -t, -i, -d combinations, namely:
-d, -i, -t, -it, -id, -td, -dit
In all cases, the container exits immediately.
If I change /bin/bash to ls, I can see a directory listing. But of course, the container exits immediately as expected. To troubleshoot, I experimented with the following commands, with these results:
+-------------------------------------------------------+----------------------------------------------------------------+
| Command | Output |
+-------------------------------------------------------+----------------------------------------------------------------+
| docker run astj/centos5-vault /bin/bash | None. Exits. |
| docker run -i astj/centos5-vault /bin/bash | None. Exits. |
| docker run -it astj/centos5-vault /bin/bash | None. Exits. |
| docker run -t astj/centos5-vault /bin/bash | None. Exits. |
| docker run -td astj/centos5-vault /bin/bash | Prints a container hash, then exits |
| docker run -id astj/centos5-vault /bin/bash | Prints a container hash, then exits |
| docker run -d astj/centos5-vault /bin/bash | Prints a container hash, then exits |
| docker run -dit astj/centos5-vault /bin/bash | Prints a container hash, then exits |
| docker run -it astj/centos5-vault ls -la /bin/bash | "-rwxr-xr-x 1 root root 768664 Jul 10 2013 /bin/bash". Exits. |
| docker run -it astj/centos5-vault /bin/bash --version | None. Exits. |
| docker run -it astj/centos5-vault /bin/bash --login | None. Exits. |
| docker run -it astj/centos5-vault /bin/uname -r | "4.19.128-microsoft-standard". Exits. |
| docker run astj/centos5-vault whoami | "root". Exits. |
+-------------------------------------------------------+----------------------------------------------------------------+
I tried to troubleshoot but docker logs <container> doesn't show a single line of log.
Does anyone know why the /bin/bash command still causes the container to exit immediately instead of bringing me to a bash prompt?
I ran into the above issue as well, seems the user above also posted this question separately, and found an answer there:
https://forums.docker.com/t/docker-run-exits-immediately-despite-detach-interactive-tty-and-providing-a-command/99444/5
For anyone finding their way here later, in my configuration mentioned above
this was an issue specific to Windows environments which was resolved
with a change in Windows 10 Build 18995.
Steps to resolve:
Create file : %userprofile%.wslconfig
Add this two lines :
[wsl2]
kernelCommandLine = vsyscall=emulate
Restart wsl
wsl --shutdown
Restart Docker Desktop
I found the answer here: https://github.com/microsoft/WSL/issues/4694#issuecomment-556152512
(Enable vsyscall=emulate in the kernel config to run older base images such as Centos 6)
This worked for me, it seems to only affect certain distributions, for me it was also centos 6

How can I grep a process in BusyBox using ps

I am getting below error in jenkins pipeline when I try to run ps -ef|grep process command:
ps: unrecognized option: p
BusyBox v1.27.2 (2017-12-12 10:41:50 GMT) multi-call binary.
Usage: ps [-o COL1,COL2=HEADER]
Show list of processes
-o COL1,COL2=HEADER Select columns for displayI have Jenkins version 2.135 and
I have BusyBox v1.27.2
Can some one tell me how can I avoid this error for ps -ef|grep process without installing the alpine image
You can use
docker exec -it container_name ps -ef | grep process
or you can do ps -ef | grep process into your container by running docker exec -it container_name /bin/bash

Running `bash` using docker exec with xargs command

I've been trying to execute bash on running docker container which has specific name as follows. --(1)
docker ps | grep somename | awk '{print $1 " bash"}' | xargs -I'{}' docker exec -it '{}'
but it didn't work and it shows a message like
"docker exec" requires at least 2 argument(s)
when I tried using command as follows --(2)
docker ps | grep somename | awk '{print $1 " bash"}' | xargs docker exec -it
it shows another error messages like
the input device is not a TTY
But when I tried using $() (sub shell) then it can be accomplished but I cannot understand why it does not work with the two codes (1)(2) above (using xargs)
Could any body explain why those happen?
I really appreciate any help you can provide in advance =)
EDIT 1:
I know how to accomplish my goal in other way like
docker exec -it $(docker ps | grep perf | awk '{print $1 " bash"}' )
But I'm just curious about why those codes are not working =)
First question
"docker exec" requires at least 2 argument(s)
In last pipe command, standard input of xargs is, for example, 42a9903486f2 bash. And you used xargs with -I (replace string) option.
So, docker recognizes that 42a9903486f2 bash is a first argument, without 2nd argument.
Below example perhaps is the what you expected.
docker ps | grep somename | awk '{print $1 " bash"}' | xargs bash -c 'docker exec -it $0 $1'
Second question
the input device is not a TTY
xargs excutes command on new child process. So you need to reopen stdin to child process for interactive communication. (MacOS: -o option)
docker ps | grep somename | awk '{print $1 " bash"}' | xargs -o docker exec -it
This worked for me:
sudo docker ps -q | xargs -I'{}' docker exec -t {} du -hs /tmp/
The exec command you run is something like this:
docker exec -it 'a1b2c3d4 bash'
And that is only one argument, not two. You need to remove the quotes around the argument to docker exec.
... | xargs -I'{}' docker exec -it {}
Then you will exec properly with two arguments.
docker exec -it a1b2c3d4 bash
------ ---
first arg ^ ^ second arg

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