DOCKER - 4 9 Exercise Create a Dockerfile
Time to build your own image from a Dockerfile. A common task for a Docker admin is to "dockerize" an existing application, often without deep knowledge of the language or the packages it uses. The process: study an official image, pick the best base with FROM, and stitch in the build instructions your app needs until you have a working container.
The brief — dockerize a Node.js app
We'll start from an existing Node.js web app. You don't need to know anything about Node beyond the fact that it's a web application. Your job is to write a Dockerfile that builds it, then test it in a container, push the image to Docker Hub, delete the local copy, and finally re-run it from the registry to confirm everything still works end to end.
- Use the official Node image on Docker Hub — its README is well documented
- Pick the Alpine variant (much smaller than the default)
- Pin to the Node 6.x branch (any tag under
6works) - Expected result: open
localhostin your browser and see the app
Once the local container runs successfully, tag the image with your Docker Hub account and a fresh repository name, then push it. After verifying it appears on Docker Hub, delete the image from your local cache so you can prove the round trip works:
docker image build -t myaccount/node-app .
docker container run -d -p 80:3000 --name web myaccount/node-app
# open localhost in browser → app should answer
docker image push myaccount/node-app
docker image rm myaccount/node-app
docker container run -d -p 80:3000 --name web2 myaccount/node-app
# Docker re-downloads from Hub, then runs the app again
Your Dockerfile may look a little different from mine — that's fine. As long as the build completes without errors and the web page answers on localhost, the exercise is a success. The next lesson walks through one possible solution.
Summary
This lesson teaches how to create a Dockerfile for an existing application, specifically a Node.js web app. The exercise involves building a Docker image, testing it locally via a web browser, tagging it with Docker Hub credentials, pushing to Docker Hub, and re-running it from the remote registry. The objective is to understand how to select appropriate base images, merge them with build instructions, and successfully "Dockerize" an existing application.
Key points
- Dockerizing means creating a Dockerfile for an existing application and making it work correctly in a Docker container, even without knowledge of the app's language or dependencies
- Select an appropriate official base image from Docker Hub—Alpine Linux is recommended for creating small, efficient images; use Node.js version 6.x for this exercise
- Build the image, test it locally by running the container and accessing it via localhost in a web browser to ensure the web app functions correctly
- Tag the Docker image with your Docker Hub account name and repository name, then push it to Docker Hub
- Delete the local image from cache and re-run the container from Docker Hub to verify it pulls and executes correctly from the remote registry
- A successful Dockerfile does not need to match a reference exactly—it succeeds if it builds without errors and the web application functions properly in a browser
FAQ
What is 'Dockerizing' an application?
Dockerizing means creating a Dockerfile for an existing application and making it run correctly inside a Docker container. It is a common task for Docker administrators and involves exploring official images and their Dockerfiles to understand how to construct your own, even if you lack knowledge of the application's language or dependencies.
What base image should I use for this exercise?
Use an official Alpine Linux-based Node.js image from Docker Hub. Specifically, select a version from the Node.js 6.x branch (e.g., node:6.x-alpine). Alpine Linux is chosen because it is a very small Linux distribution, making the resulting Docker image smaller and more efficient.
What are the steps after I create a working Dockerfile?
First, build the image and test it locally by running the container and accessing the web app via localhost in your browser. Then, tag it with your Docker Hub account credentials and repository name, push it to Docker Hub, verify it appears on Docker Hub, delete the local image, and finally run the container again from Docker Hub to confirm it pulls and functions correctly from the remote registry.