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.