无法访问此站点 docker-撰写

This site can’t be reached docker-compose

我正在尝试将 docker-compose 用于 react-typescript 应用程序 webpack-dev-server 下面是我的 Dockerfile

FROM node:lts-slim

RUN mkdir -p /usr/src/app

WORKDIR /usr/src/app

EXPOSE 3000

CMD [ "npm", "start" ]

"start": "webpack-dev-server --port 3000"package.json

docker-compose.yml

version: "3"
services:
    frontend:
        container_name: awesome_web
        build:
            context: ./client
            dockerfile: Dockerfile
        image: webpack
        ports:
            - "3000:3000"
        volumes:
            - ./client:/usr/src/app

我执行命令docker-compose up --build based on logs application compiled successful

docker的输出ps

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                    NAMES
c88198ba996c        webpack             "docker-entrypoint.s…"   21 seconds ago      Up 20 seconds       0.0.0.0:3000->3000/tcp   awesome_web

但是当我尝试访问 localhost:3000 时出现错误 无法访问此站点

我是 docker 的新手,关注在线博客,但我不明白为什么我无法访问网站?

尝试将您的 start 脚本更改为:

webpack-dev-server --host 0.0.0.0 --port 3000

你的 Dockerfile 到:

FROM node:lts-slim

RUN mkdir -p /usr/src/app

WORKDIR /usr/src/app
COPY . /usr/src/app/

EXPOSE 3000

CMD [ "npm", "start" ]

注意:我强烈建议不要 运行 您的容器作为 root。您应该始终使用命令 USER ....

降级您的用户

安全

根据this Snyk's report, you are using a vulnerable base image in addition to running it as root. I highly recommend you use this image instead. Furthermore, you should run your image as a non-root user

FROM node:13.8.0-alpine

# don't run as root
RUN addgroup -S app_group && adduser -S -G app_group app_user

RUN mkdir -p /usr/src/app && chown app_user /usr/src/app

WORKDIR /usr/src/app
COPY --chown=app_user:app_group . /usr/src/app/

EXPOSE 3000

USER node
CMD [ "npm", "start" ]
USER app_user

如果您正在使用 docker 工具包,那么每个容器都有虚拟机 运行 检查您当前的容器是 运行 在哪个虚拟机上获取 vm ip 并使用此浏览 url vm-ip:3000 或者如果你只是 运行 docker 然后尝试打开 docker 0.0.0.0:3000 .