This site can’t be reached docker-compose

依然范特西╮ 提交于 2021-02-11 02:02:21

问题


I am trying to use docker-compose for react-typescript application with webpack-dev-server below is my 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" this package.json line

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

I executed command docker-compose up --build based on logs application compiled successfully

output of 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

but when I am trying to access localhost:3000 I am getting error This site can’t be reached

I am new to docker, following online blogs but I am not able to get why am I not able reach site?


回答1:


Try changing your start script to:

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

And your Dockerfile to:

FROM node:lts-slim

RUN mkdir -p /usr/src/app

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

EXPOSE 3000

CMD [ "npm", "start" ]

Note: I highly advise against running your containers as root. You should always downgrade your user with the command USER ....

Security

According to 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



回答2:


If you are using docker toolkit then there is virtual machine running for each container check your current container is running on which virtual machine get vm ip and browse with this url vm-ip:3000 Or if you are just running docker then try to open docker 0.0.0.0:3000 .



来源:https://stackoverflow.com/questions/60361324/this-site-can-t-be-reached-docker-compose

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!