Dockerizing a Robyn App | Python
Published: 2023-07-06 18:14:17

---
title: Dockerizing a Robyn App | Python
published: true
date: 2023-06-20 17:35:46 UTC
tags: 
canonical_url: https://carlosmv.hashnode.dev/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

```

### 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

```

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

```

**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")

```

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/`](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"]

```

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`](http://app.py)`", "0.0.0.0:8000"]` line tells Docker to run the [`app.py`](http://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.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686707263122/ced0485e-e8de-498c-b930-772f4b7bc0ac.png)

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.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686706894123/e5d985f7-cb78-440b-a571-c5ebfd73e9ff.png)

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/`.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1686706687518/5663fb76-7cf6-4c0d-9324-1c7ad9463c47.png)

### 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

- [How to Dockerize a Flask Application](https://www.freecodecamp.org/news/how-to-dockerize-a-flask-app/)

- [Containerize an application](https://docs.docker.com/get-started/02_our_app/)