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 auto­mat­ic­ally using a Dock­er­file.

Re­quire­ments

  • A IONOS Linux Cloud Server
  • Docker installed and running
  • Basic fa­mili­ar­ity with Docker func­tion­al­ity and commands

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
VPS Hosting
VPS hosting at un­beat­able prices on Dell En­ter­prise Servers
  • 1 Gbit/s bandwidth & unlimited traffic
  • Minimum 99.99% uptime & ISO-certified data centres
  • 24/7 premium support with a personal con­sult­ant

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 sub­sti­tute 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 in­stall­a­tion 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 ex­plan­a­tion of the changes made to this image.
  • Creator info: Your name and (if ap­plic­able) contact info.
  • Container name or ID: You can find this in­form­a­tion with the command sudo docker ps -a.
  • Name of image: Give your image a short but de­script­ive 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 auto­mat­ic­ally with a Dock­er­file

Creating an image manually with docker commit works well, but it is a cum­ber­some process. It is also easier to share a Dock­er­file 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 Dock­er­file.

A Dock­er­file contains a set of in­struc­tions 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 sub­sti­tute an Ubuntu image instead.

First, create a new directory for your Dock­er­file:

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 trans­ferred to the Docker daemon when you run the build command.

Next, create a file named Dock­er­file inside the python-mysql directory. Note that the file name must be cap­it­al­ised.

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 Dock­er­file are straight­for­ward:

  • Any line beginning with a # will be skipped. Use this for comments.
  • The standard format is: IN­STRUC­TION statement, with the in­struc­tion in all-caps.

Put the following into your Dock­er­file:

# 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/dock­er­file from this Dock­er­file, use the command:

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

For the Dock­er­file we are spe­cify­ing the version v1 because of the ex­pect­a­tion that this image will be shared and/or updated in the future.

To launch a container named python-with-mysql-from-dock­er­file from the python/mysql/dock­er­file image, use the command:

sudo docker run --name python-with-mysql-from-dockerfile -it python/mysql/dockerfile:v1 bash
Cloud Backup powered by Acronis
Mitigate downtime with total workload pro­tec­tion
  • Automatic backup and easy recovery
  • Intuitive schedul­ing and man­age­ment
  • AI-based threat pro­tec­tion
Go to Main Menu