'Docker and Node .mjs files

I have an express application with all the JS files using the *.mjs extension.

So, to start the server I do node index.mjs and it works as expected. Now I'm trying to containerize the app. I have this basic Dockerfile

FROM mhart/alpine-node:14

WORKDIR /app

COPY package.json /app

RUN npm install

COPY . /app

CMD node index.mjs

EXPOSE 80

After building (with no errors) and tagging I try to run my application (docker run my-app:latest) it breaks the line in the console but I don't see the console logs of my server.

If I try to hit localhost at port 80, it doesn't work.

I check the containers with docker container ls and I see the container

CONTAINER ID   IMAGE                     COMMAND                  CREATED         STATUS         PORTS     NAMES
ce7ca2a0db96   my-app:latest   "/bin/sh -c 'node in…"   6 minutes ago   Up 6 minutes   80/tcp    clever_bhabha

If I look for logs, nothing.

Does anyone have this issue? Could it be related to .mjs files? If so, is there a way to use them in Docker? Thanks



Solution 1:[1]

I think you need to expose a port different to 80 locally. You should try

docker run -p 8080:80 my-app

Then in localhost:8080 you should reach your app.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 user1617125