{ "cells": [ { "cell_type": "raw", "metadata": { "tags": [] }, "source": [ "---\n", "sidebar_label: \"Custom Containers\"\n", "sidebar_position: 11\n", "---" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": { "tags": [] }, "source": [ "# How To Work With Custom Containers in Bacalhau\n", "\n", "\n", "[![stars - badge-generator](https://img.shields.io/github/stars/bacalhau-project/bacalhau?style=social)](https://github.com/bacalhau-project/bacalhau)\n", "\n", "Bacalhau operates by executing jobs within containers. This example shows you how to build and use a custom docker container.\n", "\n", "## TD;LR\n", "Running Custom Containers in Bacalhau\n", "\n", "## Prerequisite\n", "\n", "- To get started, you need to install the Bacalhau client, see more information [here](https://docs.bacalhau.org/getting-started/installation)\n", "- This example requires Docker. If you don't have Docker installed, you can install it from [here](https://docs.docker.com/install/). Docker commands will not work on hosted notebooks like Google Colab, but the Bacalhau commands will.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [ "remove_cell" ] }, "outputs": [], "source": [ "!command -v bacalhau >/dev/null 2>&1 || (export BACALHAU_INSTALL_DIR=.; curl -sL https://get.bacalhau.org/install.sh | bash)\n", "path=!echo $PATH\n", "%env PATH=./:{path[0]}" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": { "tags": [] }, "source": [ "## Running Containers in Bacalhau\n", "\n", "You're probably used to running docker commands to run a container." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [ "skip-execution" ] }, "outputs": [], "source": [ "%%bash\n", "docker run docker/whalesay cowsay sup old fashioned container run" ] }, { "cell_type": "markdown", "metadata": { "tags": [] }, "source": [ "Bacalhau uses a syntax that is similar to docker and you can use the same containers. The main difference is that input and output data is passed to the container via IPFS, to enable planetary scale. In this example, it doesn't make too much difference except that we need to download the stdout.\n", "\n", "The `--wait` flag tells Bacalhau to wait for the job to finish before returning. This is useful in interactive sessions like this, but you would normally allow jobs to complete in the background and use the `list` command to check on their status.\n", "\n", "Another difference is that by default Bacalhau overwrites the default entrypoint for the container so you have to pass all shell commands as arguments to the `run` command after the `--` flag." ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "### Running a Bacalhau Job" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [] }, "outputs": [], "source": [ "%%bash --out job_id\n", "bacalhau docker run --wait --id-only docker/whalesay -- bash -c 'cowsay hello web3 uber-run'" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "When a job is submitted, Bacalhau prints out the related `job_id`. We store that in an environment variable so that we can reuse it later on." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [ "remove_cell" ] }, "outputs": [], "source": [ "%env JOB_ID={job_id}" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "You can download your job results directly by using `bacalhau get`. Alternatively, you can choose to create a directory to store your results. In the command below, we created a directory and downloaded our job output to be stored in that directory." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "Y06KRWLDIitN", "tags": [] }, "outputs": [], "source": [ "%%bash\n", "rm -rf results && mkdir -p results\n", "bacalhau get ${JOB_ID} --output-dir results" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "Viewing your job output" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [] }, "outputs": [], "source": [ "%%bash\n", "cat ./results/stdout" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": { "tags": [] }, "source": [ "## Building Your Own Custom Container For Bacalhau\n", "\n", "To use your own custom container, you must publish the container to a container registry that is accessible from the Bacalhau network. At this time, only public container registries are supported.\n", "\n", "To demonstrate this, you will develop and build a simple custom container that comes from an old docker example. I remember seeing cowsay at a Docker conference about a decade ago. I think it's about time we brought it back to life and distribute it across the Bacalhau network." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [] }, "outputs": [], "source": [ "%%writefile cod.cow\n", "$the_cow = <<\"EOC\";\n", " $thoughts\n", " $thoughts\n", " ,,,,_\n", " ┌Φ▓╬▓╬▓▓▓W @▓▓▒,\n", " ╠▓╬▓╬╣╬╬▓╬▓▓ ╔╣╬╬▓╬╣▓,\n", " __,┌╓═╠╬╠╬╬╬Ñ╬╬╬Ñ╬╬¼,╣╬╬▓╬╬▓╬▓▓▓┐ ╔W_ ,φ▓▓\n", " ,«@▒╠╠╠╠╩╚╙╙╩Ü╚╚╚╚╩╙╙╚╠╩╚╚╟▓▒╠╠╫╣╬╬╫╬╣▓, _φ╬▓╬╬▓, ,φ╣▓▓╬╬\n", " _,φÆ╩╬╩╙╚╩░╙╙░░╩`=░╙╚»»╦░=╓╙Ü1R░│░╚Ü░╙╙╚╠╠╠╣╣╬≡Φ╬▀╬╣╬╬▓▓▓_ ╓▄▓▓▓▓▓▓╬▌\n", " _,φ╬Ñ╩▌▐█[▒░░░░R░░▀░`,_`!R`````╙`-'╚Ü░░Ü░░░░░░░│││░╚╚╙╚╩╩╩╣Ñ╩╠▒▒╩╩▀▓▓╣▓▓╬╠▌\n", " '╚╩Ü╙│░░╙Ö▒Ü░░░H░░R ▒¥╣╣@@@▓▓▓ := '` `░``````````````````````````]▓▓▓╬╬╠H\n", " '¬═▄ `\\░╙Ü░╠DjK` Å»»╙╣▓▓▓▓╬Ñ -»` -` ` ,;╓▄╔╗∞ ~▓▓▓▀▓▓╬╬╬▌\n", " '^^^` _╒Γ `╙▀▓▓╨ _, ⁿD╣▓╬╣▓╬▓╜ ╙╬▓▓╬╬▓▓\n", " ```└ _╓▄@▓▓▓╜ `╝╬▓▓╙ ²╣╬▓▓\n", " %φ▄╓_ ~#▓╠▓▒╬▓╬▓▓^ ` ╙╙\n", " `╣▓▓▓ ╠╬▓╬▓╬▀`\n", " ╚▓▌ '╨▀╜\n", "EOC" ] }, { "cell_type": "markdown", "metadata": { "tags": [] }, "source": [ "Next, the Dockerfile adds the script and sets the entrypoint." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [] }, "outputs": [], "source": [ "%%writefile Dockerfile\n", "FROM debian:stretch\n", "RUN apt-get update && apt-get install -y cowsay\n", "# \"cowsay\" installs to /usr/games\n", "ENV PATH $PATH:/usr/games\n", "RUN echo '#!/bin/bash\\ncowsay \"${@:1}\"' > /usr/bin/codsay && \\\n", " chmod +x /usr/bin/codsay\n", "COPY cod.cow /usr/share/cowsay/cows/default.cow" ] }, { "cell_type": "markdown", "metadata": { "tags": [] }, "source": [ "Now let's build and test the container locally." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [ "skip-execution" ] }, "outputs": [], "source": [ "%%bash\n", "docker build -t ghcr.io/bacalhau-project/examples/codsay:latest . 2> /dev/null" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [ "skip-execution" ] }, "outputs": [], "source": [ "%%bash\n", "docker run --rm ghcr.io/bacalhau-project/examples/codsay:latest codsay I like swimming in data" ] }, { "cell_type": "markdown", "metadata": { "tags": [] }, "source": [ "Once your container is working as expected then you should push it to a public container registry. In this example, I'm pushing to Github's container registry, but we'll skip the step below because you probably don't have permission.Remember that the Bacalhau nodes expect your container to have a `linux/amd64` architecture." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [] }, "outputs": [], "source": [ "%%bash\n", "# docker buildx build --platform linux/amd64,linux/arm64 --push -t ghcr.io/bacalhau-project/examples/codsay:latest ." ] }, { "attachments": {}, "cell_type": "markdown", "metadata": { "tags": [] }, "source": [ "## Running Your Custom Container on Bacalhau\n", "\n", "Now we're ready to submit a Bacalhau job using your custom container. This code runs a job, downloads the results and prints the stdout.\n", "\n", ":::tip\n", "The `bacalhau docker run` command strips the default entrypoint, so don't forget to run your entrypoint in the command line arguments.\n", ":::" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [] }, "outputs": [], "source": [ "%%bash --out job_id\n", "bacalhau docker run \\\n", " --wait \\\n", " --id-only \\\n", " ghcr.io/bacalhau-project/examples/codsay:v1.0.0 \\\n", " -- bash -c 'codsay Look at all this data'" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "When a job is submitted, Bacalhau prints out the related `job_id`. We store that in an environment variable so that we can reuse it later on." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [ "remove_cell" ] }, "outputs": [], "source": [ "%env JOB_ID={job_id}" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "Download your job results directly by using `bacalhau get` command." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "Y06KRWLDIitN", "tags": [] }, "outputs": [], "source": [ "%%bash\n", "rm -rf results && mkdir -p results\n", "bacalhau get ${JOB_ID} --output-dir results" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "View your job output" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%%bash\n", "cat ./results/stdout" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3.9.7 64-bit ('3.9.7')", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.13" }, "orig_nbformat": 4, "vscode": { "interpreter": { "hash": "21fd917facdca5c02b7d24e32528f1b4e6711465b0262edbfffba943391e1222" } } }, "nbformat": 4, "nbformat_minor": 2 }