# How to create Docker images

Once you have learned the [basics of working with Docker](https://www.ionos.com/digitalguide/server/configuration/docker-tutorial-installation-and-first-steps/ "Docker tutorial: Installation and first steps"), 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 IONOS Linux Cloud Server
- Docker installed and running
- Basic familiarity with Docker functionality 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:

```none
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:

```none
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:

```none
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:

```none
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:

```none
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:

```none
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:

```none
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:

```none
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:

```none
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:

```none
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.

```none
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](https://docs.docker.com/engine/reference/builder/ "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**:

```none
# 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:

```none
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:

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


This is a markdown version of: [https://www.ionos.com/digitalguide/server/know-how/creating-docker-images/](https://www.ionos.com/digitalguide/server/know-how/creating-docker-images/) for AI/LLM consumption.