skip to Main Content

I’ve got a Svelte (with Sveltekit) app which I just containerized. It builds correctly and there are no errors when running, but I cannot access it from the browser. I tried accessing http://localhost:5050/ and also http://0.0.0.0:5050/ but I’m getting:

This page isn’t working
localhost didn’t send any data.
ERR_EMPTY_RESPONSE

This is the Dockerfile I’m using:

# Our Node base image
FROM node:19-alpine

# Set the Node environment to development to ensure all packages are installed
ENV NODE_ENV development

# Change our current working directory
WORKDIR /app

# Copy over `package.json` and lock files to optimize the build process
COPY  package.json package-lock.json ./
# Install Node modules
RUN npm install

# Copy over rest of the project files
COPY . .

RUN npm run build

# Expose port
ENV PORT 5050
EXPOSE 5050

# Run `yarn dev` and set the host to 0.0.0.0 so we can access the web app from outside
CMD ["npm", "run", "dev"]

This is how I’m building it:
docker build -t sveltekit:node --network=host .

And this is how I’m running it:
docker run -d -p 5050:5050 --name sveltekit-app sveltekit:node

And this is the output of running docker ps:

a9e241b09fd3 

IMAGE            
sveltekit:node

COMMAND 
"docker-entrypoint.s…"                 

CREATED      
About a minute ago        

STATUS
Up About a minute              

PORTS          
0.0.0.0:5050->5050/tcp          

NAMES
sveltekit-app

What am I missing?

UPDATE

Container Logs

2022-11-30 19:09:18 
2022-11-30 19:09:18 > [email protected] start
2022-11-30 19:09:18 > export PORT=8080 && node ./build
2022-11-30 19:09:18 
2022-11-30 19:09:18 Listening on 0.0.0.0:8080

why it is listening to port 8080? I updated my package.json to be:

"dev": "vite dev --port=5050"

meaning that I’m enforcing port 5050, isn’t that right?

3

Answers


  1. Your container has no web server running to serve your files.
    As you are just wanting to serve static files, node http-server would probably do.

    Try running this somewhere in your dockerfile..
    npm install --global http-server && http-server ./app -p 5050

    EDIT:
    Sveltekit seems to run @sveltejs/adapter-node to serve files.

    Login or Signup to reply.
  2. Are you sure that sveltekit listens on port 5050?

    Because when I start it up with npm run dev (vite dev) it usually takes port 5173 and if it is already used it counts 1 up until it reaches a free port.

    Add in the package.json at the dev command a –port=5050.

    Full string:

    "dev": "vite dev –port=5050",

    Login or Signup to reply.
  3. In the Dockerfile try to change:

    CMD ["npm", "run", "dev"]

    to

    CMD ["npm", "run", "dev", "--", "-p", "5050]

    Then rebuild your docker image and run it again.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search