[英]unable to access docker container port from localhost when using docker-compose
I have a very basic node/express app with a dockerfile and a docker-compose file.我有一个非常基本的节点/快速应用程序,带有一个 dockerfile 和一个 docker-compose 文件。 When I run the docker container using
当我使用运行 docker 容器时
docker run -p 3000:3000 service:0.0.1 npm run dev
I can go to localhost:3000 and see my service.我可以 go 到 localhost:3000 并查看我的服务。 However, when I do:
但是,当我这样做时:
docker-compose run server npm run dev
I can't see anything on localhost:3000, below are my files:我在 localhost:3000 上看不到任何内容,下面是我的文件:
Dockerfile Dockerfile
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build
EXPOSE 3000
docker-compose.yml docker-compose.yml
version: "3.7"
services:
server:
build: .
ports:
- "3000:3000"
image: service:0.0.1
environment:
- LOGLEVEL=debug
depends_on:
- db
db:
container_name: "website_service__db"
image: postgres
environment:
- POSTGRES_USER=user
- POSTGRES_PASSWORD=pass
- POSTGRES_DB=website_service
also, everything is working fine from the terminal/docker side - no errors and services are running fine, i just cant access the node endpoints此外,终端/码头方面一切正常——没有错误,服务运行良好,我只是无法访问节点端点
tl;dr tl;博士
docker-compose run --service-ports server npm run dev
// the part that changed is the new '--service-ports' argument
the issue was a missing docker-compose run argument --service-ports
:问题是缺少 docker-compose 运行参数
--service-ports
:
from these docs :来自这些文档:
The second difference is that the docker-compose run command does not create any of the ports specified in the service configuration .
第二个区别是 docker-compose 运行命令不会创建服务配置中指定的任何端口。 This prevents port collisions with already-open ports.
这可以防止端口与已经打开的端口发生冲突。 If you do want the service's ports to be created and mapped to the host , specify the --service-ports flag:
如果您确实希望创建服务的端口并将其映射到主机,请指定 --service-ports 标志:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.