Outline
You can create your own image by invoking a Dockerfile document. Let me show you how that will work. First, though, I'll create a very simple index.html file that we'll later import into our image. I'm saving the file to the local directory.
Now here's my Dockerfile:
FROM ubuntu/apache2:2.4-22.04_beta
ENV DEBIAN_FRONTEND="noninteractive" TZ="America/Toronto"
RUN apt-get update
RUN apt-get install -y sysstat
ADD index.html /var/www/html/
CMD /usr/sbin/apache2ctl -D FOREGROUND
EXPOSE 8080:80
By the way, it's really important to name the file with an uppercase "D" or things might not work the way you expect. The seven lines of this file represent seven things that will happen as we create the image. The first FROM line tells Docker that we want to use that Ubuntu/Apache image as our base. The ENV line serves pretty much the same function as the -e
argument we used before: this will ensure that Ubuntu installs without the need for manual interaction. In particular, this will set the system timezone - Toronto in this case.
The two RUN lines tell Docker to run these two commands once Ubuntu is installed. The first will update the APT package repository index, and the second will install the sysstat
package. We don't need that package, but it's a good way to illustrate how such things would work. ADD will take that index.html
file we created before and save it to the Apache web root directory. It will overwrite the default welcome page and replace it with ours. Naturally, you can add any files or applications you like. This, in fact, is where you customize your image. CMD will ensure that Apache is active and running once the container loads, and EXPOSE configures the networking we need.
With those two files in place, I'll run docker build
to create the image. -t
sets the image's name and, optionally, tag. I'll just give it the name "newimage". Note that dot at the end of the command: it's job is to tell docker build
to look for a Dockerfile in the current directory.
The build process will take a few minutes. You'll see all the seven steps applied one at a time, including the internal Ubuntu package installation process. That's not very exiting, so I'll fast forward a bit. Here's where we move ahead to step 5 - adding that index.html file - then the Apache active step number 6, and finally step 7. When it's all done, we're given a success message, and shown the auto-assigned tag: that's the "latest" here.
When I run docker images
I can see our "newimage" along with the apache2 and hello-world images that we already downloaded. With that, we're all set to launch our new image as a container. This is all it'll take. And a few moments later, docker ps
shows us that everything looks good.
docker build -t newimage .
docker images
clear
docker run -d newimage
If you'd like to push your image to your repo in Docker Hub so you can make it available for collaborators, you first need to log in to Docker Hub. If necessary, you can create a free account on the hub.docker.com website. I pasted my password, but thankfully it's not visible in the terminal.
Now, before I can upload the image, I'll need to tag it in a way that includes the name of my own Docker Hub repo. That's how Docker will know where to send the image. Here's how that will look. tab complete is making my job a lot easier here, by the way. With the tag set, I'll run docker push
using the tagged name and it's on it's way. After just a few seconds I'll be able to proudly view the new image on the website.