简体   繁体   中英

Identical Docker images with different containers and ports not accessible

I'm running Docker host on my Windows dev machine and have 2 identifcal images exposing different ports (3000, 3001). Using the following docker-compose I build and run the containers but the container on port 3001 isn't available via localhost or my IP address.

DockerFile

FROM mhart/alpine-node:8

# Create an app directory (in the Docker container)
RUN mkdir -p /testdirectory
WORKDIR /testdirectory

COPY package.json /testdirectory
RUN npm install --loglevel=warn
COPY . /testdirectory

EXPOSE 3000
CMD ["node", "index.js"]

DockerFile

FROM mhart/alpine-node:8

# Create an app directory (in the Docker container)
RUN mkdir -p /test2directory
WORKDIR /test2directory

COPY package.json /test2directory
RUN npm install --loglevel=warn
COPY . /test2directory

EXPOSE 3001
CMD ["node", "index.js"]

Docker-Compose file

version: '3'

services:
    testdirectory:
      container_name: testdirectory
      environment:
      - DEBUG=1
      - NODE_ENV=production
      - NODE_NAME=testdirectory
      - NODE_HOST=localhost
      - NODE_PORT=3000
      - DB_HOST=mongodb://mongo:27017/testdirectory
      - DB_PORT=27017
      build:
        context: ./test-directory
      volumes:
        - .:/usr/app/
        - /usr/app/node_modules
      ports:
        - "3000:3000"
      depends_on:
        - mongodb
      command: npm start
    test2directory:
      container_name: test2directory
      environment:
      - DEBUG=1
      - NODE_ENV=production
      - NODE_NAME=test2directory
      - NODE_HOST=localhost
      - NODE_PORT=3001
      - DB_HOST=mongodb://mongo:27017/test2directory
      - DB_PORT=27017
      build:
        context: ./test2-directory
      volumes:
        - .:/usr/app/
        - /usr/app/node_modules
      ports:
        - "3001:3001"
      depends_on:
        - mongodb
      command: npm start      
    mongodb:
      image: mongo:3.4.4
      container_name: mongo
      ports:
        - 27017:27017
      volumes: 
        - /data/db:/data/db

Is there any obvious I'm missing as when I run

docker container port test2directory

it returns

3001/tcp -> 0.0.0.0:3001

Found the problem! Setting the HOST to localhost in the container caused the problem and changing it to 0.0.0.0 got it working.

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