我想为 nginx 分配静态 IP,但根本不起作用。我不知道我哪里做错了。你能帮忙吗?
docker.compose.yml
version: '3'
services:
web:
container_name: nginx
image: nginx:latest
ports:
- "9000:80"
volumes:
- .:/var/www/html/resta
- ./docker/nginx.conf:/etc/nginx/conf.d/default.conf
networks:
default:
ipv4_address: 172.25.0.9
redis:
container_name: redis
image: redis
ports:
- "6379:6379"
mysql:
container_name: mysql
image: mysql
ports:
- "33061:3306"
- "3306:3306"
volumes:
- ./docker/mysql:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: password
php:
container_name: php
build: docker
image: php:fpm
volumes:
- .:/var/www/html/resta
networks:
default:
driver: bridge
ipam:
config:
- subnet: 172.25.0.0/24
它给了我这样的错误。
ERROR: for web user specified IP address is supported only when connecting to networks with user configured subnets
ERROR: Encountered errors while bringing up the project.
为什么会出现这样的错误?
答案1
可能你遇到了缩进问题。你的服务应该缩进为 的子级services:
,如下所示:
version: '3'
services:
web: [...]
redis: [...]
mysql: [...]
php: [...]
networks:
default:
driver: bridge
ipam:
config:
- subnet: 172.25.0.0/24
你的最终结果docker-compose.yml
应该是这样的:
version: '3'
services:
web:
container_name: nginx
image: nginx:latest
ports:
- "9000:80"
volumes:
- .:/var/www/html/resta
- ./docker/nginx.conf:/etc/nginx/conf.d/default.conf
networks:
default:
ipv4_address: 172.25.0.9
redis:
container_name: redis
image: redis
ports:
- "6379:6379"
mysql:
container_name: mysql
image: mysql
ports:
- "33061:3306"
- "3306:3306"
volumes:
- ./docker/mysql:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: password
php:
container_name: php
build: docker
image: php:fpm
volumes:
- .:/var/www/html/resta
networks:
default:
driver: bridge
ipam:
config:
- subnet: 172.25.0.0/24
检查此链接作为参考:Docker Compose 网络配置参考
答案2
您收到的错误:仅当连接到具有用户配置子网的网络时,才支持 Web 用户指定的 IP 地址。经验法则是,创建第一个容器的第一个 docker compose 文件定义未来容器要使用的网络子网。
以下是示例:容器 1:Docker Compose 文件的最后一部分
networks:
customnetwork:
ipv4_address: 172.20.0.10
networks:
customnetwork:
ipam:
config:
- subnet: 172.20.0.0/24
这将创建一个名为 nginxproxymanager_customnetwork 的网络。所有未来的容器都应加入此网络。用于加入的 docker compose 文件的最后一部分应如下所示:
networks:
nginxproxymanager_customnetwork:
ipv4_address: 172.20.0.11
restart: unless-stopped
volumes:
portainer_data:
networks:
nginxproxymanager_customnetwork:
external: true