DEV Community

Cover image for Mission 2: Containerise the App
okunola babatunde
okunola babatunde

Posted on

Mission 2: Containerise the App

Introduction:

Welcome back to Mission 2! In the https://dev.to/okunola_babatunde_dff5cbd/mission-1-arm-your-workstation-438i, we laid the groundwork by setting up our project folder, building a tiny Node.js application, saving it to Git, and linking everything up to GitHub.

Now that we have a solid, version-controlled codebase, it's time to take the next critical step in modern development: containerization.

In this guide, I am going to take that exact Node.js app from our GitHub repository and package it into a lightweight, portable, and isolated container using Docker. By the end of this mission, you will ensure that your app runs consistently, regardless of where it is deployed. Let's get started!

Implementation Guide

1. Create Your First App

Instruction Summary: Creates a folder, makes a tiny Node.js app, saves it into Git history, and links it to your GitHub repository.
Why It's Needed:
Every epic journey starts with a single line of code. We connect to GitHub early so your work is always backed up and ready for CI/CD!

Pillar Connection
Operational Excellence — Versioning controls everything from minute one.
terminal
In order to create a folder and switch into the folder, run the command "mkdir devops-lab; cd devops-lab"
mkdir

To create the app.js file using PowerShell, run this command "Set-Content app.js 'console.log("Hello, DevOps!");'"

mkdir
The next step is to initialize a new Git repository in the current directory by running this command "git init"
mkdir
To list, creat or manage branches, run this commmand "git branch -M main"
mkdir
To stage all the changed files, run these command at a go "git add . && git commit -m 'Initial commit'"
mkdir
In order to connect the local repo on the machine with the Github repo created in mission 1, run the following command "git remote add origin plus url of the github repo created in the mission 1". However, this command stages the specified file(s) for the next commit.
mkdir
To manage remote repository connections (origin, upstream, etc.). Run this command "git remote -v"
git remote =v
Note: If this command "git remote -v" is run and there is no evidence of fetch and push, it means there is an error or errors somewhere.

2. Build the Docker Container
Instruction Summary: Writes a recipe (Dockerfile) to package your app, builds it into an image, and runs it! The Dockerfile tells Docker which base image to use, which files to copy in, and which command to run.
Why It's Needed: Containers ensure that if it works on your laptop, it'll work on the production servers without those pesky 'it works on my machine' excuses.
Pillar Connection:
Reliability — Eliminating environment drift with magical immutable boxes.
From the power shell, create a docker file running the following command;
@'

FROM node:18-alpine

COPY app.js .

CMD ["node","app.js"]

'@ | Out-File -FilePath Dockerfile -Encoding ascii
docker file

To successfully know if the docker file has been created, run this command "Get-Content Dockerfile"
docker file

  • Run this command "docker build -t devops-hello ." to build a Docker image from the Dockerfile in the current directory. The -t flag tags it with a name. docker fille
  • To creates and start a new container from the specified image, run this command "docker run --rm devops-hello" docker file

Summary

In this mission, you broke your application free from the unique quirks of your personal computer. You wrote a tiny JavaScript program, saved a snapshot of it using a tracking tool called Git, and then created a precise recipe file called a Dockerfile. This recipe tells Docker exactly how to wrap your program inside its own self-contained, portable digital box called an image.

Conclusion

By putting your app inside this digital box, you guaranteed that it will run flawlessly anywhere. Whether it is running on your laptop, a colleague's computer, or a massive cloud server, it will behave exactly the same way every single time. This completely solves the classic developer headache: "Well, it worked perfectly on my machine!" This predictable, hassle-free packaging is the foundation of how modern software is built and delivered to millions of users today.

Key Takeaways

  • The "Frozen" Blueprint: Think of a Docker image as a frozen, unchangeable blueprint. It contains everything your program needs to survive and run, so you never have to worry about missing files or wrong software versions.

  • Packing Light: By using a tiny, stripped-down version of Node.js (alpine), your digital box stays incredibly small and lightweight. This means it downloads in seconds and uses almost none of your computer's memory.

  • No Leftover Mess: By using a special cleanup command (--rm), the digital box instantly vanishes the moment it finishes running. This keeps your computer completely clean, with no digital clutter left behind.

  • Future-Proofing: By connecting your project to GitHub right from the start, you ensure your work is always backed up safely in the cloud and ready to be automatically updated whenever you write new code.

Top comments (0)