在docker-compose文件中映射端口时需要第二个值吗?

docker-compose.yml文件中, - "3000"- "3000:3000"之间有什么区别? 从文档看来,它们是一样的。 但是,第一种格式不会打开主机的端口。

 ports: - "3000" - "3000-3005" - "8000:8000" - "9090-9091:8080-8081" - "49100:22" - "127.0.0.1:8001:8001" - "127.0.0.1:5000-5010:5000-5010" 

我在网站上使用官方的Docker文档: 这里

从文档:

既可以指定两个端口(HOST:CONTAINER),也可以指定容器端口(将select一个随机的主机端口)。

如果只指定一个端口,则需要使用ps检查容器,或者检查主机端打开的端口。

 $ cat docker-compose.yml version: '2' services: web: image: nginx:latest volumes: - ./html:/usr/share/nginx/html ports: - "80" $ docker-compose up -d Creating network "test_default" with the default driver Creating test_web_1 $ docker-compose ps Name Command State Ports -------------------------------------------------------------------------- test_web_1 nginx -g daemon off; Up 443/tcp, 0.0.0.0:32769->80/tcp $ curl http://localhost:32769 <html><body>hello world</body></html>