iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
💡

How to fix port connection issues with Docker and Express

に公開

Conclusion

  • Check if the port specified in Docker matches the default port value of Express.

Environment

    "express": "~4.16.0",
docker-compose.yml
version: '3'
services:
  app:
    build: .
    tty: true
    ports:
      - 8000:8000
    volumes:
      - .:/app
      - /app/node_modules
FROM --platform=linux/x86_64 node:16.14.2-slim

RUN apt-get update && \
    apt-get install -y locales git procps vim tmux
RUN locale-gen ja_JP.UTF-8
RUN localedef -f UTF-8 -i ja_JP ja_JP
ENV LANG ja_JP.UTF-8
ENV TZ Asia/Tokyo
WORKDIR /app

What I did when it didn't work

Attempting to start Express

yarn start

For some reason, nothing is displayed even when accessing localhost:8000.

Checking the initial Express startup program

bin/www
#!/usr/bin/env node

/**
 * Module dependencies.
 */

var app = require('../app');
var debug = require('debug')('app:server');
var http = require('http');

/**
 * Get port from environment and store in Express.
 */

var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);
var port = normalizePort(process.env.PORT || '3000');

Looking at this, it seems that 3000 is used when process.env.PORT is not specified. Since the Docker port was set to 8000 this time, that was the problem.

Resolution

  • Set the Docker port to 3000
  • Specify the port when starting with PORT=8000 yarn start

This time, I used the second method.

PORT=8000 yarn start


Hooray! (It took me more than 3 hours)

Discussion