Docker: A primer for PHP Developers
Docker networks main benefits are (1) more isolation of containers from each other, (2) a more secured and (3) a more controlled environment for the application.
We look at the commands to (a) create a new network (b) add two containers to the network we have created and (c) run the two containers.
In the process, we look at useful commands which call the network containers using their hostnames.
$ docker network create dockerNetwork
# you can also list all networks: $ docker network ls
$ docker run --rm -d \
--name=app \
--network=network=dockerNetwork \
kbarut/app:latest
$ docker run -d -rm --name=mysql \
-e MYSQL_ROOT_PASSWORD=root \
-e MYSQL_DATABASE=DockerDB \
-e MYSQL_USER=user \
-e MYSQL_PASSWORD=1234 --network=dockerNetwork mysql:latest
# 1. To read off the two container hostnames, endpointIDs, IPv4adresses
$ docker network inspect dockerNetwork
# 2. To view the ip address of the 'app' named container hostname
getent hosts app # typical output: 172.18.0.2 app
# 3. To view the ip address of the 'mysql' named container hostname
getent hosts mysql # typical output: 172.18.0.3 mysql
# 4. To run a command within the 'mysql' named container and establish a
# terminal session inside the running container. Once inside the container,
# the next step is to connect to the MySQL server with the mysql command, your username and your password.
$ docker exec -it mysql /bin/bash