Reputation: 1464
I am using Docker for Windows (Windows 10 is at 2004 so I have got WSL2) and I am trying to containerise a Nuxt application. The application runs well on my local system and after creating a Dockerfile and building it, I cannot get it to port forward onto my host system. Whereas, when trying the same when sample applications from https://github.com/BretFisher/docker-mastery-for-nodejs/tree/master/ultimate-node-dockerfile (the Dockerfile from the test
folder is supposed to be used), I can access the same.
If I exec
into my running container, I am able to get the output on running curl http://localhost:3000
so things are supposedly fine.
My Dockerfile looks like
FROM node:12.18.3-buster-slim
LABEL [email protected]
EXPOSE 3000
WORKDIR /app
RUN chown -R node:node /app
COPY --chown=node:node package*.json ./
ENV NODE_ENV=development
RUN apt-get update -qq && apt-get install -qy \
ca-certificates \
bzip2 \
curl \
libfontconfig \
--no-install-recommends
USER node
RUN npm config list
RUN npm ci \
&& npm cache clean --force
ENV PATH=/app/node_modules/.bin:$PATH
COPY --chown=node:node . .
RUN nuxt build
ENV NODE_ENV=production
CMD ["node", "server/index.js"]
I have even tried by removing all chown
s and removing USER node
to run it as root
but to no avail.
This is the output to docker ps -a
d727c8dd4d5c my-container:1.2.3 "docker-entrypoint.s…" 23 minutes ago Up 23 minutes 0.0.0.0:3000->3000/tcp inspiring_dhawan
c3a5aac8b79f sample-node-app "/tini -- node serve…" 23 minutes ago Up 23 minutes (unhealthy) 0.0.0.0:8080->8080/tcp tender_ardinghelli
The sample-node-app
from the above GitHub link works whereas my my-container
doesn't. What am I doing wrong?
EDIT: I have tried building and running the containers in an Ubuntu VM but I get the same result, so its not an issue with WSL or Windows but something is wrong with my Dockerfile.
Upvotes: 1
Views: 2402
Reputation: 169
By default, Nuxt development server host is 'localhost', but it is only accessible from within the host machine. So, to tell Nuxt to resolve a host address, which is accessible to connections outside of the host machine, e.g. Ubuntu Windows Subsystem for Linux 2 (WSL2), you must use host '0.0.0.0'
You can fix this by adding the following code to the nuxt.config.js
file :
export default {
server: {
port: 3000, // default : 3000
host: '0.0.0.0' // do not put localhost (only accessible from the host machine)
},
...
}
See Nuxt FAQ about Host Port for more information.
Upvotes: 9
Reputation: 1464
Found the solution! Nuxt changes the default address the server listens to from 0.0.0.0 to 127.0.0.1 and Docker can only port forward from 0.0.0.0.
Upvotes: 0