Practice: Linking containers:
I'm using the official container for mariadb on docker hub and linking this container with the squash application container.
|
# Running mariadb 10.3 container in detached mode
|
|
docker run --name squash-mariadb -e MYSQL_ROOT_PASSWORD=<password> -d mariadb:10.3
|
|
# Now I link this container with the application container. Building on an existing python container I just open a text terminal and install mariadb-client to test the connection
|
|
docker run -it --link squash-mariadb python /bin/bash
|
|
root@6613ebc328bb:/# apt-get update
|
root@6613ebc328bb:/# apt-get install mysql-client
|
|
# Note that the host in the mysql command is the name of the linked container running mariadb server
|
|
root@6613ebc328bb:/# mysql -hsquash-mariadb -uroot -p
|
Enter password:
|
Welcome to the MySQL monitor. Commands end with ; or \g.
|
Your MySQL connection id is 11
|
Server version: 5.5.5-10.3.0-MariaDB-10.3.0+maria~jessie mariadb.org binary distribution
|
|
Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
|
|
Oracle is a registered trademark of Oracle Corporation and/or its
|
affiliates. Other names may be trademarks of their respective
|
owners.
|
|
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
|
|
mysql>
|
I think it is important to appreciate what is happening here. Linking takes the exposed port on the squash-mariadb container and make it directly accessible to the second container. It also changes /etc/hosts and populates a number of environment variables that are used to discover how to connect to it.
Note that these env variables are prefixed with the name of the first container.
|
$ docker run -it --link squash-mariadb python /bin/bash
|
root@b0d750aad389:/# env
|
SQUASH_MARIADB_ENV_MYSQL_ROOT_PASSWORD=admin
|
HOSTNAME=b0d750aad389
|
GPG_KEY=0D96DF4D4110E5C43FBFB17F2D347EA6AA65421D
|
TERM=xterm
|
SQUASH_MARIADB_PORT_3306_TCP=tcp://172.17.0.2:3306
|
SQUASH_MARIADB_ENV_MARIADB_MAJOR=10.3
|
SQUASH_MARIADB_ENV_no_proxy=*.local, 169.254/16
|
SQUASH_MARIADB_PORT=tcp://172.17.0.2:3306
|
SQUASH_MARIADB_ENV_GOSU_VERSION=1.7
|
PYTHON_VERSION=3.6.1
|
SQUASH_MARIADB_NAME=/relaxed_cori/squash-mariadb
|
PATH=/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
|
PWD=/
|
LANG=C.UTF-8
|
PYTHON_PIP_VERSION=9.0.1
|
SQUASH_MARIADB_ENV_MARIADB_VERSION=10.3.0+maria~jessie
|
SHLVL=1
|
HOME=/root
|
SQUASH_MARIADB_ENV_GPG_KEYS=199369E5404BD5FC7D2FE43BCBCB082A1BB943DB 430BDF5C56E7C94E848EE60C1C4CBDCDCD2EFD2A 4D1BB29D63D98E422B2113B19334A25F8507EFA5
|
no_proxy=*.local, 169.254/16
|
SQUASH_MARIADB_PORT_3306_TCP_PORT=3306
|
SQUASH_MARIADB_PORT_3306_TCP_PROTO=tcp
|
SQUASH_MARIADB_PORT_3306_TCP_ADDR=172.17.0.2
|
_=/usr/bin/env
|
Practice: Linking containers:
I'm using the official container for mariadb on docker hub and linking this container with the squash application container.
# Running mariadb 10.3 container in detached mode
docker run --name squash-mariadb -e MYSQL_ROOT_PASSWORD=<password> -d mariadb:10.3
# Now I link this container with the application container. Building on an existing python container I just open a text terminal and install mariadb-client to test the connection
docker run -it --link squash-mariadb python /bin/bash
root@6613ebc328bb:/# apt-get update
root@6613ebc328bb:/# apt-get install mysql-client
# Note that the host in the mysql command is the name of the linked container running mariadb server
root@6613ebc328bb:/# mysql -hsquash-mariadb -uroot -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 11
Server version: 5.5.5-10.3.0-MariaDB-10.3.0+maria~jessie mariadb.org binary distribution
Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
I think it is important to appreciate what is happening here. Linking takes the exposed port on the squash-mariadb container and make it directly accessible to the second container. It also changes /etc/hosts and populates a number of environment variables that are used to discover how to connect to it.
Note that these env variables are prefixed with the name of the first container.
$ docker run -it --link squash-mariadb python /bin/bash
root@b0d750aad389:/# env
SQUASH_MARIADB_ENV_MYSQL_ROOT_PASSWORD=admin
HOSTNAME=b0d750aad389
GPG_KEY=0D96DF4D4110E5C43FBFB17F2D347EA6AA65421D
TERM=xterm
SQUASH_MARIADB_PORT_3306_TCP=tcp://172.17.0.2:3306
SQUASH_MARIADB_ENV_MARIADB_MAJOR=10.3
SQUASH_MARIADB_ENV_no_proxy=*.local, 169.254/16
SQUASH_MARIADB_PORT=tcp://172.17.0.2:3306
SQUASH_MARIADB_ENV_GOSU_VERSION=1.7
PYTHON_VERSION=3.6.1
SQUASH_MARIADB_NAME=/relaxed_cori/squash-mariadb
PATH=/usr/local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
PWD=/
LANG=C.UTF-8
PYTHON_PIP_VERSION=9.0.1
SQUASH_MARIADB_ENV_MARIADB_VERSION=10.3.0+maria~jessie
SHLVL=1
HOME=/root
SQUASH_MARIADB_ENV_GPG_KEYS=199369E5404BD5FC7D2FE43BCBCB082A1BB943DB 430BDF5C56E7C94E848EE60C1C4CBDCDCD2EFD2A 4D1BB29D63D98E422B2113B19334A25F8507EFA5
no_proxy=*.local, 169.254/16
SQUASH_MARIADB_PORT_3306_TCP_PORT=3306
SQUASH_MARIADB_PORT_3306_TCP_PROTO=tcp
SQUASH_MARIADB_PORT_3306_TCP_ADDR=172.17.0.2
_=/usr/bin/env