Problem about start vite project with Docker

Problem to create vite project with docker

I’m trying to create a Vite project with Docker, but I’m running into an issue when I attempt to build the container using docker-compose up --build -d.

Docker files

Dockerfile

FROM node:23-alpine

WORKDIR /app

RUN npm install -g create-vite

RUN npm create vite@latest . --template vanilla --yes

RUN npm install

EXPOSE 3000

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

docker compose

version: '3'

services:
  vite:
    build: .
    volumes:
      - ./:/app
    ports:
      - "3000:3000"
    container_name: "vite-project"

Error

Building vite
[+] Building 0.5s (8/8) FINISHED                                                                          docker:default
 => [internal] load build definition from Dockerfile                                                                0.0s
 => => transferring dockerfile: 217B                                                                                0.0s
 => [internal] load metadata for docker.io/library/node:23-alpine                                                   0.0s
 => [internal] load .dockerignore                                                                                   0.0s
 => => transferring context: 2B                                                                                     0.0s
 => [1/5] FROM docker.io/library/node:23-alpine                                                                     0.0s
 => CACHED [2/5] WORKDIR /app                                                                                       0.0s
 => CACHED [3/5] RUN npm install -g create-vite                                                                     0.0s
 => CACHED [4/5] RUN npm create vite@latest . --template vanilla --yes                                              0.0s
 => ERROR [5/5] RUN npm install                                                                                     0.5s
------                                                                                                                   
 > [5/5] RUN npm install:                                                                                                
0.419 npm error code ENOENT
0.419 npm error syscall open
0.419 npm error path /app/package.json
0.419 npm error errno -2
0.419 npm error enoent Could not read package.json: Error: ENOENT: no such file or directory, open '/app/package.json'
0.419 npm error enoent This is related to npm not being able to find a file.
0.419 npm error enoent
0.420 npm error A complete log of this run can be found in: /root/.npm/_logs/2025-04-04T17_58_38_932Z-debug-0.log
------
Dockerfile:9
--------------------
   7 |     RUN npm create vite@latest . --template vanilla --yes
   8 |     
   9 | >>> RUN npm install
  10 |     
  11 |     EXPOSE 3000
--------------------
ERROR: failed to solve: process "/bin/sh -c npm install" did not complete successfully: exit code: 254
ERROR: Service 'vite' failed to build : Build failed

What is the solution to this issue?

It seems npm create vite@latest . --template vanilla --yes still prompts for user interaction

Also, in compose, the version property is obsolete

I’m not sure why you moved the topic back to Support after I moved it to Image Builds, maybe by accident, but I moved it for a reason as the topic has nothing to do with Docker Support, meaning asking the Docker about something instead of the community. I moved the topic back again, let’s keep it in “image builds”.

You didn’t copy the files. The volumes section works only for the running container but first you need to copy the code to the image if you want to run npm commands. The error message seems to be clear about the missing file

Search for the COPY instruction for Dockerfiles

I can also recommend this guide

Using create-vite in Dockerfile, but then bind mount the folder from host in compose? To me that setup does not make sense.

I have been using SvelteKit, which uses Vite, maybe this tutorial can demonstrate a working multi-stage build process.

I am sure there are similar tutorials available just for Vite.