This directory provides Dockerfiles for deploying a React Vite application. You can choose between three different
1. Dockerfile
- Use a custom built busybox image as the base image.
- This uses
alpine:3.20
as the base image. It uses the root user to run the application. See the Dockerfile_alpine_nonroot file for a non-root user version.
- This uses
alpine:3.20
as the base image. It uses a non-root user to run the application.
- This uses
busybox
as the base image.
- Uses
nginx:alpine
as the base image.
Heres a comparison of the image sizes. The .slim
images are built using Docker Slim.
Yup, you saw that right, the Dockerfile
is Kilobytes in size!
Copy the .dockerignore
file in the root of your project.
Create an nginx.conf
file in the root of your project.
Note: Only use this file if you are using one of the NGINX Dockerfiles.
docker build -t react-vite .
docker build -f Dockerfile_alpine -t react-vite .
docker build -f Dockerfile_alpine_nonroot -t react-vite .
docker build -f Dockerfile_busybox -t react-vite .
docker build -f Dockerfile_nginx -t react-vite .
We can use Docker Slim to reduce the size further.
Note: docker slim can remove some files that are required for your application to run. So, it is recommended to test the application after using Docker Slim.
For the Dockerfile_alpine_nonroot
file, run the following command:
docker run --rm -it -v /var/run/docker.sock:/var/run/docker.sock dslim/slim build --target react-vite --include-path /var/www/html --include-path /var/log/nginx --include-path /var/lib/nginx --include-path /run/nginx
For others (non busybox), run the following command:
docker run --rm -it -v /var/run/docker.sock:/var/run/docker.sock dslim/slim build --target react-vite --include-path /var/www/html
After successfully running the above command, you will see a new image with name react-vite.slim
in your Docker
After building the image, run the container using the following command:
docker run -p 3000:80 react-vite
If you have improvements or additional configurations for the React Vite setup, feel free to open a pull request!
- https://lipanski.com/posts/smallest-docker-image-static-website
- https://github.com/lipanski/docker-static-website/tree/master
- https://www.geeksforgeeks.org/how-to-run-nginx-for-root-non-root/
- https://medium.com/@pierre.fourny/optimized-docker-setup-for-vite-powered-react-apps-e7b7f5a82bb4
- https://youtu.be/t779DVjCKCs?si=xSdHkSH6YaZ7h54i