I am new to shell scripting, Recently started with basic. I have written code to check if i have cassandra nodes and it gives me yes or no if no then do execute some command. My problem is i already have started node1 and i am checking if node is already there then get the id of that container and start that container. But when i run it, it gets the id of the container and takes so long and never starts. If i start the container without shell commands it starts. But i want to indulge them in shell.
This is my code:
if sudo docker ps -a | grep -q 'node1';then
sudo docker inspect --format="{{.Id}}" node1
read num
sudo docker start num
elif sudo docker ps -a | grep -q 'node2';then
sudo docker inspect --formar="{{.Id}}" node2
read Idnode2
sudo docker start Idnode2
else
sudo docker run --name node1 -d -e CASSANDRA_BROADCAST_ADDRESS=192.168.1.xx -p 7000:7000 cassandra:2
fi
output:
./tet.sh
f1713abbee52ca465962ec53e97dde62058d37859005f77786db3e3eebe0086c
blinks forever after this
I am not getting why its blinking and not executing.
I solved it myself by using this command below
if sudo docker ps -a | grep -q 'node1';then
sudo docker inspect --format="{{.Id}}" node1
sudo docker start node1
elif sudo docker ps -a | grep -q 'node2';then
sudo docker inspect --formar="{{.Id}}" node2
read Idnode2
sudo docker start Idnode2
else
sudo docker run --name node1 -d -e CASSANDRA_BROADCAST_ADDRESS=192.168.1.xx -p 7000:7000 cassandra:2
fi
Related
I want to connect to a remote running Docker container directly with ssh. Normally I can
$ ssh -i privateKey user#host
$ docker ps #which will list all running containers
$ docker exec -it ***** bash deploy.sh # ***** is container id and this line run a deployment script
But I need to run this script from a Jenkins pipeline where I have only one chance. After many trying, I come up with this
$ ssh -tt -i ~/privateKey user#host docker exec -it $(docker ps | grep unique_text | cut -c1-10) /bin/bash deploy.sh
Which have not help my plight because it returns
"docker exec" requires at least 2 arguments.
Which actually mean the command is truncated here $(docker ps | grep ...
My Solution
sh 'ssh -tt -i $FILE -o StrictHostKeyChecking=no $USER#$HOST /bin/bash -c \'"docker exec -it $(docker ps | grep unique_text | cut -c1-10) bash start.sh"\''
$ ssh -tt -i ~/privateKey user#host docker exec -it $(docker ps | grep unique_text | cut -c1-10) /bin/bash deploy.sh
That will run the sub shell with the docker ps command on your local machine, not the remote one. You'll want to process that full command in a shell on the remote server:
$ ssh -tt -i ~/privateKey user#host /bin/sh -c "docker exec -it $(docker ps | grep unique_text | cut -c1-10) /bin/bash deploy.sh"
The best solution to this problem is to create a node in Jenkins
Step 1 − Go to the Manage Jenkins section and scroll down to the section of Manage Nodes.
Step 2 − Click on New Node
Step 3 − Give a name for the node, choose the Dumb slave option and click on Ok.
Step 4 − Enter the details of the node slave machine. In the below example, we are considering the slave machine to be a windows machine, hence the option of “Let Jenkins control this Windows slave as a Windows service” was chosen as the launch method. We also need to add the necessary details of the slave node such as the node name and the login credentials for the node machine. Click the Save button. The Labels for which the name is entered as “New_Slave” is what can be used to configure jobs to use this slave machine.
Once the above steps are completed, the new node machine will initially be in an offline state, but will come online if all the settings in the previous screen were entered correctly. One can at any time make the node slave machine as offline if required.
In my Jenkins pipeline
node("build_slave"){
sh 'docker exec -it $(docker ps | grep unique_text | cut -c1-10) bash deploy.sh'
}
I'm attempting to craft system admin bash tools for starting up a Docker image.
But such docker run keeps dying on me after its bash script exited.
The actual working bash script in question is:
#!/bin/sh
docker run \
--name publicnginx1 \
-v /var/www:/usr/share/nginx/html:ro \
-v /var/nginx/conf:/etc/nginx:ro \
--rm \
-p 80 \
-p 443 \
-d \
nginx
docker ps
Executing the simple script resulted in:
# ./docker-run-nginx.sh
743a6eaa33f435e3e0d211c4047bc9af4d4667dc31cd249e481850f40f848c83
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
743a6eaa33f4 nginx "nginx -g 'daemon of…" 1 second ago Up Less than a second 0.0.0.0:32778->80/tcp, 0.0.0.0:32777->443/tcp publicnginx1
And after that bash script gets completed, I executed 'docker ps'
# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
There is no Docker running.
What did I do wrong?
Try to run it without --rm.
You can see all container (including the one that already died using this command):
> docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS
743a6eaa33f4 nginx "nginx -g 'daemon of…" 1 second ago Exited (??) ??
^^^^^
You should be able to look at what is the exit code of the container. Using the container id, you can also look into it's log to understand better what is going on:
docker logs 743a6eaa33f4
If you still can't figure it out, you can start the container with tty to run bash, and try to run the command inside it.
docker run -it -v /var/www:/usr/share/nginx/html:ro -v /var/nginx/conf:/etc/nginx:ro --rm -p 80 -p 443 nginx bash
Is there any shortcut command to connect to a docker container without running docker exec -it 'container_id' bash every time?
Here is a shorter command line shortcut to:
Check if a container is running
If running, connect to a running container using docker exec -it <container> bash command:
Script docker-enter:
#!/bin/bash
name="${1?needs one argument}"
containerId=$(docker ps | awk -v app="$name:" '$2 ~ app{print $1}')
if [[ -n "$containerId" ]]; then
docker exec -it $containerId bash
else
echo "No docker container with name: $name is running"
fi
Then run it as:
docker-enter webapp
I'm using the following alias on OS X:
alias dex='function _dex(){ docker exec -i -t "$(basename $(pwd) | tr -d "[\-_]")_$1_1" /bin/bash -c "export TERM=xterm; exec bash" };_dex'
In the same directory as my docker-files, I run "dex php" to enter the PHP container.
If random id is complicated. Start container with name docker run --name test image and connect with its name docker exec -it test bash.
I have a shell script which runs as follows :
image_id=$(docker ps -a | grep postgres | awk -F' ' '{print $1}')
full_id=$(docker ps -a --no-trunc -q | grep $image_id)
docker exec -i -t $full_id bash
When I run this from the base linux OS, I expect to actually enter the postgres container which is a running container. But the issue is that the shell script hangs on 3rd line during ' docker exec' step.
My end goal is using the bash script, enter a running postgres container and run another bash script inside that container.
However the same command when I run it from command line, it works fine and gets me into the postgres container.
Please help, I have spent hours and hours to solve this but no progress.
Thanks again
Your setup is a bit more complex than it needs to be.
Docker ps can filter containers directly with the --filter= option
docker ps --no-trunc --quiet --filter="ancestor=postgres"
You can also --name containers when you run them which will be less fraught with danger than the script you are attempting
docker run --detach --name postgres_whatever postgres
docker exec -ti postgres_whatever bash
I'm not sure that your script is hanging as opposed to sitting there waiting for input. Try running a command directly
Using naming
exec_test.sh
#!/usr/bin/env bash
docker exec postgres_whatever echo "I have run the test"
When run
$ ./exec_test.sh
I have run the test
Without naming
exec_filter_test.sh
#!/usr/bin/env bash
id=$(docker ps --no-trunc --quiet --filter="ancestor=postgres")
[ -z "$id" ] && echo "no id" && exit 1
docker exec "${id}" echo "I have run the test"
When run
$ ./exec_filter_test.sh
I have run the test
I found very strange behaviour when I build and run docker container. I would like to have container with cassandra and ssh.
In my Dockerfile I've got:
RUN echo "deb http://www.apache.org/dist/cassandra/debian 20x main" | sudo tee -a /etc/apt/sources.list
RUN echo "deb-src http://www.apache.org/dist/cassandra/debian 20x main" | sudo tee -a /etc/apt/sources.list
RUN gpg --keyserver pgp.mit.edu --recv-keys 4BD736A82B5C1B00
RUN apt-key add ~/.gnupg/pubring.gpg
RUN apt-get update
RUN apt-get -y install cassandra
And then for ssh
RUN apt-get install -y openssh-server
RUN mkdir /var/run/sshd
RUN echo '{{ docker_ssh_user }}:{{docker_ssh_password}}' | chpasswd
EXPOSE 22
And I added start script to run everything I want:
USER root
ADD start start
RUN chmod 777 start
CMD ["sh" ,"start"]
And here comes problem. When I have start like this below:
#!/bin/bash
/usr/sbin/sshd -D
/usr/sbin/cassandra -f
SSH is working well. I can do ssh root#172.17.0.x. After I log in container I try to run cqlsh to ensure that cassandra is working. But cassandra is not started for some reason and I can't access cqlsh. I've also checked /var/log/cassandra/ but it was empty.
In second scenario I change my start script to this:
#!/bin/bash
/usr/sbin/sshd -D & /usr/sbin/cassandra/ -f
And I again try to connect ssh root#172.17.0.x and then when I run cqlsh inside container I have connection to cqlsh.
So I was thinking that ampersand & is doing some voodoo that all works well ?
Why I can't run bash staring script with one command below another?
Or I'm missing something else??
Thanks for reading && helping.
Thanks to my friend linux guru we found the reason of error.
/usr/sbin/sshd -D means that -D : When this option is specified, sshd will not detach and does not become a deamon. This allows easy monitoring of sshd
So in the first script sshd -D was blocking next command to run.
In second script I've got & which let sshd -D go background and then cassandra could start.
Finally I've got this version of script:
#!/bin/bash
/usr/sbin/sshd
/usr/sbin/cassandra -f