Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: page for docker #467

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 60 additions & 49 deletions docs/docs/deploy-with-docker.md
Original file line number Diff line number Diff line change
@@ -1,88 +1,99 @@
# Deploy with Docker

You can use Docker to deploy Streamsync anywhere. If you're an experienced Docker user, you may want to go straight to the provided Dockerfile.
You can use Docker to deploy Writer anywhere. If you're an experienced Docker user, you may want to go straight to the provided Dockerfile.

## Creating a Docker image

### Setting up

- Make sure you have Docker installed.
- Open a terminal and navigate to your app's folder.
- Create a `pyproject.toml` using `poetry init` and install `streamsync` using `poetry add streamsync`
- If your project is not already set up, run `writer create <path>` to initialize your project. This command sets up all necessary files, such as `pyproject.toml`, `main.py`, and `ui.json`, and uses Poetry for dependency management.

### Creating a Dockerfile
### Why Poetry is Used in Writer Framework

Poetry ensures consistent, reproducible environments, simplifies dependency management, and aligns with the Writer Framework’s needs, making it an ideal tool for Docker deployments.

A Dockerfile is a file with instructions that tell Docker how to build your image. It must be named `Dockerfile`.
### Creating a Dockerfile

You can use the following as-is, or as a starting point. It should be saved in your app's folder, together with `main.py` and `ui.json`.
A Dockerfile contains instructions for building a Docker image. Save the following Dockerfile in your app's folder:

```docker
# Use an official Python runtime as a parent image
FROM python:3.10-bullseye

# Update the package repository and install required dependencies
RUN apt-get update -y && mkdir /app
RUN apt-get install build-essential cmake python3-dev -y
COPY . /app

# Set the working directory in the container to /app
WORKDIR /app

# Copy the current directory contents into the container at /app
COPY . /app

# Install Poetry
RUN pip3 install poetry
RUN poetry config virtualenvs.create false
RUN poetry install --only main
ENTRYPOINT [ "streamsync", "run" ]
EXPOSE 8080
CMD [ ".", "--port", "8080", "--host", "0.0.0.0" ]
```

::: tip This Dockerfile is just a guideline
It uses an official Python base image.
If you're a Docker expert, feel free to work on your own `Dockerfile`. Streamsync is, after all, a standard Python package.
:::
# Configure Poetry to not create a virtual environment
RUN poetry config virtualenvs.create false

### Building the Docker image
# Install dependencies specified in pyproject.toml
RUN poetry install --only main

To build the image, write `docker build . -t ` followed by an image tag, which you're free to choose and will locally identify your image.
# Make port 8080 available to the world outside this container
EXPOSE 8080

```sh
docker build . -t my_streamsync_app
# Run Writer when the container launches
ENTRYPOINT [ "writer", "run" ]
CMD [ ".", "--port", "8080", "--host", "0.0.0.0" ]
```

::: warning Platform considerations
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey why was this removed, has it stopped being a problem?

By default, Docker builds images in the architecture it's being run on. If you're working with an ARM computer, such as a Mac M2 or a Raspberry Pi, Docker will build an ARM image. Most cloud services will only accept x86 images. You can use another computer (or virtual machine) to build the image, or you can use [Docker buildx](https://docs.docker.com/build/building/multi-platform/).
:::
### Building the Docker image

## Publishing your Docker image
To build the image, run the `docker build` command in your terminal:

Once your Docker image has been built, you can publish it to a registry. This is a place where Docker images are stored and made available to other services.
1. **Navigate to your project directory**:
```sh
cd path/to/your/app
```

We recommend using Docker Hub; it has a generous free tier, it's very easy to set up and it's widely supported. However, you can choose to use another service such as Azure Container Registry. To use Docker Hub, you'll need to sign up for an account.
2. **Build the Docker image**:
```sh
docker build . -t my_framework_app
```

You can push your image using the following commands.
### Running the Docker Container

```sh
# Login to Docker Hub
docker login
Once the image is built, you can run a container based on that image:

# Push the image
# Replace "my_streamsync_app" for the tag you've previously chosen
# Replace "my_user" for your user on Docker Hub
docker tag my_streamsync_app:latest my_user/my_streamsync_app:latest
docker push my_user/my_streamsync_app:latest
```
1. **Run the container**:
```sh
docker run -p 8080:8080 my_framework_app
```

If your image is public, anyone can now run the following command and start the app. It's important to bind the port to a port in the host machine. The command below binds port 8080 in the Docker image to port 8080 in the host.

```sh
docker run -p 8080:8080 my_user/my_streamsync_app
```
2. **Access the application**:
Open your web browser and go to [http://localhost:8080](http://localhost:8080) to see your app running.

Go on your browser to [http://localhost:8080](http://localhost:8080) to check everything is working as expected.
### Publishing Your Docker Image

## Deploying your Docker image
To publish your Docker image to a registry like Docker Hub:

As mentioned earlier, once the image is a registry, it can be spun up by others. After trying a few options, we recommend using Google Cloud Run. Its free tier is generous and SSL works out of the box.
1. **Login to Docker Hub**:
```sh
docker login
```

![Run and Share - Google Cloud Run](./images/deploy-with-docker.google-cloud-run.png)
2. **Tag your image**:
```sh
docker tag my_framework_app:latest my_user/my_framework_app:latest
```

Cloud Run can be configured in just one page. It takes the image from a registry and makes it available via a URL, with SSL enabled by default. We recommend the following settings:
3. **Push your image to Docker Hub**:
```sh
docker push my_user/my_framework_app:latest
```

- Minimum 0 instances, maximum 4 instances. Unless your app needs to serve several thousands of users.
- Request timeout to the maximum allowed and _Session Affinity_ enabled. This ensures that WebSockets connections are not unnecessarily dropped.
- 2GB of memory and 2 vCPUs. This will likely be enough to comfortably run a simple app. You can probably get away with much less (512MB of memory and 1vCPU), if your app isn't too demanding and you don't expect much traffic.
### Deploying Your Docker Image

We recommend using Google Cloud Run to deploy your Docker image, which offers a generous free tier and SSL out of the box. Follow the Google Cloud Run documentation to deploy your Docker image and make your app available on the web.