Building a Docker container for my react.js app

To build a Docker container for React.js app, you can follow these steps:
  1. Create a Dockerfile. The Dockerfile is a text file that contains instructions for building a Docker image. Create a new file called Dockerfile in the root directory of your project.

  2. Add the following instructions to the Dockerfile:

FROM node:16
WORKDIR /app
COPY package.json .
RUN npm install
COPY . .
EXPOSE 3000
CMD ["npm", "start"]

The WORKDIR instruction sets the working directory for the container.

The COPY instruction copies the package.json file and the entire contents of the current directory into the container.

The EXPOSE instruction exposes the specified ports on the container. In this case, we are exposing ports 3000.

The CMD instruction specifies the command that will be run when the container is started. In this case, we are running the npm command with the start script.

  1. Build the Docker image. Once you have created the Dockerfile, you can build the Docker image using the following command:
docker build -t my-app .

The docker build command builds a Docker image from a Dockerfile. The -t flag specifies the name of the image. In this case, we are naming the image my-app.

  1. Run the Docker container. Once you have built the Docker image, you can run it using the following command:
docker run -p 3000:3000 my-app

The docker run command runs a Docker image. The -p flag maps the specified ports on the host machine to the exposed ports on the container. In this case, we are mapping port 3000 on the host machine to the corresponding ports on the container.

Once the Docker container is running, you can access the React.js UI app at http://localhost:3000.

To share the Docker container image with others, you can push it to a Docker registry, such as Docker Hub. Once the image is pushed to the registry, others can pull it down and run it.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top