运行但仍无法访问应用程序的Docker容器

时间:2020-11-07 15:03:18

标签: docker redis docker-compose dockerfile containers

我的docker compose文件似乎运行良好,并且在docker仪表板中,我看到适当的容器正在工作。当我转到http:// localhost:8080 /时,我看到的是一条错误消息,内容为:“此页面无效。本地主机未发送任何数据。 ERR_EMPTY_RESPONSE”。我不确定该如何解决。我看到某个地方用'0.0.0.0'替换了localhost,但是我不确定在这种情况下该怎么做,而且我觉得我已经看到它可以与localhost一起工作,所以我不确定该如何处理这些信息。我检查了其他来源,看看我做错了什么,没有找到好的解决方案。我很乐意就此事寻求任何建议!

docker compose

version: '3'
services:
  dev:
    image: filamentgraphql/filament-dev
    container_name: 'filament-dev-hot'
    ports: 
      - '8080:8080'
    volumes:
      - .:/usr/src/app
      - node_modules:/usr/src/app/node_modules
    command: npm run dev:hot
  redis-server:
    image: 'redis'
    volumes:
      - "./data/redis:/usr/src/app/data"
volumes:
  node_modules:

docker-dev

FROM node:12.16.1
RUN npm install --global webpack
WORKDIR /usr/src/app
COPY package*.json /usr/src/app/
RUN npm install
EXPOSE 4000

dockerfile

FROM node:12.16.1
WORKDIR /usr/src/app
COPY . /usr/src/app
RUN npm install
RUN npm run build
EXPOSE 4000
ENTRYPOINT node ./server/server.js

server.js

const express = require('express');
const { graphqlHTTP } = require('express-graphql');
const redis = require("redis");
require('dotenv').config();

// Mongo DB Setup
const mongoose = require('mongoose');
const MONGO_URI = process.env.MONGO_URI;
mongoose
  .connect(MONGO_URI, {
    useNewUrlParser: true,
    useUnifiedTopology: true,
    dbName: 'Filament',
  })
  .then(() => console.log('Connected to Mongo DB'))
  .catch((err) => console.log(err));

// Redis Setup
const client = redis.createClient({
  host: 'redis-server',
  port: 6379
})
client
  .on("error", err => console.log("Error " + err))
  .on('connect', () => console.log('Redis client connected'));

// Filament Setup
const filamentMiddlewareWrapper = require('../filament-library/filamentMiddleware')
const filamentMiddleware = filamentMiddlewareWrapper(client)

// Express setup
const app = express()
const PORT = 4000;
const schema = require('./schema');

app.use(express.json())
app.use('/filament', filamentMiddleware)
app.use(
  '/graphql',
  graphqlHTTP((req) => ({
    schema,
    graphiql: true,
    context: {
      req,
    }
  })),
);

app.listen(PORT, '0.0.0.0', () => console.log(`GraphQL server is on port: ${PORT}`));

package.json

  "scripts": {
    "test": "jest",
    "build": "webpack",
    "client": "webpack-dev-server --hot",
    "start": "nodemon server/server.js",
    "json-server": "json-server --watch fake-database.json",
    "dev": "npm run client & npm run start & npm run json-server & redis-server",
    "dev:hot": "npm run client & npm run start"

1 个答案:

答案 0 :(得分:0)

您打开和发布的端口不匹配。您需要在docker-compose.yml中修复.services.dev.ports.[0],(用Node将第二个端口更改为内部打开的端口):

version: '3'
services:
  dev:
    image: filamentgraphql/filament-dev
    container_name: 'filament-dev-hot'
    ports:
      # Fix port here
      - '8080:4000'
    volumes:
      - .:/usr/src/app
      - node_modules:/usr/src/app/node_modules
    command: npm run dev:hot
  redis-server:
    image: 'redis'
    volumes:
      - "./data/redis:/usr/src/app/data"
volumes:
  node_modules: