In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the knowledge of "how to use Compose variables to customize Docker network". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Using [Docker multi-node network] to create a virtual network and connecting the container to the virtual network can achieve the network topology needed in the application. Specifically, Bridge network can be used to create single-node network, and Overlay network can create multi-node network. By customizing the private network for the application in this way, it can provide absolute isolation for the container.
The goal of Docker Compose is to achieve a single-node network, by using-- x-networking to create an application-specific bridging network, and if the application requires multi-node deployment, you can use Docker Swarm clusters to create an overlay network.
What if a bridging network or overlay network already exists and you want to assign that network to a running container?
Docker 1.9 introduces the feature of variable substitution, which we can use to associate containers to a pre-created network.
Create a Docker bridged network
1. Create a network:
Sh
Docker network create-d bridge mynet
47d6225ffe56ddd1a8bc0d6abb0ffd8f8ac3eec2090ff243f8cd6f77c170751b =
two。 List the networks:
Sh
Docker network ls
NETWORK ID NAME DRIVER
Feb6e9567439 bridge bridge
29563a59abe8 none null
25ab737cd665 host host
47d6225ffe56 mynet bridge
Docker automatically creates three networks for each node:
| | Network name | function | |
|:-|:-|
| | bridge | the default network connected to the container, which is the docker0 network installed by default when all Docker are installed | |
| | none | Network stack customized by the container |
| | host | add a container to the host network stack, and the network configuration in the container will be the same as that of the host | |
As you can see above, the mynet network I just created is also on the list.
Use the docker inspect command to view the details of the mynet network:
Json
[
{
"Name": "mynet"
"Id": "47d6225ffe56ddd1a8bc0d6abb0ffd8f8ac3eec2090ff243f8cd6f77c170751b"
"Scope": "local"
"Driver": "bridge"
"IPAM": {
"Driver": "default"
"Config": [
{}
]
}
"Containers": {}
"Options": {}
}
]
As you can see from the Containers section, there is currently no container associated with it.
Docker Compose and Network
1. The new network above can be used in the new container, as long as you use the docker run-- net= command at run time. However, this article will use the Compose file to implement:
Json
Mycouchbase:
Container_name: "db"
Image: couchbase/server
Ports:
-8091 Phantom 8091
-8092PUR 8092
-8093PUR 8093
-11210 11210
Net: ${NETWORK}
Mywildfly:
Image: arungupta/wildfly-admin
Environment:
-COUCHBASE_URI=db
Ports:
-8080,8080
-9990R 9990
Net: ${NETWORK}
Notice here that net has specified to use a custom network.
two。 Use the newly created network to start the application:
Sh
NETWORK=mynet docker-compose up-d
Check the network details again:
Json
Docker network inspect mynet
[
{
"Name": "mynet"
"Id": "47d6225ffe56ddd1a8bc0d6abb0ffd8f8ac3eec2090ff243f8cd6f77c170751b"
"Scope": "local"
"Driver": "bridge"
"IPAM": {
"Driver": "default"
"Config": [
{}
]
}
"Containers": {
"300bebe6c3e0350ebf9b9d3746eb3a7b49444e14c00314770627a9f101442639": {
"EndpointID": "82a3e2d7cd4f1bb03c9ef52bb6abf284942d7e9fcac89fe3700b0e0c4ed2654f"
"MacAddress": "02:42:ac:14:00:03"
"IPv4Address": "172.20.0.3plus 16"
"IPv6Address":
}
"4fdae4eb919f0934422513227fe541255557dd9e8b3317374685927e7f427249": {
"EndpointID": "937605d716d144b55288d70817d611da5fb0f87e3aedd6b5074fca07f82c3953"
"MacAddress": "02:42:ac:14:00:02"
"IPv4Address": "172.20.0.2 prime 16"
"IPv6Address":
}
}
"Options": {}
}
]
You can see that there are already two containers associated with this network.
Use docker ps to view the container ID:
Sh
# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
300bebe6c3e0 couchbase/server "/ entrypoint.sh couch" 2 minutes ago Up 2 minutes 0.0.0.0 minutes ago Up 8091-8093-> 8091-8093/tcp, 11207/tcp, 11211/tcp, 0.0.0.0 minutes ago Up 11210-> 11210/tcp, 18091-18092/tcp db
4fdae4eb919f arungupta/wildfly-admin "/ opt/jboss/wildfly/b" 2 minutes ago Up 2 minutes 0.0.0.0 minutes ago Up 8080-> 8080/tcp, 0.0.0.0 minutes ago Up 9990-> 9990/tcp wildflycouchbasejavaee7network_mywildfly_1
View the network settings for one of the containers:
Sh
Docker inspect-f'{{.HostConfig.NetworkMode}} '300
Mynet
View the detailed network information of this container:
Sh
Docker inspect-f'{{.NetworkSettings.Networks.mynet}} '300
{82a3e2d7cd4f1bb03c9ef52bb6abf284942d7e9fcac89fe3700b0e0c4ed2654f 172.20.0.1 172.20.0.3 16 0 02:42:ac:14:00:03}
More details of this container can be seen in docker inspect, and the relevant parts are here:
Json
"Networks": {
"mynet": {
"EndpointID": "82a3e2d7cd4f1bb03c9ef52bb6abf284942d7e9fcac89fe3700b0e0c4ed2654f"
"Gateway": "172.20.0.1"
"IPAddress": "172.20.0.3"
"IPPrefixLen": 16
"IPv6Gateway":
"GlobalIPv6Address":
"GlobalIPv6PrefixLen": 0
"MacAddress": "02:42:ac:14:00:03"
}
}
This is the end of the content of "how to customize the Docker network using Compose variables". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.