{ "cells": [ { "cell_type": "markdown", "id": "78a149e6-ef3b-4b6e-a633-0c1d2eb2a7b1", "metadata": {}, "source": [ "# Setting up Docker for FastAPI and Jenkins CI-CD on Gentoo\n", "\n", "## Prerequisites:\n", "- Docker\n", "- Jenkins\n", "- FastAPI\n", "\n", "## Preparations.\n", "\n", "\n", "Create directory for your stuff, call it tech_project, enter it." ] }, { "cell_type": "code", "execution_count": null, "id": "4d35806b-a9f8-43b0-82fd-b9a669295562", "metadata": {}, "outputs": [], "source": [ "$ mkdir tech_project && cd tech_project" ] }, { "cell_type": "markdown", "id": "bdfd8575-625f-488b-aec3-73ce987086d2", "metadata": {}, "source": [ "Create virtual Python environment:" ] }, { "cell_type": "code", "execution_count": null, "id": "42b86b21-ffa1-4d22-af97-a130b76e04d0", "metadata": {}, "outputs": [], "source": [ "$ python -m venv venv" ] }, { "cell_type": "code", "execution_count": null, "id": "ba4a8e5c-5e31-4e67-9c8f-17c75b52f5e2", "metadata": {}, "outputs": [], "source": [ "You can activate it now with:" ] }, { "cell_type": "code", "execution_count": null, "id": "edaeb7c2-f19a-4a6b-bb5f-4c5fd93dcf60", "metadata": {}, "outputs": [], "source": [ "$ source venv/bin/activate" ] }, { "cell_type": "code", "execution_count": null, "id": "8ebfbc06-03a0-4380-a9aa-970aafdbde75", "metadata": {}, "outputs": [], "source": [ "(venv) - That's what should prepend your prompt now." ] }, { "cell_type": "markdown", "id": "bb6d9596-a6fd-4127-a626-e63851a3e38f", "metadata": {}, "source": [ "Create a requirements.txt file, check which versions of fastapi and pydantic you have and use those:" ] }, { "cell_type": "code", "execution_count": null, "id": "a199797a-73ea-4015-b58f-e87ead2ea3f0", "metadata": {}, "outputs": [], "source": [ "fastapi>=0.111\n", "pydantic>=2.7.3\n", "uvicorn>=0.30.1" ] }, { "cell_type": "markdown", "id": "25c4e82b-0685-400e-9f40-c171deefcc1f", "metadata": {}, "source": [ "Create a Dockerfile:" ] }, { "cell_type": "code", "execution_count": null, "id": "237c5063-5370-4be7-8e29-e0e9802077e6", "metadata": {}, "outputs": [], "source": [ "FROM python:3.12\n", "WORKDIR app/ .\n", "COPY requirements.txt .\n", "RUN pip install --no-cache-dir -r requirements.txt\n", "CMD [\"python\", \"main.py\"]" ] }, { "cell_type": "markdown", "id": "e2b41ff9-c9d3-4a84-bb22-0494990ef272", "metadata": {}, "source": [ "Start docker service (as root):" ] }, { "cell_type": "code", "execution_count": null, "id": "37b1ef3f-8ed1-4c40-9ce9-5e88843356f9", "metadata": {}, "outputs": [], "source": [ "# rc-service docker start" ] }, { "cell_type": "markdown", "id": "7df0e4c5-20f2-4367-be36-e43617dab0ed", "metadata": {}, "source": [ "Build docker image:" ] }, { "cell_type": "code", "execution_count": null, "id": "1220f1b3-1b2c-4bba-a51f-54bd1401b1ec", "metadata": {}, "outputs": [], "source": [ "$ docker build -t fastapi_tech_blog ." ] }, { "cell_type": "markdown", "id": "36881148-8ff4-48e6-80d9-8c53a05bb90b", "metadata": {}, "source": [ "Watch if warn you and build:" ] }, { "cell_type": "code", "execution_count": null, "id": "c962a037-75ef-4b22-b46a-f24da4994be4", "metadata": {}, "outputs": [], "source": [ "WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv\n", " ---> Removed intermediate container bb903c49b10d\n", " ---> 2a71d86268fd\n", "Step 5/7 : COPY . .\n", " ---> 9fdcdd3d46cb\n", "Step 6/7 : EXPOSE 8000\n", " ---> Running in e63e0e7b3ccc\n", " ---> Removed intermediate container e63e0e7b3ccc\n", " ---> 72d0d9f39af7\n", "Step 7/7 : CMD [\"python\", \"main.py\"]\n", " ---> Running in b49bc996f993\n", " ---> Removed intermediate container b49bc996f993\n", " ---> fb426f2a7e15\n", "Successfully built fb426f2a7e15\n", "Successfully tagged fastapi_tech_blog:latest" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.12.7" } }, "nbformat": 4, "nbformat_minor": 5 }