# Building your own Flask web service In this chapter, we will build our own web service using the Flask framework, dockerize it and register it in the registry. ## Requirements You will require `docker` in this example. You can find installation instructions [here](https://docs.docker.com/engine/install/). For example, on Ubuntu you can install it with: ```bash curl -fsSL https://get.docker.com -o get-docker.sh sudo sh get-docker.sh ``` To create, build and register docker image you don't need a compiled hscloud environment, howerver you will need it to deploy the image to kubernetes. ## Steps We create a simple Flask application: ```py # app.py from flask import Flask app = Flask(__name__) @app.route('/') def hello_world(): return 'Hello, World!' if __name__ == '__main__': app.run() ``` We can check if the application works by running it. First we start the virtual environment and install Flask: ```bash # for Linux and macOS environments python3 -m venv .venv source .venv/bin/activate ``` ```bash # for Windows environments py -m venv .venv .venv\bin\Activate.bat ``` After that: ```bash pip3 install flask python3 app.py ``` Now we create `Dockerfile` file for our application: ```Dockerfile # Use an official Python runtime as a base image FROM python:3.12.0-bullseye # Set the working directory in docker WORKDIR /app # Copy the current directory contents into the container at /app COPY . /app # Install any needed packages specified in requirements.txt RUN pip install --upgrade pip && \ pip install flask gunicorn # Set environment variables ENV FLASK_APP=app.py ENV FLASK_RUN_HOST=0.0.0.0 # Expose port 5000 for the Flask app to listen on EXPOSE 5000 # Define command to run the application using gunicorn CMD ["gunicorn", "-w", "4", "-b", ":5000", "app:app"] ``` We can test if the application works in Docker: ```bash docker build -t myflask:2137 . # Build the image docker run -p 5000:5000 myflask:2137 # Run the container ``` We can check if the application works by opening the browser at `http://localhost:5000`. ## Pushing the image to the registry First, we need to authenticate to the registry. That will require visiting the link in a web browser: ``` https://registry.k0.hswaw.net ``` You should get command similar to: ``` docker login -u $hs_username -p *************** https://registry.k0.hswaw.net ``` Run it in your environment. Now we need to create local server for our docker image: ```bash # create local server for our docker image. Replace $hs_username with your sso username docker tag myflask:2137 registry.k0.hswaw.net/$hs_username/myflask:2137 docker push registry.k0.hswaw.net/$hs_username/myflask:2137 ``` To remove the image from the registry, you can use: ```bash # remove image from registry. Replace $hs_username with your sso username docker rmi registry.k0.hswaw.net/$hs_username/myflask:2137 ``` ## Next step [Deploying Flask web app](deploying-flask-web-app.md)