How to check if a certain port/container/image is in use in Docker - bash

I'm creating a bash script to loop all my packages, and execute their tests.
testAll () {
dir=~/packages;
for f in "$dir"/*
do
cd $f;
sail down;
done;
for f in "$dir"/*
do
cd $f;
sail up;
if MYSQL is EXIST; then
artisan migrate:fresh --seed;
fi
test-all;
sail down;
done;
cd ..;
}
I need to check if mysql is exist, so I can execute "migrate" command. I could only find functions that display the container, but I need a boolean. So, how can I do that.
Thanks in advance.

The solution I came up is the following.
if [ ! -z $(docker ps --filter expose=3306 --format "{{.ID}}") ];
then
# code ...
fi

You can try this:
docker inspect --format='{{.State.Status}}' your_container
This will return docker status, running, up, and exited.

Related

replace upper case character with dash and lowercase

Hi I have this bash script with loop through every directories and build it
for d in * ;
do (cd ./"$d" && docker build -t "$d" .);
done
the problem is docker does not support uppercase for images name, so with the directory named repairService I want to rename the images to service-desk.
How do I achive that with bash scripting
Also I don't want to build it with docker compose so don't answer it with docker compose
An IF condition is added & then the variable value is reassigned.
#!/bin/sh
for d in *;
do
if [ "$d" = "repairService" ];
then d="service-desk";
fi;
echo "$d"; # Your docker operation here
done

Phpunit test suites are not running

I've an issue with
php vendor/bin/phpunit
I don't know why when I tried to run with codeship or bitbucket pipelines it seems wasn't run the test suites as follow:
php vendor/bin/phpunit
dir=$(cd "${0%[/\\]*}" > /dev/null; cd "../phpunit/phpunit" && pwd)
if [ -d /proc/cygdrive ] && [[ $(which php) == $(readlink -n /proc/cygdrive)/* ]]; then
# We are in Cgywin using Windows php, so the path must be translated
dir=$(cygpath -m "$dir");
fi
"${dir}/phpunit" "$#"
Perhaps someone could give me a thoughts ?
First recommendation would be to test out an ssh debug session, failing that you may want to reach out directly to support#codeship.com with your build url.

How to check for existing docker image in terminal?

I'm creating a new tag from an existing image. But sometimes the image is missing, and the command fails.
So I need to check if an docker image is existing at all before running the command:
$ docker tag source:anything target:something
But how do I check for existing docker image and how do I use an if-statement correctly in the terminal?
if [docker source:anything] docker tag source:anything target:something fi
this is the correct way to do it
if [[ "$(docker images -q myimage:mytag 2> /dev/null)" == "" ]]; then
// do something
fi
you could also use
[ ! -z $(docker images -q myimage:mytag) ] || echo "does not exist"

How to escape space in bash script from inline if?

I know that similar questions have been asked and answered before on stackoverflow (for example here and here) but so far I haven't been able to figure it out for my particular case.
I'm trying to create a script that adds the -v flag only if the variable something is equal to "true" (what I'm trying to do is to mount the current folder as a volume located at /src in the Docker container):
docker run --name image-name `if [ "${something}" == "true" ]; then echo "-v $PWD:/src"; fi` ....
The problem is that $PWD may contain spaces and if so my script won't work. I've also tried assigning "$PWD" to an intermediate variable but it still doesn't work:
temp="$PWD"
docker run --name image-name `if [ "${something}" == "true" ]; then echo "-v $temp:/src"; fi` ....
If I run:
docker run --name image-name -v "$PWD":/src ....
from plain bash (without using my script) then everything works.
Does anyone know how to solve this?
Use an array.
docker_args=()
if something; then
docker_args+=( -v "$PWD/src" )
fi
docker run --blah "${docker_args[#]}" …
Don't have arrays? Use set (in a function, so it doesn't affect outer scope).
Generally:
knacker() {
if something; then
set -- -v "$PWD:/src" "$#"
fi
crocker "$#"
}
knacker run --blah
But some commands (like docker, git, etc) need special treatment because of their two-part command structure.
slacker() {
local cmd="$1"
shift
if something; then
set -- -v "$PWD:/src" "$#"
fi
docker "$cmd" "$#"
}
slacker run --blah
Try this (using the array way):
declare -a cmd=()
cmd+=(docker run --name image-name)
if [ "${something}" = "true" ]
then
cmd+=(-v "$PWD:/src")
fi
"${cmd[#]}"

How to execute bash script after rsync

When I deploy on my dev server I use rsync. But after rsync I need to execute a .sh file for "after" deploy operations like clear cache...
Usually I do this via SSH, but if I deploy very often it's boring write:
ssh ...
write password
cd /var/www/myapp/web
./after_deploy.sh
There is a way to do this quickly? This is my rsync.sh:
#!/bin/bash
host=""
directory="/var/www/myapp/web"
password=""
usage(){
echo "Cant do rsync";
echo "Using:";
echo " $0 direct";
echo "Or:";
echo " $0 dry";
}
echo "Host: $host";
echo "Directory: $directory"
if [ $# -eq 1 ]; then
if [ "$1" == "dry" ]; then
echo "DRY-RUN mode";
rsync -CvzrltD --force --delete --exclude-from="app/config/rsync_exclude.txt" -e "sshpass -p '$password' ssh -p22" ./ $host:$directory --dry-run
elif [ "$1" == "direct" ]; then
echo "Normal mode";
rsync -CvzrltD --force --delete --exclude-from="app/config/rsync_exclude.txt" -e "sshpass -p '$password' ssh -p22" ./ $host:$directory
else
usage;
fi;
else
usage;
fi
If instead of using rsync over SSH, you can run an rsync daemon on the server. This allows you to use the pre-xfer exec and post-xfer exec options in /etc/rsyncd.conf to specify a command to be run before and/or after the transfer.
For example, in /etc/rsyncd.conf:
[myTransfers]
path = /path/to/stuff
auth users = username
secrets file = /path/to/rsync.secrets
pre-xfer exec = /usr/local/bin/someScript.sh
post-xfer exec = /usr/local/bin/someOtherscript.sh
You can then do the transfer from the client machine, and the relevant scripts will be run automatically on the server, for example:
rsync -av . username#hostname::myTransfers/
This approach may also be useful because environment variables relating to the transfer on the server can also be used by the scripts.
See https://linux.die.net/man/5/rsyncd.conf for more information.
You can add a command after the rsync command to execute it instead of starting a shell.
Add the following after the rsync command :
sshpass -p "$password" ssh $host "cd $dir && ./after_deploy.sh"

Resources