Using GitLab-CI, I am attempting to echo a secret variable into a file inside a Docker container. The file exists and the user has permissions to write to the file yet I get a No such file or directory error.
$ /usr/bin/docker exec -t $CI_PROJECT_NAME ls -la /opt/application/conf/kubeadminaccount.yml
-rw-rw-r-- 1 nodeuser nodeuser 420 Aug 18 07:19 /opt/application/conf/kubeadminaccount.yml
$ /usr/bin/docker exec -t $CI_PROJECT_NAME whoami
nodeuser
$ /usr/bin/docker exec -t $CI_PROJECT_NAME echo $KUBE_ADMIN_ACCOUNT > /opt/application/conf/kubeadminaccount.yml
bash: line 69: /opt/application/conf/kubeadminaccount.yml: No such file or directory
Your redirection operator is working on host and not inside your container. Change below
$ /usr/bin/docker exec -t $CI_PROJECT_NAME echo $KUBE_ADMIN_ACCOUNT > /opt/application/conf/kubeadminaccount.yml
to
$ /usr/bin/docker exec -t $CI_PROJECT_NAME bash -c "echo $KUBE_ADMIN_ACCOUNT > /opt/application/conf/kubeadminaccount.yml"
Related
I am trying to run docker commands from a shell script in a remote server using ssh. I am able to execute the command manually on the remote server but when I run it through the script, it shows this error:
+ eloquent_shamir
+ echo eloquent_shamir
+ docker version
/tmp/nmon_jstat_scripts_to_container.sh: line 4: docker: command not found
copying nmon.sh in container:eloquent_shamir
+ echo 'copying nmon.sh in container:eloquent_shamir'
+ docker cp /tmp/nmon.sh eloquent_shamir:/opt/
/tmp/nmon_jstat_scripts_to_container.sh: line 6: docker: command not found
copying jstat.sh in container:eloquent_shamir
+ echo 'copying jstat.sh in container:eloquent_shamir'
+ docker cp /tmp/jstat.sh eloquent_shamir:/opt/
/tmp/nmon_jstat_scripts_to_container.sh: line 9: docker: command not found
+ docker exec -it eloquent_shamir /bin/bash -c 'chmod 777 /opt/nmon.sh'
/tmp/nmon_jstat_scripts_to_container.sh: line 12: docker: command not found
+ docker exec -it eloquent_shamir /bin/bash -c 'chmod 777 /opt/jstat.sh'
/tmp/nmon_jstat_scripts_to_container.sh: line 14: docker: command not found
Here is my script which I am running in the remote server:
#!/bin/bash -x
echo $1
docker version
echo "copying nmon.sh in container:$1"
docker cp /tmp/nmon.sh $1:/opt/
echo "copying jstat.sh in container:$1"
docker cp /tmp/jstat.sh $1:/opt/
docker exec -it $1 /bin/bash -c "chmod 777 /opt/nmon.sh"
docker exec -it $1 /bin/bash -c "chmod 777 /opt/jstat.sh"
echo "...................."
echo "creating NMON_OUTPUT dir in container:$1"
docker exec -it $1 /bin/bash -c "mkdir /opt/NMON_OUTPUT"
echo "creating JSTAT_OUTPUT dir in container:$1"
docker exec -it $1 /bin/bash -c "mkdir /opt/JSTAT_OUTPUT"
echo "...................."
sleep 10
Here "eloquent_shamir" is container name.
How do I make this work?
[alan#stormfather-0be642-default-1 ~]$ kubectl exec -it my-pod-0 -- bash -c "/bin/bash && cd /tmp"
[root#my-pod-0 /]# pwd
/
Change directory first and then sh into it.
kubectl exec -it my-pod-0 -- bash -c "cd /tmp && /bin/bash"
Mohsin Amjad's answer is both simple and correct, if you are getting the
..."bash": executable file not found in $PATH...
error, this just means the container inside the pod does not have bash installed, instead try sh or other shells. I.e. something like:
kubectl exec -it my-pod-0 -- sh -c "cd /tmp && echo $0 $SHELL"
my goal is to execute a script once on a permanently running pod in kubernetes. The pod is called busybox-<SOME_ID> and finds itself in the namespace default.Therefore, I wrote this script - called scan-one-pod.sh:
#!/bin/bash
export MASTER_IP=192.168.56.102
export SCRIPT_NAME=script.sh
export POD_NAMESPACE=default
export POD_NAME=busybox
echo "echo HALLO" | ssh ubuntu#$MASTER_IP
export POD_ID=$(kubectl get po | grep busybox | sed -n '1p'|awk '{print $1}')
kubectl cp $SCRIPT_NAME $POD_NAMESPACE/$POD_ID:.
kubectl exec $POD_ID -- chmod +x $SCRIPT_NAME
export CONTAINER_ID=$(kubectl describe pod busybox | grep 'Container ID' | sed -n '1p'|awk '{print $3}')
ssh -t ubuntu#$MASTER_IP "sudo docker exec -u root $CONTAINER_ID -- ./script.sh"
The referred script script.sh has the following content:
$ kubectl exec $POD_ID -- cat script.sh
#!/bin/bash
echo "test" >> test
cp test test-is-working
However, it is not possible to run the script on the pod:
the files test and test-is-working are not created
the script scan-one-pod.sh returns just EOF:
$ ./scan-one-pod.sh
Pseudo-terminal will not be allocated because stdin is not a terminal.
Welcome to Ubuntu 16.04.3 LTS (GNU/Linux 4.4.0-87-generic x86_64)
* Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/advantage
155 Software-Pakete können aktualisiert werden.
72 Aktualisierungen sind Sicherheitsaktualisierungen.
HALLO
[sudo] Passwort für ubuntu:
EOF
Connection to 192.168.56.102 closed.
If I execute the docker-command directly, remote on my kubernetes-controller, I get the same message of EOF:
ubuntu#controller:~$ export CONTAINER_ID=$(kubectl describe pod busybox | grep 'Container ID' | sed -n '1p'|awk '{print $3}')
ubuntu#controller:~$ sudo docker exec -u root $CONTAINER_ID ./script.sh
EOF
If I execute it from my local workstation via kubectl exec I get this error:
$ kubectl exec $POD_ID ./script.sh
rpc error: code = 13 desc = invalid header field value "oci runtime error: exec failed: container_linux.go:247: starting container process caused \"no such file or directory\"\n"
I don't know, which missing file they are referring to, but the script.sh-file is present and the busybox-pod seems to be running:
$ kubectl exec $POD_ID ls script.sh
script.sh
$ kubectl get po busybox-6bdf9b5bbc-4skds
NAME READY STATUS RESTARTS AGE
busybox-6bdf9b5bbc-4skds 1/1 Running 10 12d
Question: As far as I know, EOF means End-Of-File. End of which file would be important for me to know, and why is that a problem?
Thanks in advance, any help is appreciated :)
This works:
# echo 1 and exit:
$ docker run -i -t image /bin/bash -c "echo 1"
1
# exit
# echo 1 and return shell in docker container:
$ docker run -i -t image /bin/bash -c "echo 1; /bin/bash"
1
root#4c064f2554de:/#
Question: How could I source a file into the shell? (this does not work)
$ docker run -i -t image /bin/bash -c "source <(curl -Ls git.io/apeepg) && /bin/bash"
# content from http://git.io/apeepg is sourced and shell is returned
root#4c064f2554de:/#
In my case, I use RUN source command (which will run using /bin/bash) in a Dockerfile to install nvm for node.js
Here is an example.
FROM ubuntu:14.04
RUN rm /bin/sh && ln -s /bin/bash /bin/sh
...
...
RUN source ~/.nvm/nvm.sh && nvm install 0.11.14
I wanted something similar, and expanding a bit on your idea, came up with the following:
docker run -ti --rm ubuntu \
bash -c 'exec /bin/bash --rcfile /dev/fd/1001 \
1002<&0 \
<<<$(echo PS1=it_worked: ) \
1001<&0 \
0<&1002'
--rcfile /dev/fd/1001 will use that file descriptor's contents instead of .bashrc
1002<&0 saves stdin
<<<$(echo PS1=it_worked: ) puts PS1=it_worked: on stdin
1001<&0 moves this stdin to fd 1001, which we use as rcfile
0<&1002 restores the stdin that we saved initially
You can use .bashrc in interactive containers:
RUN curl -O git.io/apeepg.sh && \
echo 'source apeepg.sh' >> ~/.bashrc
Then just run as usual with docker run -it --rm some/image bash.
Note that this will only work with interactive containers.
I don't think you can do this, at least not right now. What you could do is modify your image, and add the file you want to source, like so:
FROM image
ADD my-file /my-file
RUN ["source", "/my-file", "&&", "/bin/bash"]
I'm writing a deployment script and have run in to a strange problem...
ian#baster:~$ sudo echo "Build: "$REVISION" - Deployed: "$(date +%Y-%m-%d) > /home/www/prod/www/revision.html
-bash: /home/www/prod/www/revision.html: Permission denied
but...
root#baster:~# echo "Build: "$REVISION" - Deployed: "$(date +%Y-%m-%d) > /home/www/prod/www/revision.html
root#baster:~# more /home/www/prod/www/revision.html
Build: - Deployed: 2011-01-28
then...
ian#baster:~$ sudo ls -l /home/www/prod/www
total 28
-rw-r--r-- 1 root root 31 2011-01-28 21:56 revision.html
ian#baster:~$ sudo more /home/www/prod/www/revision.html
Build: - Deployed: 2011-01-28
What's the deal?
The usual way to do that is with tee:
echo "foo" | sudo tee filename
You can suppress the output to the screen which tee does like this:
echo "foo" | sudo tee filename >/dev/null
The echo is run as root, but not the redirection. Run the redirection in a sudo subshell.