'Understanding Dockerfile syntax?

I am learning Docker and looking at this Dockerfile example for React application

FROM node:alpine
WORKDIR /app
COPY package.json ./
COPY package-lock.json ./
COPY ./ ./
RUN npm i
CMD ["npm", "run", "start"]

To me it's saying

  1. Grab image node:alpine from docker library
  2. create WORKDIR called /app
  3. copy the package.json file to the /app dir
  4. copy the lock file also to /app dir
  5. I don't understand what COPY ./ ./ is doing?
  6. command npm install
  7. then CMD npm run start

Am I interpreting this language correctly? Can anyone give me insight of what is actually going on?



Solution 1:[1]

Docker is an open-source containerization platform. Here we run our application in the container(which is managed by Docker Engine). Dockerfile contains all the commands. Also, you can say in Dockerfile that we write all the procedures to make a container runnable.

Coming back to your point...

Here COPY ./ ./ meaning is, COPY <source_path> <destination_path> and <source_path> is the path in your host machine and <destination_path> is path in your container machine

I will try to simplify the other contents in your Dockerfile...

  • FROM node:alpine : Pull image node:alpine from Docker Hub. Here node is the package name and alpine is the Linux distribution with very minimal and required packages.

  • WORKDIR /app: (Work Directory) In container you're setting up your WORKDIR as /app folder.

  • COPY package.json ./: COPY the package.json(host machine) file to ./(current directory) in your container

  • And other COPY will also work in the same way.

  • RUN npm i: RUN command npm i in container

  • CMD ["npm", "run", "start"]: CMD command will executed(npm run start) when Docker Container will start.

For more detail please see Dockerfile Documentation.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dev