Create Your First Docker App

Jack Paczos
Get that Data!
Published in
4 min readJan 24, 2024

This is a guide to containerize your first docker app.

Docker Image

If you don’t know what Docker Images or Containers are, you might want to read this first.

Agenda:

1. The App
2. Image Layers
3. Creating Dockerfile
4. Building Image
5. Running Container



1. The App


For this guide we are going to use a very simple Flask App just to demonstrate the concept.

First, create a folder where you want to place your app eg. (app).

Then, get Flask.

pip install Flask

Afterwards, create a file called app.py and paste this inside:

from flask import Flask

app = Flask(__name__)

@app.route('/')
def hello():
return "Hi there Flask is wokring"

app.run(debug=True, host='0.0.0.0', port=4050)

This is a super simple app which runs on your local port 4050 and just returns a sentence to confirm that it is working.

You can open your port here or type localhost:4050 in the browser.
And you should see this:

Hi there Flask is wokring

This means our Flask app is running locally.

Now you can stop the app and change this line to return:

return “Hi there Docker is wokring”

Important to note is, that inside the same directory, you should have a file called requirements.txt where all the dependencies are.
In this example it would look like that, since we need to use Flask.

Flask==2.3.2

2. Image Layers

By now you should now what images are. In a nutshell they are blueprints for containers. However those images consist of multiple layers.

Layers may include the following elements:


- runtime environment (version of programming language)
- dependencies (libraries)
- application code
- configurations (eg. environment variables)
- commands

Layers of Docker Image

To crate an Image for our app. We need to create a Dockerfile.

Dockerfile — a file which builds the image by adding layer after layer.

So lets create a file called “Dockerfile” with no extension.

app-structure

Now we can step by step enter the Layers we need.

First we need the Base Layer, which containers the Parent Image. The Parent Image is an Image that contains the OS and the runtime environment.

Let’s navigate to Docker Hub and search for the programming language we need in our case it’s python. Since we have a Flask App. So we can type the base layer in our dockerfile. We do not need to pull it. Docker will do this automatically for us.

FROM python:3.11-alpine

Now we are going to add the following things.

WORKDIR — we will run commands later in the file, thus we want to be in the correct directory (root app directory)

COPY — copies one file into another. In this case we copy the our dependencies from a requirements.txt file into our container (second dot is container path).

RUN — this command is used to run commands. We run a command to install the necessary requirements

COPY — the second copy. Copies our files from our directory into the container. So we have the app code we need.

EXPOSE — tells us on which port the application should be accessible. It does not necessarily run the app on this port

CMD — sets the default commands to run then container

FROM python:3.11-alpine
WORKDIR /
COPY ./requirements.txt .
RUN pip install -r ./requirements.txt
COPY . .
EXPOSE 4050
CMD ["python", "app.py"]

The Dockerfile should look like this.

Now we are ready and our app folder should look like this:

.
├── Dockerfile
├── app.py
└── requirements.txt
1 directory, 3 files

3. Building Image

Now we can build our image running this command.

docker build -t image1 .

-t — flag used to tag the image with a name

image1 — name of our image. you can name it however you want

. — is the path where the image will be built

If run successfully we can now see our image in docker desktop (docker app)

image inside docker desktop

4. Run Container

All is left to do, is to run our container. We can achieve this comfortably by using the GUI of docker desktop.

You can tun this image by clicking on the arrow.

You can specify a name for the container and the port on which you would like to run our App and leave the rest of the options empty.

Ensure nothing else is running on this port.

If you run it now and visit localhost:4050.

Voila. Now we can see that our app is running inside the container.

Hi there Docker is wokring

--

--