DEV Community

Carlos Armando Marcano Vargas
Carlos Armando Marcano Vargas

Posted on • Originally published at carlosmv.hashnode.dev on

Dockerizing a Robyn App | Python

This is an article about how to create an image and run a container for a simple Robyn app using Docker. This article assumes that the reader knows what Docker, images and containers are.

The app will have one endpoint, that will send a "Hello, World!" message when the endpoint is hit. Then, we will create a dockerfile for the app and create the image using Docker. And finally, we will run the container.

Requirements

  • Docker installed

  • Python installed

Robyn

"Robyn is a fast, high-performance Python web framework with a Rust runtime. It has been designed to provide near-native Rust throughput while benefiting from the code being written in Python. Robyn is comparable to other products such as Flask, FastAPI, Django, and a web server of choice. One of the key advantages of Robyn is that it does not require an external web server for production, making it more efficient and streamlined".

Sanskar Jethi

Project structure

robyn-app/
    app.py
    requirements.txt
    dockerfile

Enter fullscreen mode Exit fullscreen mode

Building the app

For building the app, we need to create a virtual environment and installed Robyn.

We create a folder for our project, and inside the project's folder, we run the following commands with our console to create a virtual environment:

#Windows users
py -m venv venv
cd venv/Scripts
./activate

#Linux
python3 -m venv venv
source venv/bin/activate

Enter fullscreen mode Exit fullscreen mode

Then, we run pip install robyn, to install Robyn.

After Robyn is installed, we run pip freeze > requirements.txt command, to create a requirements.txt file.

requirements.txt

The requirements.txt file should look like this:

dill==0.3.6
multiprocess==0.70.14
nestd==0.3.1
robyn==0.33.0
watchdog==2.2.1

Enter fullscreen mode Exit fullscreen mode

app.py

from robyn import Robyn, status_codes

from robyn.robyn import Response

app = Robyn( __file__ )

@app.get("/")
async def hello():
    return Response(status_code=status_codes.HTTP_200_OK, headers={}, body="Hello, World!")

app.start(port=8000, url="0.0.0.0")

Enter fullscreen mode Exit fullscreen mode

With this code, we create a web server that listens on port 8000 and returns the string "Hello, World!" to all requests.

The app = Robyn( __file__ ) line creates a new Robyn application and loads the current file into it.

The @app.get("/") decorator defines a route that will be called when a request is made to the root of the application. The async def hello() function is the handler for this route. It returns a Response object, which contains the status code, headers, and body of the response.

The app.start(port=8000, url="0.0.0.0") line starts the web server and listens on port 8000. The 0.0.0.0 address means that the server will listen on all interfaces, so it can be accessed from any machine on the network.

To test the web server, We open a web browser and navigate to http://localhost:8000/. You should see the string "Hello, World!" displayed in the browser.

dockerfile

FROM python:3.11

RUN mkdir /code
WORKDIR /code
RUN pip install --upgrade pip
COPY requirements.txt /code/

RUN pip install -r requirements.txt
COPY . /code/

EXPOSE 8000

CMD ["python", "app.py", "0.0.0.0:8000"]

Enter fullscreen mode Exit fullscreen mode

The FROM python:3.11 line tells Docker to use the official Python 3.11 image as the base for the new image.

The RUN mkdir /code line creates a directory called /code in the new image.

The WORKDIR /code line sets the working directory of the new image to /code.

The RUN pip install --upgrade pip line updates the pip package to the latest version.

The COPY requirements.txt /code/ line copies the requirements.txt file into the /code directory of the new image.

The RUN pip install -r requirements.txt line installs the Python packages listed in the requirements.txt file.

The COPY . /code/ line copies the current directory into the /code directory of the new image.

The EXPOSE 8000 line tells Docker that the new image exposes port 8000.

The CMD ["python", "app.py", "0.0.0.0:8000"] line tells Docker to run the app.py file when the image is started. The 0.0.0.0 address means that the server will listen on all interfaces, so it can be accessed from any machine on the network.

We run docker build -t robyn-app . command in our console to build the image.

After the image building is completed, we run the following command in our console docker run -dp 8000:8000 robyn-app to run the image.

Also, we can go to Docker Desktop and click on the play button.

If there are no errors or problems, we can see the logs of our Robyn app in the console of Docker Desktop. And we should see a "Hello, World!" message displayed in the web browser when we navigate to http://localhost:8000/.

Conclusion

In this article, we have shown you how to dockerize a Robyn web server with Docker. We have shown you how to create a Dockerfile, build an image, and run a container.

Docker is a powerful tool that can be used to improve the development, deployment, and management of applications. By dockerizing your Robyn web server, you can make it easier to deploy to production, share with others, and scale.

Resources

Top comments (0)