Create React Dev Env using Docker GPT
Create React Dev Env using Docker GPT
To create a Docker container for a React application that can be run in Visual Studio Code (VSCode), you can follow these steps:
-
Create a React App: If you don't have a React app yet, create one using the following command:
bashCopy code npx create-react-app my-react-app -
Create a Dockerfile: In the root of your React app, create a file named
Dockerfilewith the following content:DockerfileCopy code # Use an official Node.js runtime as a parent image FROM node:14-alpine # Set the working directory in the container WORKDIR /usr/src/app # Copy package.json and package-lock.json to the working directory COPY package*.json ./ # Install app dependencies RUN npm install # Copy the local code to the container COPY . . # Expose port 3000 to the outside world EXPOSE 3000 # Start the application CMD ["npm", "start"] -
Build the Docker Image: Open a terminal and navigate to the directory containing your
Dockerfileand run the following command to build the Docker image:bashCopy code docker build -t my-react-app . -
Run the Docker Container: Once the image is built, you can run a container using the following command:
bashCopy code docker run -p 3000:3000 -v $(pwd):/usr/src/app my-react-appThis command maps port 3000 from the container to your local machine and mounts the current directory as a volume so that changes in your code are reflected inside the container.
-
Open in VSCode: Open Visual Studio Code and install the "Remote - Containers" extension if you haven't already.
- Press
F1orCtrl+Shift+Pto open the command palette. - Type "Remote-Containers: Attach to Running Container" and select it.
- Choose the container you just started (it should be named something like
my-react-app).
- Press
VSCode will attach to the running container, and you can now edit and debug your React app directly within the container.
Remember to stop the running container when you're done:
bashCopy code
docker stop <container-id>
Replace <container-id> with the actual ID or name of your running container.
This setup allows you to have a consistent development environment inside a Docker container and use VSCode to interact with it seamlessly.
Docker Commands
List all the containers (irrespective of the state)
docker ps -a
docker start <container-id>
docker stop <container-id>