Create and Share Your First Docker Image

8 Oct 2015

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

Redis

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:

$ mkdir Redis_Docker_Image
$ cd Redis_Docker_Image
$ sudo touch 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.

# Set the base image
FROM ubuntu
# Dockerfile author / maintainer 
MAINTAINER Name <email.id@here> 

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 RUN instruction:

# Update application repository list and install the Redis server. 
RUN apt-get update && apt-get install -y redis-server

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.

# Expose default port
EXPOSE 6379
# Set the default command
ENTRYPOINT ["/usr/bin/redis-server"]

This completes the Dockerfile for your Redis server.

Final Look

You can now save the file. Here’s how it should look:

# Set the base image
FROM ubuntu

# Dockerfile author / maintainer 
MAINTAINER Name <email.id@here> 

# Update application repository list and install the Redis server. 
RUN apt-get update && apt-get install -y redis-server

# Expose default port
EXPOSE 6379

# Set the default command
ENTRYPOINT ["/usr/bin/redis-server"]

Build the Docker Image

Now that the Dockerfile is ready, let’s create the image.

Run this command:

$ docker build -t redis-server .

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:

$ docker run --name redis_instance -t redis-server

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.

redis-server container

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:

$ docker login

If you use the CLI, credentials are stored in the config.json file inside the .docker folder.

Docker Command Support

Docker hub provides a web interface to search, push, and pull repositories. Corresponding commands like docker search, docker pull, docker push are available if you’re using the Docker CLI.

The search command can be used to find images by image name, user name, or description.

For example, you can run:

$ docker search <keyword>

The push and pull commands can be used to send or retrieve images from the Docker hub.

To pull an image, run:

$ docker pull <user-name>/<repo-name>

Anyone can pull a public repository from the Docker hub. However, you need to be registered to push.

To push a repository to its registry, run:

$ docker push <user-name>/<image-name>

For this to work, you need to have named an image (while building) or committed your container to a named image.

Conclusion

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.

Posted in Tutorial, Docker

triangle square circle

Did you enjoy this post?