Create custom Docker image
Create custom Docker image is our next short tutorial where we will be showing you in just a few easy steps the basics of how to create a simple custom Docker image from the scratch, how to build it and also how to push this image to your own Docker repository. If you are a beginner then this is the right tutorial where you will learn one of the basic tasks when working with Docker, build your own custom images for your own web applications or websites.
Table of contents
Assuming that we have been involved into a really nice project where we have been told to use Docker in order to modernise the existing stack. Well it all starts with installing Docker on our local machine or something similar that will provide a Docker environment. Once the environment is in place the next step is to build a simple basic custom Docker image as we have been told to use some modules that are not present in the default official image. For example we have been asked to build a PHP-FPM image that will have to include two PHP modules like
opcache, modules that are not present by default in the official PHP-FPM Docker image.
Having Docker installed on our local machine we can finally start this short tutorial where we will demonstrate how to build our own custom Docker image, so let’s start by creating a simple folder structure that will help in the future to keep things under control. Assuming that we are using a Mac for this tutorial let’s open a terminal window and create the folder structure like shown in the example below:
mkdir -p ~/docker-templates/php-fpm/7.4 && cd ~/docker-templates/php-fpm/7.4
Next step is to create a filename called
Dockerfile, this particular file will be used as a template for our Docker image:
Custom Docker image content
Having the right folder structure in place and the
Dockerfile created we can now move to our next step where we will define our custom Docker image, the content of this file will be very basic, just enough to have a rough idea about how to build a minimalistic image.
# # My Custom PHP-FMP:7.4 Docker Image # FROM php:7.4-fpm MAINTAINER My Brand
LABEL vendor="My Brand" LABEL version="1.0.0" LABEL releasedate="11-12-2019" LABEL environment="Production" LABEL description="My Brand PHP-FPM-7.4 Docker Image, based on official PHP-FPM-7.4 docker image with two additional modules" RUN apt-get update && apt-get install -y libmemcached-dev zlib1g-dev \ && pecl install memcached-3.1.5 \ && docker-php-ext-enable memcached \ && docker-php-ext-install opcache \ && docker-php-ext-enable opcache
The content of our
Dockerfile file is pretty self-explanatory, it contains a source
FROM php:7.4-fpm, a few
LABEL details and a
RUN section where we have declared our two PHP modules needed for our project,
Of course we can extend the configuration for this custom PHP-FPM image with some environment variables, custom
php.ini values and so on, but as we have said before this is just a basic image so we can get familiar with the basics of building Docker images from the scratch.
Now, save and close
Dockerfile that we have just created and let’s move to our next step where we will build the actual Docker image.
Build custom Docker image
Building a custom Docker image is quite simple, all we have to do is just to invoke
docker build command and we are ready to go, so please let’s get back to our terminal window and run the next docker command:
build -t myreponame/php-fpm:7.4 .
That was all, our custom image has been successfully created, being now available on our machine and ready to use within our project. We can check to see if our image is really present locally by using
docker image ls command, a successful output will look something like this:
REPOSITORY TAG IMAGE ID CREATED SIZE myreponame/php-fpm 7.4 119f0710e22d 2 minutes ago 427MB
Push Docker image to Docker Hub
We have managed to build our custom Docker image but what if we need to use this image on a different machine? Well we have to publish / upload this image to a repository from where this can be pulled later for various projects. The simplest way to publish our custom image is to create a Docker Hub account and to push it there.
Assuming that we already have an active Docker Hub account called
myreponame let’s try to push this image to our account with a single command line like shown in the example below:
docker push myreponame/php-fpm:7.4
docker push command finishes we can check on our Docker Hub account page under Repositories section.
This is all, our short tutorial ends here, we have managed to build a basic custom Docker image and push it to Docker Hub.