DOCKER - 4 8 Official image extension
In this lesson we extend an official image rather than build from scratch. The sample directory contains two files: a small Dockerfile and a basic index.html (a Hello World page) that we'll copy into our container image during the build. Whenever you can start from an official Docker Hub image and simply layer your changes on top, do so — it's far easier to maintain than reinventing Nginx, MySQL or any other piece of software yourself.
The Dockerfile, three instructions
FROM nginx:latest
WORKDIR /usr/share/nginx/html
COPY index.html index.html
FROM picks the base image — here the official Nginx — and we inherit everything it declares, including the default CMD that starts the Nginx process. That's why we don't need to repeat CMD. WORKDIR changes directory inside the image; it's the recommended way to navigate, far better than chaining RUN cd ... calls, and it documents intent clearly. We move into Nginx's default static-files directory. COPY brings files from your build context into the image — here we drop our custom index.html in place of the default welcome page.
First, let's confirm the default Nginx works. Run it briefly and remove the container on exit:
docker container run --rm -p 80:80 nginx
Open localhost in a browser — you'll see the default Nginx welcome page. Stop it, then build our custom image and run it the same way:
docker image build -t nginx-with-html .
docker container run --rm -p 80:80 nginx-with-html
The build finishes in seconds because the Nginx layers are already cached locally — only our tiny copy step is new. Refresh the browser and you'll see our custom HTML served instead of the default page. docker image ls shows the new image at the top.
- FROM — start from an official image (you inherit its
CMD, ports, etc.) - WORKDIR — change directory cleanly inside the image
- COPY — drop source files from your machine into the image
docker image tag nginx-with-html myaccount/nginx-with-html— retag beforedocker push
To publish, retag with your Docker Hub account name and run docker push. docker image ls will show two tags pointing to the same image ID, ready for upload.
Summary
This lesson demonstrates how to extend official Docker images from Docker Hub (using Nginx as an example) by inheriting from them and adding custom code. It explains key Dockerfile directives (FROM, WORKDIR, COPY) and shows how official images provide pre-configured functionality (like Nginx running as a web server) while allowing developers to customize them. The lesson includes a practical demonstration of building a custom Nginx image with a personalized HTML file and pushing it to Docker Hub.
Key points
- Official images from Docker Hub reduce maintenance burden and provide tested, reliable base configurations—use them whenever possible rather than building everything from scratch
- WORKDIR is the best-practice way to change directories in a Dockerfile (better than RUN cd commands) because it makes the build steps more readable and traceable
- Docker images form an inheritance chain through the FROM directive—when you build on an official image, you automatically inherit all its configurations (CMD, exposed ports, etc.) without having to redefine them
- The COPY directive is essential for incorporating your source code into container images, enabling you to customize official images with your own files and configurations
- Image layers and caching speed up builds—when rebuilding, Docker reuses cached layers (like the Nginx base image) and only processes new instructions, making subsequent builds very fast
- Docker images can be versioned and published to Docker Hub by re-tagging them with your account name, following the same push workflow as any other image
FAQ
Why should I use official images from Docker Hub instead of building my own base image?
Official images reduce maintenance burden, are well-tested by the community, come with proper security updates, and handle default configurations for you. As your systems become more complex and you need customizations, you can extend official images rather than manage everything from scratch.
What is the difference between using RUN cd and the WORKDIR directive in a Dockerfile?
While RUN cd technically changes directories, WORKDIR is the best practice because it is clearer, more readable, and makes the Dockerfile easier to maintain. WORKDIR explicitly declares directory changes during the build, whereas RUN cd only affects that single RUN instruction and is easily missed when reading the file.
How do I add my own code to an official image like Nginx?
Use the COPY directive to copy your source code from your local machine or build server into the container image. For example, COPY index.html /usr/share/nginx/html/ replaces the default Nginx homepage with your custom HTML file. You inherit the base image's CMD (startup command) automatically through the FROM directive, so you don't need to redefine it.