Logo

dev-resources.site

for different kinds of informations.

Effortlessly Dockerize Your Vite-React Application

Published at
11/5/2024
Categories
docker
react
container
dockerize
Author
rakibtweets
Categories
4 categories in total
docker
open
react
open
container
open
dockerize
open
Author
11 person written this
rakibtweets
open
Effortlessly Dockerize Your Vite-React Application

Steps to Dockerize a Vite + React App

1. Create a Dockerfile

Create a file named Dockerfile in the root of your project with the following content:

# Use an official node image as the base image
FROM node:18-alpine


WORKDIR /app

# Copy package.json and package-lock.json
COPY package*.json ./

RUN npm install

COPY . .

# Expose the port the app runs on
EXPOSE 5173

# Command to run the application
CMD ["npm", "run", "dev"]`

Enter fullscreen mode Exit fullscreen mode
  1. Create a .dockerignore file Create a .dockerignore file in the root of your project to exclude unnecessary files:
node_modules
npm-debug.log
build
.dockerignore
**/.git
**/.DS_Store
**/node_modules
Enter fullscreen mode Exit fullscreen mode

3. Build the Docker image

Run the following command in your terminal to build the Docker image:

docker build -t vite-react-app .
Enter fullscreen mode Exit fullscreen mode

4. Run the Docker container

After the image is built, run the container using:

docker run -p 5173:5173 vite-react-app
Enter fullscreen mode Exit fullscreen mode

Your Vite + React app should now be accessible at http://localhost:5173.

5. Give name Docker container and run in detached mode

After the image is built, run the container using:

docker run -d -p 5173:5173 --rm --name vite-react-container vite-react-app
Enter fullscreen mode Exit fullscreen mode

Now if we stop the container it will be deleted automatically.

6. Using Docker Compose

Docker Compose is a tool for defining and running multi-container Docker applications. Here's how to use it with your Vite + React app:

6.1. Create a docker-compose.yml file

Create a file named docker-compose.yml in the root of your project with the following content:

version: '3.8'
services:
  react-vite-app:
    image: react-vite-app
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - '5173:5173'
    container_name: react-vite-container
    stdin_open: true
    environment:
      - CHOKIDAR_USEPOLLING=true
    volumes:
      - .:/app
      - /app/node_modules

Enter fullscreen mode Exit fullscreen mode

This docker-compose.yml file sets up a Docker container for a React project built with Vite. It mounts the project directory to /app for live code updates and ignores /app/node_modules to prevent conflicts with the host's node_modules. As a result, any changes in the code will be reflected in the running container at http://localhost:5173.

6.2. Run the container using Docker Compose

To start your application using Docker Compose, run the following command in your terminal:

docker-compose up
Enter fullscreen mode Exit fullscreen mode

This command will build the image if it doesn't exist and start the container. Your Vite + React app should now be accessible at http://localhost:5173.

6.3. Stop the container

To stop the container, you can use:

docker-compose down
Enter fullscreen mode Exit fullscreen mode

This will stop and remove the containers defined in your docker-compose.yml file.

Using Docker Compose provides several advantages, including easier management of environment variables, volume mounting for live code updates, and the ability to define and run multi-container applications.

container Article's
30 articles in total
Favicon
How to run a Nginx-web server
Favicon
Docker Basics
Favicon
What is Kubernetes Vs Terraform
Favicon
It is time to express your intention ,before you really code
Favicon
Docker Hands-on: Learn Docker Volume and Bind Mounts with Sample Projects using NGINX
Favicon
Can I start and stop Docker Desktop using CLI?
Favicon
The Power of Containers: Why Docker is Essential in Cloud, AI, Software Engineering and DevOps
Favicon
Docker Tutorial and Easy Guide to Master Dockerfile, Images, Containers, Commands, Volume, Network, and Compose
Favicon
Mastering the Container-Presenter Pattern in Angular: A Deep Dive
Favicon
Terraform: Use Template file for AWS CodeDeploy AppSpec file
Favicon
Building a PSR-11 Compatible Dependency Injection Container with PHP 8.4 Lazy Objects
Favicon
PnR: Configuration-Intention Driven Container Orchestration with Go's Platform Abstraction
Favicon
Why Rootless Containers Matter: A Security Perspective
Favicon
How to Install Tailscale in a Proxmox CE 8.2 LXC Container (AlmaLinux 9)
Favicon
Create a container using the Ubuntu image in Docker.
Favicon
Kubernetes คืออะไร? แบบ Dev เห็นภาพ
Favicon
A brief breakdown of Kubernetes architecture
Favicon
Docker Image Optimization: Reducing Size for Faster Deployments
Favicon
Docker
Favicon
Dockerfile Anti-Patterns: What Not to Do
Favicon
Docker Layer Caching Explained: Tips to Improve Build Times
Favicon
Homemade application firewall for Linux
Favicon
Kubernetes: Introduction
Favicon
Pod Security with K8Studio
Favicon
Docker ARG vs ENV: Understanding Build-time and Runtime Variables
Favicon
Containerize Rust Application in 2 Minutes using Docker Init
Favicon
What is a Container Registry
Favicon
How to Use Docker to Improve Your Development Workflow: A Complete Guide
Favicon
Effortlessly Dockerize Your Vite-React Application
Favicon
Docker Networking every developer should know

Featured ones: