Creating Docker Images With Docker Commit and Dockerfiles

Leave your reply

Introduction

Once you have learned the basics of working with Docker, the next step is to learn how to create and save your own images.

There are two ways to create a Docker image: manually using the docker commit command, or automatically using a Dockerfile.

Requirements

  • A 1&1 IONOS Linux Cloud Server
  • Docker installed and running
  • Basic familiarity with Docker functionality and commands

For more information on installing and using Docker, consult our other articles:

Safely Working With Docker Without Using Sudo

The Docker daemon runs as root, which means that users will need to use sudo to run Docker commands.

To avoid having to use sudo for every Docker command, simply add your user(s) to the docker group with the command:

usermod -aG docker [username]

For example, by adding the user jdoe to the Docker group, this user will no longer have to use sudo for every Docker command. To add the user to the group, use the command:

usermod -aG docker jdoe

Creating an Image Manually With "docker commit"

The easiest way to begin is to create a container from a pre-existing image, make your changes, then save this container as an image. For this tutorial we will create a container from the official CentOS 7 image, install the MySQLdb Python module, then save this new container as an image using docker commit.

The official CentOS 7 image is named centos. You can download it from the Docker registry with the command:

sudo docker pull centos

Note: If you prefer to use Ubuntu, simply substitute an Ubuntu image instead.

Begin by launching a container named python-with-mysql from the centos image with the command:

sudo docker run -it --name python-with-mysql centos bash

Once you are at the command prompt in the new container, install the MySQLdb Python module with the command:

yum -y install MySQL-python

Once the installation is complete, use CTRL-pCTRL-q to exit the container.

Next, save the container as an image with the docker commit command, which has the following syntax:

sudo docker commit -m "[build notes]" -a "creator info" [container name or ID] [name of image]:[version tag]
  • Build notes: A brief explanation of the changes made to this image.
  • Creator info: Your name and (if applicable) contact info.
  • Container name or ID: You can find this information with the command sudo docker ps -a.
  • Name of image: Give your image a short but descriptive name.
  • Optional: Version tag: The version number of your image. You can either specify a number (like v1, v2, v3, etc.) or you can use "latest." If you leave off the version tag, "latest" is assumed.

To save the container python-with-mysql as an image named python/mysql, use the command:

sudo docker commit -m "Added MySQL-python module" -a "J Doe" python-with-mysql python/mysql

To run a container from this new image, use the command:

sudo docker run -it python/mysql /bin/bash

Creating an Image Automatically With a Dockerfile

Creating an image manually with docker commit works well, but it is a cumbersome process. It is also easier to share a Dockerfile with your team, as opposed to sharing a list of commands they need to run. Although docker commit works well for some uses, it is more practical in the long run to use a Dockerfile.

A Dockerfile contains a set of instructions for building a new image. For this tutorial we will follow the same process as the previous section: we will create a container from the official CentOS 7 image, and install the MySQLdb Python module.

The official CentOS 7 image is named centos. You can download it from the Docker website with the command:

sudo docker pull centos

Note: If you prefer to use Ubuntu, simply substitute an Ubuntu image instead.

First, create a new directory for your Dockerfile:

mkdir python-mysql

This directory is where you will put any necessary files to be included in the build. It is important to begin by creating a new directory, because everything inside that directory will be transferred to the Docker daemon when you run the build command.

Next, create a file named Dockerfile inside the python-mysql directory. Note that the file name must be capitalized.

cd python-mysql
sudo nano Dockerfile

This file will contain all of your build commands. A full list of the available commands and their usage can be found here on the Docker website.

The basic contents of the Dockerfile are straightforward:

  • Any line beginning with a # will be skipped. Use this for comments.
  • The standard format is: INSTRUCTION statement, with the instruction in all-caps.

Put the following into your Dockerfile:

# The source image to start with
FROM centos

# Your contact info
MAINTAINER J Doe <jdoe@example.com>

# Run a command inside the image
RUN yum -y install MySQL-python

Save and exit the file.

To build an image named python/mysql/dockerfile from this Dockerfile, use the command:

sudo docker build -t python/mysql/dockerfile:v1 .

For the Dockerfile we are specifying the version v1 because of the expectation that this image will be shared and/or updated in the future.

To launch a container named python-with-mysql-from-dockerfile from the python/mysql/dockerfile image, use the command:

sudo docker run --name python-with-mysql-from-dockerfile -it python/mysql/dockerfile:v1 bash