Skip to content

Instantly share code, notes, and snippets.

@potikanond
Last active August 26, 2018 15:13
Show Gist options
  • Save potikanond/2cfeb6d2bfbbf3bb5c1144a96884b14e to your computer and use it in GitHub Desktop.
Save potikanond/2cfeb6d2bfbbf3bb5c1144a96884b14e to your computer and use it in GitHub Desktop.
Ex2.5: Multiple virtual docker network

Ex2.5: Multiple virtual docker network

Create 'my_web_net' and 'my_api_net' virtual network

$ docker network create my_web_net $ docket network create my_api_net $ docker network ls sample output:

NETWORK ID          NAME                DRIVER              SCOPE
0a34ec3a4f58        bridge              bridge              local
5452c8e97607        host                host                local
5046472b737d        my_api_net          bridge              local
11ac61aabd27        my_web_net          bridge              local
d6a1ef964756        none                null                local

Create and connect MySQL, Apache containers to 'my_web_net'

Note: MySQL container does not need to expose (or publish) port 3306 to the host. We only need to expose Apache container.

$ docker container run -d --name my_mysql --network my_web_net -e MYSQL_RANDOM_ROOT_PASSWORD=yes mysql:5.7
$ docker container run -d --name my_apache --network my_web_net -p 80:80 httpd:alpine
$ docker network inspect my_web_net

Create and connect MongoDB, Nginx containers to 'my_api_net'

Note: MongoDB container does not need to expose (or publish) port 27017 to the host. We only need to expose Nginx container.

$ docker container run -d --name my_mongo --network my_api_net -e MYSQL_RANDOM_ROOT_PASSWORD=yes mysql:5.7
$ docker container run -d --name my_nginx --network my_api_net -p 8081:80 nginx:alpine
$ docker network inspect my_api_net

Execute 'sh' on 'my_apache'

$ docker container exec -it my_apache sh

within the 'my_apache', and try to ping 'my_mysql' and 'my_mongo'

/# ping my_mysql
/# ping my_mongo

Create temporary alpine container in 'my_web_net' just to ping 'my_mysql' and remove the container immediatly

$ docker container run -it --rm --network my_web_net alpine ping my_mysql

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment