Created
April 6, 2019 21:00
-
-
Save docwhat/95338d8cc349141d4096916dd875831c to your computer and use it in GitHub Desktop.
Example of using docker bridge networking
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
PS4='+\[\033[01;34m\](${BASH_SOURCE[0]}:${LINENO})\[\033[00m\]: ${FUNCNAME[0]:+\[\033[01;33m\]${FUNCNAME[ 0]}()\[\033[00m\]: }' | |
set -eu | |
# Tear Down previous run | |
{ | |
docker container rm -f db client || true | |
docker network rm majidi || true | |
} 2>/dev/null | |
# Setup network | |
docker network create majidi | |
db_password="sekret${RANDOM}" | |
function banner() { | |
echo | |
echo "================================================" | |
echo "$*" | |
echo | |
} | |
function is_db_up() { | |
echo 'SELECT 1;' | | |
docker run \ | |
"--name=client" \ | |
"--network=majidi" \ | |
"--rm" \ | |
"--interactive" \ | |
mariadb:latest \ | |
mysql \ | |
"--host=db" \ | |
"--user=root" \ | |
"--password=${db_password}" \ | |
mysql | |
} | |
banner "Using 'root' password: ${db_password}" | |
banner "Starting the container with the name and hostname 'db'..." | |
docker run \ | |
"--name=db" \ | |
"--network=majidi" \ | |
"--env=MYSQL_ROOT_PASSWORD=${db_password}" \ | |
"--detach" \ | |
mariadb:latest | |
while ! is_db_up; do | |
echo "waiting for 'db' to start..." | |
sleep 1 | |
done | |
banner "The database is started!" | |
banner "This is a mysql client into the MariaDB running in another client." | |
echo 'SELECT TABLE_NAME, TABLE_TYPE FROM TABLES ORDER BY TABLE_NAME LIMIT 10;' | | |
docker run \ | |
--name=client \ | |
--network=majidi \ | |
--rm \ | |
--interactive \ | |
mariadb:latest \ | |
mysql \ | |
"--host=db" \ | |
"--user=root" \ | |
"--password=${db_password}" \ | |
information_schema | |
# EOF |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment