docker run -d -P --name web training/webapp python app.py # -name means give the to-be-run container a name ‘web‘. -P means connect web to default network space bridge docker network ls docker run -itd --name=networktest ubuntu #container named networktest from image ubuntu has defaultly connected to bridge docker network inspect bridge docker network create -d bridge my-bridge-network #create a new network space "my-bridge-network" with network type "bridge", the other type is "overlay". docker network ls docker network inspect my-bridge-network docker run -d --network=my-bridge-network --name db training/webapp #run container "db" and add it to my-bridge-network docker inspect my-bridge-network docker inspect --format=‘{{json .NetworkSettings.Networks}}‘ db # check the networking of container db docker inspect --format=‘{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}‘ db # check the networking of container db docker run -d -P --name web training/webapp python app.py # start a container web in net space "bridge" docker inspect --format=‘{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}‘ web # check the networking of container web docker exec -it db bash #run container db with bash cmd #in the container, we use ping ip_of_web, it fails for web is in bridge while db is in my-bridge-network, eventhough both web and db containers are from the SAME image docker network connect my-bridge-network web #now connect web to my-bridge-network,Docker networking allows you to attach a container to as many networks as you like. docker exec -it db bash #run container db with bash cmd, use ping web. succeed cause web and db are in the same network
时间: 2024-10-05 05:56:41