Skip to content
English
On this page

Dockerfile

A Dockerfile is a text file with a series of commands in it. Listing 1.1 is the Dockerfile we’ll use for this example. Create a new folder, move into it, and create a file called “Dockerfile” with these contents.

dockerfile

You begin the Dockerfile by defining the base image with the FROM command. This example uses a Node.js image so you have access to the Node.js binaries. The official Node.js image is called node. Next, you declare the maintainer with the LABEL command. In this case, we’re using one of our email addresses, but you can replace this with your own reference because it’s your Dockerfile now. This line isn’t required to make a working Docker image, but it’s good practice to include it. At this point, the build has inherited the state of the node container, and you’re ready to work on top of it.

Next, you clone the todoapp code with a RUN command. This uses the specified command to retrieve the code for the application, running git within the container. Git is installed inside the base node image in this case, but you can’t take this kind of thing for granted.

Now you move to the new cloned directory with a WORKDIR command. Not only does this change directories within the build context, but the last WORKDIR command determines which directory you’re in by default when you start up your container from your built image.

Next, you run the node package manager’s install command (npm). This will set up the dependencies for your application. You aren’t interested in the output in this example, so you redirect it to /dev/null. Because port 8000 is used by the application, you use the EXPOSE command to tell Docker that containers from the built image should listen on this port. Finally, you use the CMD command to tell Docker which command will be run when the container is started up.

This simple example illustrates several key features of Docker and Dockerfiles. A Dockerfile is a simple sequence of a limited set of commands run in strict order. It affects the files and metadata of the resulting image. Here the RUN command affects the filesystem by checking out and installing applications, and the EXPOSE, CMD, and WORKDIR commands affect the metadata of the image.