Skip to main content

Writing a Dockerfile

Downloading pre-built images like node or postgres from Docker Hub is great, but the true power of Docker shines when you need to freeze and package your own code (For example, your Python backend) so that your teammates can run it without installing Python on their computers.

To transform your code into a customized "Docker Image", we use a simple text file called Dockerfile. (Like that, uppercase at the beginning, without extensions like .txt or anything).

This file must be hosted in the root folder (the base) of your project.

Structure of a Dockerfile

The Dockerfile is literally a recipe that runs from top to bottom instruction by instruction.

There are 4 or 5 vital keywords that you will use 99% of your career.

Let's imagine you have a simple application made in Node.js that requires installing packages with npm install and starts with node app.js, this would be your base Dockerfile:

# 1. FROM: "The base ingredient"
# Defines the operating system or pre-built container from which we will start.
# INSTEAD of asking for an empty 'ubuntu' and installing NodeJS by hand via terminal...
# We use an official image that the NodeJS people already preloaded for us and that is super slim (alpine is a miniature and secure version of Linux).
FROM node:18-alpine

# 2. WORKDIR: "Creating my office"
# We enter the Linux emulated by Docker and tell it to create and send us to the '/app' folder.
# Any subsequent command we write will occur EXCLUSIVAMENTE inside that safe isolated room.
WORKDIR /app

# 3. COPY: "Moving files from Windows to Linux"
# Syntax: COPY [your local file] [Linux destination]
# The [.] means "Copy EVERYTHING from my real project on my PC to the Linux folder where we currently stand (/app)"
COPY . .

# 4. RUN: "Running the setup"
# Command for that Linux to start writing things in the terminal.
# This is where we install all our libraries and assemble the software packaging of our code that we previously brought.
RUN npm install

# 5. CMD: "Instructing how to start"
# This command IS NOT executed at the time of building the image (like 'RUN').
# This command will remain 'asleep' and will execute ONLY when someone runs 'docker run' on their own screen.
CMD ["node", "app.js"]

Building your Image (docker build)

You have your recipe (Dockerfile) ready. Your machine still does not have a generated image. To "bake" it, open your terminal where the file is and type build.

docker build -t my-super-node-api .

Breakdown of this command that will change your productive life:

  • build: The main cooking order.
  • -t: Means tag. We use it to give it a descriptive name ("my-super-node-api") and not just an incomprehensible random ID of 40 numbers and letters.
  • .: "Dot" means "The current directory". You are telling it: "Hey Docker, look for the file named Dockerfile in this exact folder and use it, because all the source code is there too".

If you run docker images in your console after this, you will proudly notice a new installed image with the name of your application, ready to be converted into a container wherever, whenever, and to provide service as if nothing happened on any OS on this planet.