简体   繁体   中英

Docker app not able to access from host machine

I created a docker file and created a docker container using the file. But the docker webapp is not accessible from host machine. I am using WSL2 for running docker. My docker container is inside my ubuntu distro. When I start the container, it says app is running. But when I tried to access the webserver from chrome of my windows machine (host), it says unable to connect. My port mapping all looks fine.

Here is my docker-compose.yml file

version: '2'
services:
  db:
    image: mysql:5.7
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: password
      MYSQL_DATABASE: dp
      MYSQL_USER: app
      MYSQL_PASSWORD: appuser
      MYSQL_ALLOW_EMPTY_PASSWORD: 'no'
    ports:
      - "3307:3306"
    volumes:
      - "./db_dump:/docker-entrypoint-initdb.d/:ro"
    container_name: mysql_db
  app:
    build:
      context:
        ./
    volumes:
      - "./dp:/app"
    ports:
      - "3000:3000"
    depends_on: 
      - db
    links: 
      - db
    environment: 
      MYSQL_HOST: db
      MYSQL_ROOT_PASSWORD: password
      MYSQL_DATABASE: dp
      MYSQL_USER: app
      MYSQL_PASSWORD: appuser
    container_name: dp_app

Here is my Dockerfile

FROM node:14.15.5-alpine3.13
RUN mkdir -p /app
WORKDIR /app
#RUN adduser -S app
COPY ./dp/ .
RUN npm install
RUN npm install sharp --ignore-scripts=false
#RUN chown -R app /app
#USER app
EXPOSE 3000
CMD [ "npm", "run", "deploy" ]

I have a folder called dp in my root directory which is a next.js app. Also I have a folder called db_dump which have my initial db scripts for database setup.

Here is the scripts in package.json file inside dp folder

"scripts": {
    "dev": "next dev",
    "build": "next build",
    "start": "next start",
    "lint": "next lint",
    "deploy": "next build && next start"
  }

When I run docker-compose up , the image is built and next app built and started with message

ready - started server on 0.0.0.0:3000, url: http://localhost:3000 Since I binded 3000 port of container to 3000 port of my host machine, I should be able access the webserver from localhost:3000, right? But I am not able to access it. I even tried getting the docker container ip by docker inspect , but that also not working. I tried to connect mysql server using 3307 port, that also not working. There seems no error. But not able to connect.

I tried curl -I http://localhost:3000 from another wsl instance, it shows 200 response. But when I try the same from windows command prompt, it is not working. Not able to understand what is the issue.

If you're using Docker Machine on a Mac or Windows, use docker-machine ip MACHINE_VM to get the IP address of your Docker host. Then, open http://MACHINE_VM_IP:5000 in a browser.

Try this it should work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM