Explore Courses Blog Tutorials Interview Questions
0 votes
in DevOps and Agile by (2.3k points)

So while learning Docker I saw that WORKDIR & COPY commands were used a lot in the dockerfile like given below:

FROM node:latest
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY package.json /usr/src/app/
RUN npm install
COPY . /usr/src/app
CMD [ “npm”, “start” ] 

I just don't get the whole point of using the WORKDIR & COPY commands, I would prefer to just work from the root of my project.

Are there any downsides to this approach?

1 Answer

0 votes
by (6.9k points)

Let's first see the definition/purpose of both of them :

WORKDIR : This instruction is used to set the working directory in the container.

COPY : This instruction is used to copy any files from the host onto the container.

The reason why these instructions should be used are :

  • Using WORKDIR gives clarity and reliability.
  •  Using RUN cd … && do-something instructions instead is tougher as they are hard to read & maintain & troubleshoot.
It would be better if you understood these instructions and made proper use of them as these are part of the best docker practices, so keep them in mind. 
Hope this helped :)

If you want to know more about how to make really effective docker files and overall become better at docker make sure to check out docker training course.

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Sep 18, 2020 in Docker by Justin (7k points)

Browse Categories