官术网_书友最值得收藏!

Connecting a container to the network

Now we have our myNetwork ready, we can run the Docker container and attach it to the network. To launch containers, we are going to user the docker run --net=<NETWORK> option, where the <NETWORK> is the name of one of the default networks or the one you have created yourself. Let's run Apache Tomcat for example, which is an open source implementation of the Java Servlet and JavaServer pages technologies:

docker run -it --net=myNetwork tomcat  

It will take a while. The Docker engine will pull all of the Tomcat's image layers from the Docker Hub and then run the Tomcat container. There's another option to attach the network to the container, you can inform Docker that you would like the container to connect to the same network as other containers use. This way, instead of specifying a network explicitly, you just instruct Docker that you want two containers run on the same network. To do this, use the container: prefix, as in the following example:

docker run -it --net=bridge myTomcat
docker run -it --net=container:myTomcat myPostgreSQL  

In the previous example, we run the myTomcat image using the bridge network. The next command will run the myPostgreSQL image, using the same network as myTomcat uses. This is a very common scenario; your application will run on the same network as the database and this will allow them to communicate. Of course, the containers you launch into the same network must be run on the same Docker host. Each container in the network can directly communicate with other containers in the network. Though, the network itself isolates the containers from external networks, as seen in the following diagram:

If you run your containers in a bridge, isolated network, we need to instruct Docker on how to map the ports of our containers to the host's ports. We are going to do this now.

主站蜘蛛池模板: 岳西县| 和林格尔县| 襄汾县| 隆回县| 依安县| 贡山| 岳西县| 黎川县| 兴宁市| 吴堡县| 佛坪县| 通化县| 无锡市| 余干县| 旌德县| 长泰县| 灌云县| 磐石市| 红桥区| 新疆| 泸定县| 白水县| 叶城县| 卫辉市| 喀喇沁旗| 吴旗县| 旌德县| 阿图什市| 潮安县| 尚志市| 吴堡县| 乾安县| 湘潭市| 宣化县| 咸阳市| 从化市| 农安县| 永昌县| 略阳县| 沈阳市| 北碚区|