I want to write a shell script where I will enter any gcloud side environment and I will have multiple docker images that will be running. I will enter as a bash mode in any container and then run the gcloud connect MySQL command which is nothing but a Rubik. How can I enter these using a shell script? The script I have written is like a plain simple::
#!/bin/bash
echo "Executing cluster"
docker exec -ti cluster-name bash
echo "Starting a connection to gcloud mysql"
gcloud sql connect ql03-ee102-mysql --user=root --quiet.
The command is running till "docker exec -ti cluster-name bash" and entering the bash mode inside Rubik but stops running after that and do not run the next statements. If it does then I can create other DB scripts. How Do I achieve it? I am stuck. Any helo would be highly appreciated.
Related
I'm trying to set-up a Cloud SQL Auth proxy with a Cloud SQL for MySQL instance.
I'm following this guide but without success.
so I'm creating a new VM instance. Once it has been created I'm running the following command in the cloud shell
gcloud beta compute ssh --zone "europe-west2-c" "nameinstance" --tunnel-through-iap --project "my_project"
From what I understand this allow me to connect to my instance. Then I'm running the following command:
docker pull gcr.io/cloudsql-docker/gce-proxy:1.19.1
all good. then I'm kind of lost as when entering gcloud sql instances describe Cloud_SQL_instance_name I got the following error gcloud: command not found
and when entering docker run -d \\ -p 127.0.0.1:3306:3306 \\ gcr.io/cloudsql-docker/gce-proxy:1.19.1 /cloud_sql_proxy \\ -instances=sql_connection_name=tcp:0.0.0.0:3306 I have the following error docker: invalid reference format.
Ultimately, and if I'm right, I should be able to execute successfully the following command mysql -u USERNAME -p --host 127.0.0.1
Container Optimized OS (or COS) target is simple: run containers. That's all. All the other capacity of linux have been deactivated, to keep the kernel small, to reduce the attack surface, and to limit the point of failure (with third party binaries, like gcloud).
Thus, run container with docker (or docker-containerd).
# interactive mode
docker run -ti google/cloud-sdk:latest gcloud version
# Script mode
docker run --entrypoint gcloud google/cloud-sdk:latest version
It works as is in startup script. If you log into the VM and want to run these commands, add a sudo before to have the permission to run the binaries.
So, you will be able to run Cloud SQL proxy in a container, Gcloud in a container, and also MySQL client in a container. Forget the fact to run something without container (and docker run command). And think also to redirect the port correct when you run your containers.
I can run a docker exec command that calls mysql fine in PowerShell:
docker exec -it my_container_name mysql -e "source my-query.sql" -uroot -pMyRootPassword
The container is running, the sql file is there, and the query runs successfully.
But when I copy and paste the exact same command into a .bat batch file that contains some other docker commands, and run it (via PowerShell), I get an error message "Can't connect to local MySQL server through socket". If I put it into a separate batch file and run it on its own, it works fine.
There is a similar issue asked here - https://forums.docker.com/t/run-docker-exec-command-from-batch-file-in-windows-10/48163 - with no answer.
More Detail:
My complete batch file (simplified down) looks something like this:
set "container=my_container_name"
set "mysqlRootPassword=MyRootPassword"
docker stop %container%
docker rm %container%
docker run --name=%container% --env="MYSQL_ROOT_PASSWORD=%mysqlRootPassword%" --detach mariadb:10.5.6
docker exec -it %container% mysql -e "show databases;" -uroot -p%mysqlRootPassword%
When run this gives Error: ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/run/mysqld/mysqld.sock' (2). Everything above the exec runs OK.
Now if I run the exact same exec command in a separate batch file afterwards...
set "container=my_container_name"
set "mysqlRootPassword=MyRootPassword"
docker exec -it %container% mysql -e "show databases;" -uroot -p%mysqlRootPassword%
...Then it connects fine, the SQL command works fine, and shows the databases as expected.
Hm, it looks like it may be a timing issue. Might be trying to exec into the container before the container is fully spun up. Try adding a sleep statement before the exec.
PowerShellGuy is correct that it ended up being a timing issue. Apparently just because the docker container is up and running, it doesn't mean that the database is ready for connections. I solved the issue by adding a loop that waits and checks for a connection. There might be a better way to do this, but it solved my issue in the near term.
set "container=my_container_name"
set "mysqlRootPassword=MyRootPassword"
docker stop %container%
docker rm %container%
docker run --name=%container% --env="MYSQL_ROOT_PASSWORD=%mysqlRootPassword%" --detach mariadb:10.5.6
for %%i in (1 2 3 4 5 6 7) ^
do (
docker exec -it %container% mysql -e "show databases;" -uroot -p%mysqlRootPassword% && GOTO :connected || (
#echo "Trying again (%%i)..."
TIMEOUT 2
)
)
#echo "Timed out"
EXIT
:connected
docker exec -it %container% mysql -e "source my-query.sql" -uroot -p%mysqlRootPassword%
So I'm writing an install script and because I haven't been able to find a solid MySQL replacement on armfh (the db must be MySQL compatible), I'm using a community on that works, however it does not initiate the db as it should. it requires me to pass the following argument.
mysql -h"db" -u"root" -p"$MYSQL_ROOT_PASSWORD" "$MYSQL_DATABASE" < /docker-entrypoint-initdb.d/1_db.sql
From inside the docker. Problem is I want this to flow naturally as a smooth install script. I've tried using the following command to pass the document and get a password prompt:
docker exec -it db bash -c "mysql -h"db" -u"root" -p"$MYSQL_ROOT_PASSWORD" "$MYSQL_DATABASE" < /docker-entrypoint-initdb.d/1_db.sql"
If also tried:
docker exec -it db bash -c "mysql -h'db' -u'root' -p'$MYSQL_ROOT_PASSWORD' '$MYSQL_DATABASE' < /docker-entrypoint-initdb.d/1_db.sql
FwIW: I used the MYSQL_ROOT_PASSWORD=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 64 | head -n 1) to define the password. And if I manually enter the docker send command 1 (in quotations) the db initiates.
So to summarize my question: Is it possible to pass a command like the above to activate the 1_db.sql file from outside docker?
Any help would be amazing! Thanks in advance!
Is it possible to pass a command like the above to activate the
1_db.sql file from outside docker?
you can try something like
cat 1_db.sql | docker exec -i test bash -c 'mysql -uroot -p$MYSQL_ROOT_PASSWORD $MYSQL_DATABASE'
also, remember when you try exec bash -c "mysql -uroot -p$MYSQL_ROOT_PASSWORD"it will for MYSQL_ROOT_PASSWORD in the host, not inside container, use single quotes.
determined $MYSQL_ROOT_PASS to MySQL docker from outside docker? If so
how?
docker exec -i test bash -c 'echo mysql docker password is $MYSQL_ROOT_PASSWORD'
Goal: Currently trying to connect to a MySQL database on the host from the container.
Action: I have seen several answers to this problem, one being to run the container in host only mode with the --net=host option, so I can access the host using localhost/127.blah.
Result: Normally, when I execute the command
docker run -it [image_id] bash
I get a shell prompt with [user]#[container_id] (i.e., username#12345abcdef). However, when I execute the command
docker run --net=host -it [image_id] bash
it drops me to the same prompt as the host, but no longer in bold (specifically the prompt [user]#[container_id] is no longer in bold). I can tell I am in the container because of the file structure.
Question: What is happening? Am I one the right track to connecting to the database on the host? Why is it the same prompt and what is the significance of the change in font type from bold (host) to not bold (container)?
If it matters, the docker container is being created/ran inside a Vagrant machine.
The container id you normally see in your shell prompt is your container hostname. When you use --net=host, your container hostname is the same as your host's hostname. You have removed the normal network namespace that containers have. So the bash prompt with your container will look similar to the prompt on the host of you display the same fields on each. You can see the formatting of the prompt by checking the value of $PS1.
Edit: here's a comparison of different values of $PS1 from different base images (my host happens to be Debian):
$ echo $PS1
${debian_chroot:+($debian_chroot)}\u#\h:\w\$
$ docker run -it --rm debian:latest
root#4aca692dc29d:/# echo $PS1
${debian_chroot:+($debian_chroot)}\u#\h:\w\$
root#4aca692dc29d:/# exit
exit
$ docker run -it --rm ubuntu
root#b1eb8e51d672:/# echo $PS1
\[\e]0;\u#\h: \w\a\]${debian_chroot:+($debian_chroot)}\u#\h:\w\$
root#b1eb8e51d672:/# exit
exit
$ docker run -it --rm busybox
/ # echo $PS1
\w \$
/ # exit
$ docker run -it --rm centos
[root#abbfa9aa6968 /]# echo $PS1
[\u#\h \W]\$
[root#abbfa9aa6968 /]# exit
exit
start mysql container
$ docker run --name mysql -e MYSQL_ROOT_PASSWORD=my-secret-pw -d mysql
Connect to MySQL and manually create new database
$ docker run -it --link mysql:mysql --rm mysql sh -c 'exec mysql -h"$MYSQL_PORT_3306_TCP_ADDR" -P"$MYSQL_PORT_3306_TCP_PORT" -uroot -p"$MYSQL_ENV_MYSQL_ROOT_PASSWORD"'
link the database and start php container
$ docker run -d --link mysql:mysql --name myapp -v "$PWD":/var/www/html -p 80:80 php:5.6-apache
first question:
When access my php website: http://localhost/index.php, I got below error:
Fatal Error: Mysql is not supported in your PHP, recompile and try again.
Here is the configure command shows in phpinfo page, seems mysql module has been included in compile.
Configure Command './configure' '--with-config-file-path=/usr/local/etc/php' '--with-config-file-scan-dir=/usr/local/etc/php/conf.d' '--with-apxs2' '--disable-cgi' '--enable-mysqlnd' '--with-curl' '--with-openssl' '--with-readline' '--with-recode' '--with-zlib'
Are there anything missed in official php image?
second question:
When access http://localhost/info.php, I can see phpinfo page.
But it also shows database password in session "Environment":
MYSQL_ENV_MYSQL_ROOT_PASSWORD my-secret-pw
and in session "PHP Variables"
_ENV["MYSQL_ENV_MYSQL_ROOT_PASSWORD"] my-secret-pw
So how to hide the password in phpinfo()?
I assume you're trying to run phplist in a docker environment.
The message you're seeing (Fatal Error: Mysql is not supported in your PHP, recompile and try again.) is a phplist error message hardcoded in both the ./admin/mysql.inc and ./admin/mysqli.inc files.
This message is displayed upon check for the mysql_connect and mysqli_connect functions being present. You are seeing this message because the functions are not present in your environment.
You have to find out what package offers this functionality and either install it on your docker image, or build a new docker image with this support present.
The official PHP docker image is based on Debian, which offers the php5-mysql package. This is not present in the docker image, so you install this package using apt-get, then use docker-php-ext-install, docker-php-ext-configure and docker-php-ext-enable to enable the mysql and mysqli extensions.