简体   繁体   English

使用 docker-compose 时无法从本地主机访问 docker 容器端口

[英]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.

相关问题 当network_mode:“bridge”(docker-compose)时,无法从localhost连接容器端口 - Unable to connect container port from localhost when network_mode: “bridge” (docker-compose) docker-compose:从docker容器访问localhost - docker-compose: access from docker container to localhost 如何从 docker-compose 中的容器访问 localhost 应用程序 - how to access a localhost app from a container in docker-compose Docker组成的网络,从容器访问主机端口 - Docker-compose networking, access host port from container 如何使用 docker-compose 而不是使用 docker bridge 从 docker 容器连接到 localhost:9092 - how to connect to localhost:9092 from docker container using docker-compose and not using docker bridge Docker组成了从容器到本地主机的映射端口 - Docker compose mapping port from container to localhost 如何使用docker-compose将localhost卷挂载到docker容器 - how to mount localhost volume to a docker container using docker-compose 使用docker-compose访问localhost和docker网络 - Access localhost and docker network using docker-compose 如何通过 docker-compose run 使用 Docker 访问 Sinatra 端口 - How to access Sinatra port using Docker with docker-compose run 使用Docker-compose,如何通过容器和主机中的相同端口访问容器数据库 - Using Docker-compose, how to access the container database via the same port in container and in host
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM