Create and Share Your First Docker Image
In the previous post, we looked at Dockerfile instructions, Dockerfile syntax, Docker images, and Docker containers. Let’s put of all of this Docker knowledge we have into action, i.e. take a real life scenario, and use Docker to simplify it.
Imagine you’re developing an awesome new application and need a Redis service (more on Redis later) to handle its data. Now, you can install Redis locally if you’re the only one developing the application. But in a distributed environment, you want everyone to use the same set of similarly configured services so there are no surprises during deployment. A simple solution is to create a Redis Docker image and distribute it across the teams.
We’ll start by creating the Dockerfile. Then we’ll create the Docker image from the Dockerfile. We’ll run it as a containerized service. Then finally, we’ll learn how to use the Docker hub to share your Docker images.
Preparing the Dockerfile
As you might know, Redis is a NoSQL key-value data store with support for data structures like Hashes, lists, and so on. Another key aspect is that while in use, the data remains in-memory giving very fast response time. But persistent storage is also available so that data doesn’t get lost when system reboots.
Get Started With the Dockerfile
First create a new folder at a convenient location on your computer. We’ll use this to store everything relevant to the Redis Docker Image.
In this example we will only have a Dockerfile, but it’s always good practice to segregate all the file system dependencies in one folder before creating the image.
After creating the folder, create the empty Dockerfile:
Setting the Base Image and Maintainer
Now that the Dockerfile is created, open it in your favorite text editor.
To begin with, set the base image. Next step is to add the info about the maintainer of the image. This is not mandatory, but it is always good to specify contact info so people can get in touch if they have an issue.
Install the Redis Server
The next step is to install the Redis server. But before that, we need to update the application repository list, so that we get the latest software.
Do this with the
Expose the Default Port
Finally we expose the default port for Redis using the
EXPOSE instruction and then set the default command for this image.
This completes the Dockerfile for your Redis server.
You can now save the file. Here’s how it should look:
Build the Docker Image
Now that the Dockerfile is ready, let’s create the image.
Run this command:
A path is a mandatory argument for the
build command. We used
. as the path because we’re currently in the same directory. We also used the
-t flag to tag the image. To learn about other flags you can use with the with
build command, run
sudo docker build --help.
Run a Redis-Server Instance
With the image we just created, we can now create a container running a Redis server instance inside.
Run this command:
This creates a container with the name
redis_instance. It is generally good practice to assign the name for the container or else you will need to deal with complex alphanumeric container IDs.
Upload to Docker Hub
Now that we’ve successfully built an image and created a container with it, let’s move on to learn about Docker hub and see how to use it to share images.
Docker hub is a public registry maintained by Docker (the company). It has over 15,000 images that can be downloaded and used to build containers. Docker hub also provides authentication, workflow tools like webhooks and build triggers, and privacy tools like private repositories for storing images you don’t want to share publicly.
If you have used GitHub, you can think of Docker hub as the GitHub specifically designed for Docker images.
Create an Account
To start using Docker hub, you need to create an account first. You can do that by either visiting the Docker hub homepage or by running:
If you use the CLI, credentials are stored in the
config.json file inside the
Docker Command Support
Docker hub provides a web interface to search, push, and pull repositories. Corresponding commands like
docker push are available if you’re using the Docker CLI.
search command can be used to find images by image name, user name, or description.
For example, you can run:
pull commands can be used to send or retrieve images from the Docker hub.
To pull an image, run:
Anyone can pull a public repository from the Docker hub. However, you need to be registered to
To push a repository to its registry, run:
For this to work, you need to have named an image (while building) or committed your container to a named image.
In this post, we took a hypothetical use case and learnt how to write a Dockerfile to make the development process easier. We also learnt about the Docker hub and various commands it supports.
Here is a brief recap of the steps we followed:
- Create a base folder to store anything related to the Dockerfile.
- Create an empty file, name it as Dockerfile and add instructions as per Redis installation flow.
- Build the Docker image and then create a container with the image.
- Upload the image to Docker Hub after creating an account.