DEV Community

Morteza Mousavi
Morteza Mousavi

Posted on

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`
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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 .`
Enter fullscreen mode Exit fullscreen mode

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`
Enter fullscreen mode Exit fullscreen mode

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 .`
Enter fullscreen mode Exit fullscreen mode

ACI image

ACI.dev: The Only MCP Server Your AI Agents Need

ACI.dev’s open-source tool-use platform and Unified MCP Server turns 600+ functions into two simple MCP tools on one server—search and execute. Comes with multi-tenant auth and natural-language permission scopes. 100% open-source under Apache 2.0.

Star our GitHub!

Top comments (0)

Billboard image

Try REST API Generation for Snowflake

DevOps for Private APIs. Automate the building, securing, and documenting of internal/private REST APIs with built-in enterprise security on bare-metal, VMs, or containers.

  • Auto-generated live APIs mapped from Snowflake database schema
  • Interactive Swagger API documentation
  • Scripting engine to customize your API
  • Built-in role-based access control

Learn more

👋 Kindness is contagious

Dive into this insightful write-up, celebrated within the collaborative DEV Community. Developers at any stage are invited to contribute and elevate our shared skills.

A simple "thank you" can boost someone’s spirits—leave your kudos in the comments!

On DEV, exchanging ideas fuels progress and deepens our connections. If this post helped you, a brief note of thanks goes a long way.

Okay