1.docker-compose创建网桥
通过安装hello-world镜像来实现网桥的创建。
version: '3'
networks:
ubuntu-network:
name: ubuntu-network
ipam:
config:
- subnet: "192.168.3.0/24"
gateway: "192.168.3.1"
services:
hello-world:
image: hello-world
container_name: hello-world
networks:
- ubuntu-network
容器hello-world运行完成便会退出,不会保留容器,但网桥还保留。
test@test:~/network# docker compose up -d
[+] Running 1/2
⠼ Network ubuntu-network Created 0.5s
✔ Container hello-world Started
查询当前网络,可以看到已经创建了一个名为ubuntu-network的网桥。
test@test:~/network# docker network ls
NETWORK ID NAME DRIVER SCOPE
e66ea9b6eb57 bridge bridge local
8948e1300549 host host local
5f8cdd39b77a none null local
5f4d025d0ce2 ubuntu-network bridge local
查询当前容器,可以看到当前没有任何容器在运行
test@test:~/network# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2.删除网桥
使用docker-compose down删除网桥
test@test:~/network# docker compose down
[+] Running 2/2
✔ Container hello-world Removed 0.0s
✔ Network ubuntu-network Removed
查询网络,便会发现网桥已删除
test@test:~/network# docker network ls
NETWORK ID NAME DRIVER SCOPE
e66ea9b6eb57 bridge bridge local
8948e1300549 host host local
5f8cdd39b77a none null local
3.其他容器使用网桥
version: '3'
services:
nginx:
restart: always
container_name: nginx
image: nginx
ports:
- 80:80
- 443:443
networks:
- ubuntu-network
networks:
ubuntu-network:
name: ubuntu-network
external: true