Building a Docker image from a JavaScript file
One of the simplest and most basic exercises in containerizing and getting familiar with Docker files is working with a simple JavaScript file. In this example, we will containerize a simple JavaScript file that doesn’t perform any specific function, or to put it more simply, we will create an image in Docker from this file. To get started, we will create a JavaScript file that contains, for example, console.log('hello');. Next, alongside this file, we will create a file named DockerFile. Make sure that the file name is exactly this, and then we will open the file to start our work. First of all, we need Node.js to run JavaScript files, so we will download the Node.js image from Docker Hub: `FROM node:alpine` Using the FROM command, we download the Alpine version, which is a lightweight version of Node.js. Now we need to create a directory in the image we are building to store the files and execute commands: WORKDIR ./jsdir With the WORKDIR command, you can create a directory with any name you want. Now it’s time to copy the files into this directory: `COPY hello.js .` Now that we have copied the files, we move on to the step of executing the hello.js file that we created: `CMD node hello.js` Now, if you build and run the image using the following command, you will be able to see the printed output of “Hello”: `docker build -t hello .`
![Building a Docker image from a JavaScript file](https://media2.dev.to/dynamic/image/width%3D1000,height%3D500,fit%3Dcover,gravity%3Dauto,format%3Dauto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyyq4w2bon0j3ypuukump.png)
One of the simplest and most basic exercises in containerizing and getting familiar with Docker files is working with a simple JavaScript file. In this example, we will containerize a simple JavaScript file that doesn’t perform any specific function, or to put it more simply, we will create an image in Docker from this file.
To get started, we will create a JavaScript file that contains, for example, console.log('hello');.
Next, alongside this file, we will create a file named DockerFile. Make sure that the file name is exactly this, and then we will open the file to start our work.
First of all, we need Node.js to run JavaScript files, so we will download the Node.js image from Docker Hub:
`FROM node:alpine`
Using the FROM command, we download the Alpine version, which is a lightweight version of Node.js.
Now we need to create a directory in the image we are building to store the files and execute commands:
WORKDIR ./jsdir
With the WORKDIR command, you can create a directory with any name you want.
Now it’s time to copy the files into this directory:
`COPY hello.js .`
Now that we have copied the files, we move on to the step of executing the hello.js file that we created:
`CMD node hello.js`
Now, if you build and run the image using the following command, you will be able to see the printed output of “Hello”:
`docker build -t hello .`