My world of technology

Tag: docker

Creating development environment using Docker Compose

Hello and welcome to the next entry in this series. Today I will cover creating development environment on local machine using Docker Compose. This way we can setup both API application and database server quickly and effortlessly.

For purpose of demonstration we can reuse the dockerfiles created in previous entries in the series (for postgres server and API app). We were already able to setup and run these containers separately, but using Docker Compose we can create both of them at the same time with just one command.

The important part is networking: we need to make sure that containers can „talk” to each other. For that let’s create separate network called „docker_network” utilising bridge type driver. There is also need to expose the ports; one thing changes compared to the previous setup. We don’t need to specify port forwarding for database server and API container can talk to database directly on port 5432 (default for Postgres). We still need to forward the requests to API app on different port than the one exposed by Flask container (5000).

I’ve also added some safety mechanism in form of healthcheck for database and „depends_on” clause for api which will cause the API to start only when database container is up and ready.

Putting this all together compose file looks the following:

services:
  db:
    container_name: postgres
    build: ./db/
    ports:
      - "5432"
    expose:
    - "5432"
    environment:
    - POSTGRES_USER=postgres
    - POSTGRES_PASSWORD=postgrespassword
    - POSTGRES_DB=postgres
    networks:
    - docker_network
    healthcheck:
      test: ["CMD", "pg_isready", "-U", $POSTGRES_USER, "-d", $POSTGRES_DB]
      interval: 1s
      timeout: 5s
      retries: 10
  api:
    container_name: rpgapp
    build: .
    ports:
      - "5005:5000"
    networks:
      - docker_network
    depends_on:
      db:
        condition: service_healthy
networks:
  docker_network:
    driver: bridge

Let’s give it a spin and start the environment using command:

docker compose up

Afterwards we can see in Docker desktop app that both containers are running as part of single compose stack:

To confirm that our app works let’s try to create a new character via API call:

We have successfully created environment consisting of database and application server with just one command!

That’s it for today, in my next entry I will cover creating Prometheus container for monitoring our application.

Deploying application in Azure Container Apps

In the previous blog entry we’ve created a Docker image and made sure that the containerised application runs properly. Now we are ready to host the application in Azure using Azure Container Apps.

Azure Container apps is serverless platform allowing us to maintain less infrastructure and focus only on development of application. This way there is no need to provision virtual machine; instead the container consists only of bare minimum to run the application including the Linux kernel, Python and required dependencies (such as Flask).

In free plan of Azure which I am using Microsoft provides 180,000 vCPU seconds, 360,000 GiB seconds, and 2 million requests which is plentiful for the needs of such simple application.

To create an Azure Container App navigate to portal.azure.com, find „Container Apps” in Azure Services and hit „Create button in upper left corner. You will be presented with an input form like the one below:

In order to create Container App you need to have valid Azure Subscription, and create a Resource group and Container App Environment (both can be created from this view and defaults work just fine in our scenario).The „Region” field should be preferably the one geographically closest to you. Name the container app to your liking, choose „Container image” as deployment source and hit „Next”.

In the „Container” tab choose „Docker Hub” as image source and Image type „Public”. Registry login server remains as default „docker.io”. Fill in name of your image(including version tag if you use it) in „Image and tag” field. In CPU and Memory you can choose the lowest resource tier, it is enough in case of this app. Hit continue to go to next tab.

On „Ingress” tab mark Ingress as enabled and go with „Accept traffic from anywhere” in Ingress traffic section. Insecure connections can be made allowed for the demo purpose. Don’t forget to specify target port as 5000 (default for Flask). You can go to last tab, Review + Create and if validation is passed hit „Create” button in the lower left corner of the page.

The deployment process can take a while, but after it is completed you should be presented with details page of your application, such as the one below:

Make sure that the Status of application is presented as „Running”. If that’s the case you can test the application by copying the application URL and running curl against it utilising our /character endpoint. In my case that would be:

curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X GET https://rpg-app.redfield-12c457b2.polandcentral.azurecontainerapps.io/character

Replace the URl with the one where your application is hosted. After query you should see output like the one below:

We’ve successfully received a list of our characters from the application deployed in Azure Container.

That’s it for today’s entry, in the next step we will automate the deployment process using Azure Devops pipeline!

Containerising the application

Hello and welcome to the second entry of my tech blog.

Now that we have built a simple API app, we need a place to run it and expose it to our users.

But first things first; in order to build a Docker container we first need a Dockerfile. Since we are deploying just a simple Flask application without any dependencies (such as database) this example is fairly simple. You can see the full Dockerfile below:

We utilise the latest Python version (as of writing this blog post on 22nd of December 2024), expose port 5000 (default for Flask), establish a working directory, install Flask, copy the content of our application there and specify command to run the app. All of it in just 6 lines of code, pretty neat, right?

Dockerfile alone is not enough, we need to build a container image out if it and upload it to container registry. For this I am using Docker Hub public repo.

In order to build an image, navigate to the folder containing Dockerfile and run following command:

docker buildx build -t imagename:version .

By using -t argument we specify a name of the image and version tag. The dot at the end means context of current directory. After execution you should see output similar to the one below (note it might take few minutes to build the image including pulling the python 3.12 base if it’s not in your cache):

Once the image is built, we can run the container instance based on it. To do so, execute the following command:

docker run imagename:version -p 5005:5000

Since our container instance is run locally we’ve utilised port forwarding and we redirect traffic from port 5005 on our local machine to port 5000 in the container instance. Now that container is created let’s make sure it is running. To do so run:

docker container list

Afterwards you should see output similar to the one below (name of the container is generated randomly). The important thing is the „STATUS” section, it should be showing „Up” (not „Exited” or „Created”).

Let’s query our /character endpoint to make sure that the app is indeed running. You can do it by running curl:

curl http://127.0.0.1:5005/character

You should receive the following output in the terminal:

Right now image is present only on our local machine. In order to use it in Azure we first need to push it to Docker Hub registry. To do so first make sure you are logged in Docker by running:

docker login

After that is done you can push the image to registry utilising this command:

docker push imagename:version

It might take a minute or two, during the process you should see output such as:

Last thing to check is the actual Docker Hub registry. Login to hub.docker.com, navigate to repositories section and you should fine your repo containing the image there. In my case it looks the following:

Here it is! Now we have our image in Docker repository ready to be used in a deployment process, which I will cover in the next blog post.

© 2025 KW Digital

Theme by Anders NorenUp ↑