Creating our own Docker images
Tags: devops, docker, tutorial, beginners
Published: 2024-01-15 08:31:10


After learning the [Docker basics](https://letslearnabout.net/devops/docker-tutorial-beginners/), it is time to create our images. Creating our own custom Docker image allows us to have more control over application configurations, dependencies, security, and more. And it is easy and fun. Get ready for it!

<table><tbody><tr><td><strong><span style="text-decoration: underline;">Table of contents</span></strong></td></tr><tr><td><a href="#intro">Introduction</a></td></tr><tr><td><a href="#manually">Doing the process steps manually</a></td></tr><tr><td><a href="#creating-docker-image">Creating our Docker image</a></td></tr><tr><td><a href="#public-docker-image">Making public our Docker image</a></td></tr><tr><td><a href="#final-thoughts">Final thoughts</a></td></tr><tr><td><a href="#resources">Resources</a></td></tr></tbody></table>

## Introduction

Custom Docker images give us control over application configurations and dependencies, improving security, and performance. We can create a custom Docker image tailored to our own needs, with the dependencies we want, and the code we want.

For example, we want everybody in our dev team to use Ubuntu 20.04 with Apache and the code in one GitHub repo. We could create a custom Docker image, upload it and tell everybody in the dev team to work using that image.

No more dependencies problem, no more "I don't know how to install X" and, especially, no more "But it works on my computer".

We just need to create one image packaged with all we need, that executes every step we want.

## Doing the process steps manually

One tip before creating our own custom Docker image is to do the process manually: We get a clean Linux system (A Virtual Machine created with [Vagrant](https://letslearnabout.net/devops/vagrant-tutorial-beginners/) it is the easy way as you can use the image and discard it later) and we run the commands we want.

Using a process of trial and error, we install and configure everything we want until it is done. We will repeat these steps on our Docker image.

For this, of course, you need to have Docker installed (and, if not, here's how to [Install Docker with 3 easy steps](https://letslearnabout.net/devops/docker-tutorial-beginners/#installation)).

Let's start then!

We want to run a Flask web application inside an Ubuntu OS. The steps we want to do are:

- Install Ubuntu

- Update and upgrade Ubuntu

- Install Python and its installer

- Install Flask dependency

- Copy our Python code

- Run the Flask web application

So, let's start!

First, let's download a Docker image, then create a container using that image, and run the bash terminal:

```
docker run -it ubuntu bash
```

Now we are in the container's terminal as a root. Let's install all our dependencies:

```
apt update
apt upgrade -y
apt install -y python3
apt install python3-pip
pip install flask
```

Now we have everything we need to run our code. You can create a Flask app, search for one on GitHub that you want to use, or just use an [application I have created for this tutorial](https://raw.githubusercontent.com/david1707/flask-app/main/app.py).

Copy the code and paste it inside _/opt/app.py._


```
cat > /opt/app.py

import os
from flask import Flask

app = Flask(__name__)


@app.route("/")
def main():
    return "Welcome!"


@app.route("/How are you")
def how_are_you():
    return "I'm fine, how about you?"


@app.route("/about")
def about():
    return "I'm just a small Flask app :)"


@app.route("/info")
def info():
    return "I have been created just to pass Pau's subject."


if __name__ == "__main__":
    app.run()
```

Everything is set, time to run the server!

```
FLASK_APP=app.py flask run --host=0.0.0.0
```

Now we can visit the URL in our Browser:


![Starting our Flask service](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q0x877p0xxqdky22se75.png)

## Creating our Docker image

We have our server running perfectly. Now, we want to reproduce the same steps in a Docker image so we can distribute it.

First, close everything and check the container isn't running:

```
docker ps
```

Now, let's create a folder for us to work in. Inside, create a Dockerfile (A text document that contains all the commands we want to run) where we will create a script to do all the steps we did before:

```
mkdir app_flask
cd app_flask

sudo vim Dockerfile

FROM ubuntu
RUN apt update
RUN apt upgrade -y
RUN apt install -y python3 python3-pip
RUN pip install flask
COPY app.py /opt/app.py
ENTRYPOINT FLASK_APP=/opt/app.py flask run --host=0.0.0.0
```

Now we can run Docker build to run the script and create a Docker image. But before, as we see in the COPY line, we need to have our script in the same folder. Copy it or just download it with wget:

```
wget https://raw.githubusercontent.com/david1707/flask-app/main/app.py

docker build . -t david1707/app-flask-caminas
```

Remember to change the image's name, following the convention of `<YOUR_NAME>/<IMAGE_NAME>`.

After a few minutes (it took me 6), you have your custom Docker image:


![sudo docker images](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4vh6tgqmqqsvi3xofmfi.png)

## Making public our Docker image

Now we can distribute the Dockerfile and everybody will have the same image, with the same dependencies. But we can publish it in one Docker repo (for example, in [Docker](https://hub.docker.com/) [Hub](https://hub.docker.com/)). Very much like GitHub, everybody could download your image and use it on their computers.

You have to register in Docker Hub first, and then push it to the repo:

```
docker push david1707/app-flask-caminas
```

Now anyone can use your image with:

```

docker run david1707/app-flask-caminas
```

This will download the image, if it isn't on our computer, and run it. Now, we can visit the browser using the URL given:


![downloading our docker image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/avia12gdkp7iuk8l8vfq.png)

## Final thoughts

Creating our own Docker images is pretty easy. We just need the basics of using the terminal, then we can replicate all the steps in a Dockerfile to create our images, even distributing them via Docker Hub.

The benefits of using our own custom Docker files are:

- **Customization:** Creating and using your own Docker images allows for tailored configurations, ensuring that the image contains specific dependencies, settings, and optimizations that align with the requirements of your applications.

- **Security Compliance:** By building and managing your own Docker images, we have direct control over the inclusion and configuration of security measures.

- **Reduced Image Size:** Customizing Docker images allows for the elimination of unnecessary components, resulting in smaller image sizes. Smaller images lead to faster deployment times and more efficient resource utilization.

- **Flexibility and Specialized Tooling:** Building our own Docker images provides the flexibility to incorporate specialized tooling or unique requirements specific to your organization's workflows. This customization lets teams create and deploy applications with the exact environment they need for optimal performance and functionality.

## Resources
[Original post](https://letslearnabout.net/devops/creating-docker-image/)

[Docker basics for beginners](https://letslearnabout.net/devops/docker-tutorial-beginners)

[Install Docker with 3 easy steps](https://letslearnabout.net/devops/docker-tutorial-beginners/#installation)

[GitHub: Flask App](https://github.com/david1707/flask-app/)

[GitHub](https://github.com/david1707)

[Docker hub](https://hub.docker.com/)