educative.io

Docker build question onn node.js

In this exercise: Exercise: Build an Image and Run It - Docker for Developers,
I wrote the following in the Dockerfile:
FROM node:11-alpine

Write your commands here…

COPY /compute.js .
CMD [“node”,“compute.js”]

My questions is the second argument of COPY command. It copies compute.js to the home directory of the default user?? How do we determine the correct path to use? In the previous example for Nginx server, we need to specify COPY index.html /usr/… to make the Nginx server works… Just a bit confused about this.
Thanks in advance.

Hi @BeauRob !!
In Docker, the second argument of the COPY command specifies the destination path within the container where the file or directory from the source path (first argument) should be copied to. Let’s break down your questions:

  1. Copying Files with COPY Command:
    In your Dockerfile, you have this line:

    COPY /compute.js .
    

    Here, /compute.js is the source path, and . is the destination path. The . represents the current working directory within the container. So, the compute.js file from the source path will be copied to the current working directory of the container, which is often the home directory of the default user.

  2. Correct Path to Use:
    The correct path to use for the destination depends on where you want to place the file within the container. In the example, copying compute.js to the current working directory of the container (often the home directory of the default user) seems to be your intention.

  3. Comparison to Nginx Example:
    In the case of the Nginx server example, you mentioned:

    COPY index.html /usr/…
    

    This is copying the index.html file from the source path to the /usr/… directory within the container. The specific path (/usr/… in this case) depends on where the Nginx server is configured to look for its content. The COPY command is used to make sure that the necessary files are available within the container for the application or service to function properly.

In both cases, you need to determine the correct path to use based on the application’s requirements and where the application expects its files to be located within the container. The paths are relative to the root directory of the container filesystem.

Remember that Docker containers are isolated environments, and file paths within the container are not the same as file paths on your host machine. You’ll need to understand the directory structure and configuration of the application you’re working with to determine the appropriate paths to use in your Dockerfile.
I hope it helps. Happy Learning😊

1 Like

Thank you so much for your detailed explanation. So what is the default user? I’m confused about the default user since for example, nginx default index.html page should be in /var/www/html and not the path used in the tutorial… so does that mean in docker the settings are different than installing directly on the machine? Or there’s some configuration for this.
Thank you :blush: for your answer again.