8

I am trying to dockerize a Vite React-Typescript boilerplate setup, but I unable to connect to the container.

Installed vite-react-typescript boilerplate:

npm init vite@latest vite-docker-demo -- --template react-ts

Dockerfile

# Declare the base image
FROM node:lts-alpine3.14
# Build step
# 1. copy package.json and package-lock.json to /app dir
RUN mkdir /app
COPY package*.json /app
# 2. Change working directory to newly created app dir
WORKDIR /app
# 3 . Install dependencies
RUN npm ci
# 4. Copy the source code to /app dir
COPY . .
# 5. Expose port 3000 on the container
EXPOSE 3000
# 6. Run the app
CMD ["npm", "run", "dev"]

Command to run docker container in detached mode and open local dev port 3000 on host: docker run -d -p 3000:3000 vite

The vite instance seems to be running just fine within the container (docker logs output):

> [email protected] dev /app
> vite

Pre-bundling dependencies:
  react
  react-dom
(this will be run only when your dependencies or config have changed)

  vite v2.4.4 dev server running at:

  > Local: http://localhost:3000/
  > Network: use `--host` to expose

  ready in 244ms.

However, when I navigate to http://localhost:3000/ within Chrome. I see an error indicating The connection was reset.

Any help resolving this issue would be greatly appreciated!

3 Answers 3

23

It turns out that I needed to configure host to something other than localhost.

Within vite.config.ts:

import { defineConfig } from 'vite'
import reactRefresh from '@vitejs/plugin-react-refresh'

// https://vitejs.dev/config/
export default defineConfig({
  server: {
    host: '0.0.0.0',
    port: 3000,
  },
  plugins: [reactRefresh()],
})

Resolves the issue.

Sign up to request clarification or add additional context in comments.

Comments

4

in package.json use script

"dev": "vite --host"

example:

  "scripts": {
    "dev": "vite --host",
    "build": "tsc && vite build",
    "serve": "vite preview"
  },

or run with vite --host

Comments

0

To use hot reload, so the changes actually get reflected:

export default (conf: any) => {
  return defineConfig({
    server: {
      host: "0.0.0.0",
      hmr: {
        clientPort: ENV_VARIABLES.OUTER_PORT_FRONTEND,
      },
      port: ENV_VARIABLES.INNER_PORT_FRONTEND_DEV, 
      watch: {
        usePolling: true,
      },
    },
  });
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.