DEV Community

Cover image for Creating our own Docker images

Creating our own Docker images

After learning the Docker basics, 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!


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 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).

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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.

Copy the code and paste it inside /opt/

cat > /opt/

import os
from flask import Flask

app = Flask(__name__)

def main():
    return "Welcome!"

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

def about():
    return "I'm just a small Flask app :)"

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

if __name__ == "__main__":
Enter fullscreen mode Exit fullscreen mode

Everything is set, time to run the server! flask run --host=
Enter fullscreen mode Exit fullscreen mode

Now we can visit the URL in our Browser:

Starting our Flask service

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
Enter fullscreen mode Exit fullscreen mode

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 /opt/
ENTRYPOINT FLASK_APP=/opt/ flask run --host=
Enter fullscreen mode Exit fullscreen mode

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:


docker build . -t david1707/app-flask-caminas
Enter fullscreen mode Exit fullscreen mode

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

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 Hub). 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
Enter fullscreen mode Exit fullscreen mode

Now anyone can use your image with:

docker run david1707/app-flask-caminas
Enter fullscreen mode Exit fullscreen mode

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

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.


Original post

Docker basics for beginners

Install Docker with 3 easy steps

GitHub: Flask App


Docker hub

Top comments (0)