From 6509bfea017034ee48bf2595d43f526b6232c888 Mon Sep 17 00:00:00 2001 From: Esteban Maya Cadavid Date: Wed, 10 Jul 2024 16:46:10 -0500 Subject: [PATCH 1/6] Add python docker example based on fastapi app --- content/language/python/containerize.md | 126 ++++++++++++------- content/language/python/deploy.md | 159 ++++++++++++++++++++---- content/language/python/develop.md | 141 +++++++++++++++++---- 3 files changed, 331 insertions(+), 95 deletions(-) diff --git a/content/language/python/containerize.md b/content/language/python/containerize.md index 9c4d169862f..4a26a76092d 100644 --- a/content/language/python/containerize.md +++ b/content/language/python/containerize.md @@ -18,12 +18,12 @@ This section walks you through containerizing and running a Python application. ## Get the sample application -The sample application uses the popular [Flask](https://flask.palletsprojects.com/) framework. +The sample application uses the popular [FastAPI](https://fastapi.tiangolo.com) framework. Clone the sample application to use with this guide. Open a terminal, change directory to a directory that you want to work in, and run the following command to clone the repository: ```console -$ git clone https://github.com/docker/python-docker +$ git clone https://github.com/estebanx64/python-docker-example ``` ## Initialize Docker assets @@ -35,9 +35,9 @@ feature to help streamline the process, or you can manually create the assets. {{< tabs >}} {{< tab name="Use Docker Init" >}} -Inside the `python-docker` directory, run the `docker init` command. `docker +Inside the `python-docker-example` directory, run the `docker init` command. `docker init` provides some default configuration, but you'll need to answer a few -questions about your application. For example, this application uses Flask to +questions about your application. For example, this application uses FastAPI to run. Refer to the following example to answer the prompts from `docker init` and use the same answers for your prompts. @@ -56,7 +56,7 @@ Let's get started! ? What application platform does your project use? Python ? What version of Python do you want to use? 3.11.4 ? What port do you want your app to listen on? 8000 -? What is the command to run your app? python3 -m flask run --host=0.0.0.0 --port=8000 +? What is the command to run your app? python3 -m uvicorn app:app --host=0.0.0.0 --port=5000 ``` {{< /tab >}} @@ -115,10 +115,10 @@ USER appuser COPY . . # Expose the port that the application listens on. -EXPOSE 8000 +EXPOSE 5000 # Run the application. -CMD python3 -m flask run --host=0.0.0.0 --port=8000 +CMD python3 -m uvicorn app:app --host=0.0.0.0 --port=5000 ``` Create a file named `compose.yaml` with the following contents. @@ -138,40 +138,7 @@ services: build: context: . ports: - - 8000:8000 - -# The commented out section below is an example of how to define a PostgreSQL -# database that your application can use. `depends_on` tells Docker Compose to -# start the database before your application. The `db-data` volume persists the -# database data between container restarts. The `db-password` secret is used -# to set the database password. You must create `db/password.txt` and add -# a password of your choosing to it before running `docker compose up`. -# depends_on: -# db: -# condition: service_healthy -# db: -# image: postgres -# restart: always -# user: postgres -# secrets: -# - db-password -# volumes: -# - db-data:/var/lib/postgresql/data -# environment: -# - POSTGRES_DB=example -# - POSTGRES_PASSWORD_FILE=/run/secrets/db-password -# expose: -# - 5432 -# healthcheck: -# test: [ "CMD", "pg_isready" ] -# interval: 10s -# timeout: 5s -# retries: 5 -# volumes: -# db-data: -# secrets: -# db-password: -# file: db/password.txt + - 5000:5000 ``` Create a file named `.dockerignore` with the following contents. @@ -212,17 +179,77 @@ Create a file named `.dockerignore` with the following contents. LICENSE README.md ``` +Create a file named `.gitignore` with the following contents. + +```text {collapse=true,title=".dockerignore"} +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ +cover/ + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm +__pypackages__/ + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ +``` + {{< /tab >}} {{< /tabs >}} -You should now have the following contents in your `python-docker` +You should now have the following contents in your `python-docker-example` directory. ```text -├── python-docker/ +├── python-docker-example/ │ ├── app.py │ ├── requirements.txt │ ├── .dockerignore +│ ├── .gitignore │ ├── compose.yaml │ ├── Dockerfile │ └── README.md @@ -231,34 +258,37 @@ directory. To learn more about the files, see the following: - [Dockerfile](../../reference/dockerfile.md) - [.dockerignore](../../reference/dockerfile.md#dockerignore-file) + - [.gitignore](https://git-scm.com/docs/gitignore) - [compose.yaml](../../compose/compose-file/_index.md) ## Run the application -Inside the `python-docker` directory, run the following command in a +Inside the `python-docker-example` directory, run the following command in a terminal. ```console $ docker compose up --build ``` -Open a browser and view the application at [http://localhost:8000](http://localhost:8000). You should see a simple Flask application. +Open a browser and view the application at [http://localhost:5000](http://localhost:5000). You should see a simple FastAPI application. In the terminal, press `ctrl`+`c` to stop the application. ### Run the application in the background You can run the application detached from the terminal by adding the `-d` -option. Inside the `python-docker` directory, run the following command +option. Inside the `python-docker-example` directory, run the following command in a terminal. ```console $ docker compose up --build -d ``` -Open a browser and view the application at [http://localhost:8000](http://localhost:8000). +Open a browser and view the application at [http://localhost:5000](http://localhost:5000). + +To see the OpenAPI docs you can go at [http://localhost:5000/docs](http://localhost:5000/docs) -You should see a simple Flask application. +You should see a simple FastAPI application. In the terminal, run the following command to stop the application. diff --git a/content/language/python/deploy.md b/content/language/python/deploy.md index de964d8a3d7..46eb3f4dbb6 100644 --- a/content/language/python/deploy.md +++ b/content/language/python/deploy.md @@ -6,7 +6,7 @@ description: Learn how to develop locally using Kubernetes ## Prerequisites -- Complete all the previous sections of this guide, starting with [Containerize a Python application](containerize.md). +- Complete all the previous sections of this guide, starting with [Use containers for python development](develop.md). - [Turn on Kubernetes](/desktop/kubernetes/#install-and-turn-on-kubernetes) in Docker Desktop. ## Overview @@ -15,12 +15,85 @@ In this section, you'll learn how to use Docker Desktop to deploy your applicati ## Create a Kubernetes YAML file -In your `python-docker-dev` directory, create a file named -`docker-python-kubernetes.yaml`. Open the file in an IDE or text editor and add +In your `python-docker-dev-example` directory, create a file named `docker-postgres-kubernetes.yaml`. Open the file in an IDE or text editor and add the following contents. Replace `DOCKER_USERNAME/REPO_NAME` with your Docker username and the name of the repository that you created in [Configure CI/CD for your Python application](configure-ci-cd.md). +```yaml +apiVersion: apps/v1 +kind: Deployment +metadata: + name: postgres + namespace: default +spec: + replicas: 1 + selector: + matchLabels: + app: postgres + template: + metadata: + labels: + app: postgres + spec: + containers: + - name: postgres + image: postgres + ports: + - containerPort: 5432 + env: + - name: POSTGRES_DB + value: example + - name: POSTGRES_USER + value: postgres + - name: POSTGRES_PASSWORD + valueFrom: + secretKeyRef: + name: postgres-secret + key: POSTGRES_PASSWORD + volumeMounts: + - name: postgres-data + mountPath: /var/lib/postgresql/data + volumes: + - name: postgres-data + persistentVolumeClaim: + claimName: postgres-pvc +--- +apiVersion: v1 +kind: Service +metadata: + name: postgres + namespace: default +spec: + ports: + - port: 5432 + selector: + app: postgres +--- +apiVersion: v1 +kind: PersistentVolumeClaim +metadata: + name: postgres-pvc + namespace: default +spec: + accessModes: + - ReadWriteOnce + resources: + requests: + storage: 1Gi +--- +apiVersion: v1 +kind: Secret +metadata: + name: postgres-secret + namespace: default +type: Opaque +data: + POSTGRES_PASSWORD: cG9zdGdyZXNfcGFzc3dvcmQ= # Base64 encoded password (e.g., 'postgres_password') +``` + +In your `python-docker-dev-example` directory, create a file named `docker-python-kubernetes.yaml`. + ```yaml apiVersion: apps/v1 kind: Deployment @@ -31,19 +104,32 @@ spec: replicas: 1 selector: matchLabels: - service: flask + service: fastapi template: metadata: labels: - service: flask + service: fastapi spec: containers: - - name: flask-service - image: DOCKER_USERNAME/REPO_NAME - imagePullPolicy: Always - env: - - name: POSTGRES_PASSWORD - value: mysecretpassword + - name: fastapi-service + image: technox64/python-docker-dev-example-test:latest + imagePullPolicy: Always + env: + - name: POSTGRES_PASSWORD + valueFrom: + secretKeyRef: + name: postgres-secret + key: POSTGRES_PASSWORD + - name: POSTGRES_USER + value: postgres + - name: POSTGRES_DB + value: example + - name: POSTGRES_SERVER + value: postgres + - name: POSTGRES_PORT + value: "5432" + ports: + - containerPort: 5001 --- apiVersion: v1 kind: Service @@ -53,20 +139,23 @@ metadata: spec: type: NodePort selector: - service: flask + service: fastapi ports: - - port: 8001 - targetPort: 8001 + - port: 5001 + targetPort: 5001 nodePort: 30001 ``` -In this Kubernetes YAML file, there are two objects, separated by the `---`: +In these Kubernetes YAML file, there are various objects, separated by the `---`: - A Deployment, describing a scalable group of identical pods. In this case, you'll get just one replica, or copy of your pod. That pod, which is described under `template`, has just one container in it. The container is created from the image built by GitHub Actions in [Configure CI/CD for your Python application](configure-ci-cd.md). + - A Service, which will define how the ports are mapped in the containers. + - A PersistentVolumeClaim, to define a storage that will be persistent through restarts for the database. + - A Secret, Keeping the database password as a example using secret kubernetes resource. - A NodePort service, which will route traffic from port 30001 on your host to port 8001 inside the pods it routes to, allowing you to reach your app from the network. @@ -75,16 +164,31 @@ To learn more about Kubernetes objects, see the [Kubernetes documentation](https ## Deploy and check your application -1. In a terminal, navigate to `python-docker-dev` and deploy your application to +1. In a terminal, navigate to `python-docker-dev-example` and deploy your database to Kubernetes. ```console - $ kubectl apply -f docker-python-kubernetes.yaml + $ kubectl apply -f docker-postgres-kubernetes.yaml ``` You should see output that looks like the following, indicating your Kubernetes objects were created successfully. - ```shell + ```console + deployment.apps/postgres created + service/postgres created + persistentvolumeclaim/postgres-pvc created + secret/postgres-secret created + ``` + + and let's deploy our python application + + ```console + kubectl apply -f docker-python-kubernetes.yaml + ``` + + You should see output that looks like the following, indicating your Kubernetes objects were created successfully. + + ```console deployment.apps/docker-python-demo created service/service-entrypoint created ``` @@ -97,9 +201,10 @@ To learn more about Kubernetes objects, see the [Kubernetes documentation](https Your deployment should be listed as follows: - ```shell + ```console NAME READY UP-TO-DATE AVAILABLE AGE - docker-python-demo 1/1 1 1 15s + docker-python-demo 1/1 1 1 48s + postgres 1/1 1 1 2m39s ``` This indicates all one of the pods you asked for in your YAML are up and running. Do the same check for your services. @@ -110,13 +215,14 @@ To learn more about Kubernetes objects, see the [Kubernetes documentation](https You should get output like the following. - ```shell - NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE - kubernetes ClusterIP 10.96.0.1 443/TCP 23h - service-entrypoint NodePort 10.99.128.230 8001:30001/TCP 75s + ```console + NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE + kubernetes ClusterIP 10.43.0.1 443/TCP 13h + postgres ClusterIP 10.43.209.25 5432/TCP 3m10s + service-entrypoint NodePort 10.43.67.120 5001:30001/TCP 79s ``` - In addition to the default `kubernetes` service, you can see your `service-entrypoint` service, accepting traffic on port 30001/TCP. + In addition to the default `kubernetes` service, you can see your `service-entrypoint` service, accepting traffic on port 30001/TCP and the internal `ClusterIP` `postgres` with the port `5432` open to accept connections from you python app. 3. In a terminal, curl the service. Note that a database was not deployed in this example. @@ -126,10 +232,11 @@ To learn more about Kubernetes objects, see the [Kubernetes documentation](https Hello, Docker!!! ``` -4. Run the following command to tear down your application. +4. Run the following commands to tear down your application. ```console $ kubectl delete -f docker-python-kubernetes.yaml + $ kubectl delete -f docker-postgres-kubernetes.yaml ``` ## Summary diff --git a/content/language/python/develop.md b/content/language/python/develop.md index 494da50b3e5..b152afd12a1 100644 --- a/content/language/python/develop.md +++ b/content/language/python/develop.md @@ -22,7 +22,7 @@ You'll need to clone a new repository to get a sample application that includes 1. Change to a directory where you want to clone the repository and run the following command. ```console - $ git clone https://github.com/docker/python-docker-dev + $ git clone https://github.com/estebanx64/python-docker-dev-example ``` 2. In the cloned repository's directory, manually create the Docker assets or run `docker init` to create the necessary Docker assets. @@ -36,19 +36,19 @@ You'll need to clone a new repository to get a sample application that includes ```console $ docker init Welcome to the Docker Init CLI! - + This utility will walk you through creating the following files with sensible defaults for your project: - .dockerignore - Dockerfile - compose.yaml - README.Docker.md - + Let's get started! - + ? What application platform does your project use? Python ? What version of Python do you want to use? 3.11.4 ? What port do you want your app to listen on? 8001 - ? What is the command to run your app? python3 -m flask run --host=0.0.0.0 --port=8001 + ? What is the command to run your app? python3 -m uvicorn app:app --host=0.0.0.0 --port=5001 ``` {{< /tab >}} @@ -107,10 +107,10 @@ You'll need to clone a new repository to get a sample application that includes COPY . . # Expose the port that the application listens on. - EXPOSE 8001 + EXPOSE 5001 # Run the application. - CMD python3 -m flask run --host=0.0.0.0 --port=8001 + CMD python3 -m uvicorn app:app --host=0.0.0.0 --port=5001 ``` Create a file named `compose.yaml` with the following contents. @@ -130,7 +130,7 @@ You'll need to clone a new repository to get a sample application that includes build: context: . ports: - - 8001:8001 + - 5001:5001 # The commented out section below is an example of how to define a PostgreSQL # database that your application can use. `depends_on` tells Docker Compose to @@ -204,6 +204,65 @@ You'll need to clone a new repository to get a sample application that includes LICENSE README.md ``` + Create a file named `.gitignore` with the following contents. + + ```text {collapse=true,title=".dockerignore"} + # Byte-compiled / optimized / DLL files + __pycache__/ + *.py[cod] + *$py.class + + # C extensions + *.so + + # Distribution / packaging + .Python + build/ + develop-eggs/ + dist/ + downloads/ + eggs/ + .eggs/ + lib/ + lib64/ + parts/ + sdist/ + var/ + wheels/ + share/python-wheels/ + *.egg-info/ + .installed.cfg + *.egg + MANIFEST + + # Unit test / coverage reports + htmlcov/ + .tox/ + .nox/ + .coverage + .coverage.* + .cache + nosetests.xml + coverage.xml + *.cover + *.py,cover + .hypothesis/ + .pytest_cache/ + cover/ + + # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm + __pypackages__/ + + # Environments + .env + .venv + env/ + venv/ + ENV/ + env.bak/ + venv.bak/ + ``` + {{< /tab >}} {{< /tabs >}} @@ -217,14 +276,17 @@ In the `compose.yaml` file, you need to uncomment all of the database instructio The following is the updated `compose.yaml` file. -```yaml {hl_lines="7-36"} +```yaml {hl_lines="7-43"} services: server: build: context: . ports: - - 8001:8001 + - 5001:5001 environment: + - POSTGRES_SERVER=db + - POSTGRES_USER=postgres + - POSTGRES_DB=example - POSTGRES_PASSWORD_FILE=/run/secrets/db-password depends_on: db: @@ -271,16 +333,18 @@ mysecretpassword Save and close the `password.txt` file. -You should now have the following contents in your `python-docker-dev` +You should now have the following contents in your `python-docker-dev-example` directory. ```text -├── python-docker-dev/ +├── python-docker-dev-example/ │ ├── db/ │ │ └── password.txt │ ├── app.py +│ ├── config.py │ ├── requirements.txt │ ├── .dockerignore +│ ├── .gitignore │ ├── compose.yaml │ ├── Dockerfile │ ├── README.Docker.md @@ -295,18 +359,50 @@ $ docker compose up --build Now test your API endpoint. Open a new terminal then make a request to the server using the curl commands: +Let's create an object with a post method + ```console -$ curl http://localhost:8001/initdb -$ curl http://localhost:8001/widgets +$ curl -X 'POST' \ + 'http://0.0.0.0:5001/heroes/' \ + -H 'accept: application/json' \ + -H 'Content-Type: application/json' \ + -d '{ + "id": 1, + "name": "my hero", + "secret_name": "austing", + "age": 12 +}' ``` You should receive the following response: ```json -[] +{ + "age": 12, + "id": 1, + "name": "my hero", + "secret_name": "austing" +} +``` + +Let's make a get request with the next curl command: + +```console +curl -X 'GET' \ + 'http://0.0.0.0:5001/heroes/' \ + -H 'accept: application/json' ``` -The response is empty because your database is empty. +You should receive the same response as above because it's the only one object we have in database. + +```json +{ + "age": 12, + "id": 1, + "name": "my hero", + "secret_name": "austing" +} +``` Press `ctrl+c` in the terminal to stop your application. @@ -319,14 +415,17 @@ Watch](../../compose/file-watch.md). Open your `compose.yaml` file in an IDE or text editor and then add the Compose Watch instructions. The following is the updated `compose.yaml` file. -```yaml {hl_lines="14-17"} +```yaml {hl_lines="17-20"} services: server: build: context: . ports: - - 8001:8001 + - 5001:5001 environment: + - POSTGRES_SERVER=db + - POSTGRES_USER=postgres + - POSTGRES_DB=example - POSTGRES_PASSWORD_FILE=/run/secrets/db-password depends_on: db: @@ -371,13 +470,13 @@ $ docker compose watch In a terminal, curl the application to get a response. ```console -$ curl http://localhost:8001 +$ curl http://localhost:5001 Hello, Docker! ``` Any changes to the application's source files on your local machine will now be immediately reflected in the running container. -Open `python-docker-dev/app.py` in an IDE or text editor and update the `Hello, Docker!` string by adding a few more exclamation marks. +Open `python-docker-dev-example/app.py` in an IDE or text editor and update the `Hello, Docker!` string by adding a few more exclamation marks. ```diff - return 'Hello, Docker!' @@ -387,7 +486,7 @@ Open `python-docker-dev/app.py` in an IDE or text editor and update the `Hello, Save the changes to `app.py` and then wait a few seconds for the application to rebuild. Curl the application again and verify that the updated text appears. ```console -$ curl http://localhost:8001 +$ curl http://localhost:5001 Hello, Docker!!! ``` From 6ccf441e61ce3ede5b0927a893fa610404e65d2b Mon Sep 17 00:00:00 2001 From: Esteban Maya Cadavid Date: Wed, 10 Jul 2024 16:55:02 -0500 Subject: [PATCH 2/6] Add gitignore example in python docs --- content/language/python/containerize.md | 61 ++++++++++++++++++++++- content/language/python/develop.md | 65 +++++++++++++++++++++++-- 2 files changed, 122 insertions(+), 4 deletions(-) diff --git a/content/language/python/containerize.md b/content/language/python/containerize.md index 4a26a76092d..7223942115e 100644 --- a/content/language/python/containerize.md +++ b/content/language/python/containerize.md @@ -59,6 +59,65 @@ Let's get started! ? What is the command to run your app? python3 -m uvicorn app:app --host=0.0.0.0 --port=5000 ``` +Create a file named `.gitignore` with the following contents. + +```text {collapse=true,title=".gitignore"} +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ +cover/ + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm +__pypackages__/ + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ +``` + {{< /tab >}} {{< tab name="Manually create assets" >}} @@ -181,7 +240,7 @@ README.md ``` Create a file named `.gitignore` with the following contents. -```text {collapse=true,title=".dockerignore"} +```text {collapse=true,title=".gitignore"} # Byte-compiled / optimized / DLL files __pycache__/ *.py[cod] diff --git a/content/language/python/develop.md b/content/language/python/develop.md index b152afd12a1..a50ae947f80 100644 --- a/content/language/python/develop.md +++ b/content/language/python/develop.md @@ -51,12 +51,71 @@ You'll need to clone a new repository to get a sample application that includes ? What is the command to run your app? python3 -m uvicorn app:app --host=0.0.0.0 --port=5001 ``` + Create a file named `.gitignore` with the following contents. + + ```text {collapse=true,title=".gitignore"} + # Byte-compiled / optimized / DLL files + __pycache__/ + *.py[cod] + *$py.class + + # C extensions + *.so + + # Distribution / packaging + .Python + build/ + develop-eggs/ + dist/ + downloads/ + eggs/ + .eggs/ + lib/ + lib64/ + parts/ + sdist/ + var/ + wheels/ + share/python-wheels/ + *.egg-info/ + .installed.cfg + *.egg + MANIFEST + + # Unit test / coverage reports + htmlcov/ + .tox/ + .nox/ + .coverage + .coverage.* + .cache + nosetests.xml + coverage.xml + *.cover + *.py,cover + .hypothesis/ + .pytest_cache/ + cover/ + + # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm + __pypackages__/ + + # Environments + .env + .venv + env/ + venv/ + ENV/ + env.bak/ + venv.bak/ + ``` + {{< /tab >}} {{< tab name="Manually create assets" >}} - + If you don't have Docker Desktop installed or prefer creating the assets manually, you can create the following files in your project directory. - + Create a file named `Dockerfile` with the following contents. ```dockerfile {collapse=true,title=Dockerfile} @@ -206,7 +265,7 @@ You'll need to clone a new repository to get a sample application that includes ``` Create a file named `.gitignore` with the following contents. - ```text {collapse=true,title=".dockerignore"} + ```text {collapse=true,title=".gitignore"} # Byte-compiled / optimized / DLL files __pycache__/ *.py[cod] From ded53959f7fef31fdcae22e77c8610e2ff8e2023 Mon Sep 17 00:00:00 2001 From: Esteban Maya Cadavid Date: Fri, 12 Jul 2024 14:15:26 -0500 Subject: [PATCH 3/6] =?UTF-8?q?=E2=9C=8F=EF=B8=8F=20Fix=20port=20mapping?= =?UTF-8?q?=20in=20docker=20init=20section?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- content/language/python/containerize.md | 2 +- content/language/python/develop.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/content/language/python/containerize.md b/content/language/python/containerize.md index 7223942115e..2036e50bac8 100644 --- a/content/language/python/containerize.md +++ b/content/language/python/containerize.md @@ -55,7 +55,7 @@ Let's get started! ? What application platform does your project use? Python ? What version of Python do you want to use? 3.11.4 -? What port do you want your app to listen on? 8000 +? What port do you want your app to listen on? 5000 ? What is the command to run your app? python3 -m uvicorn app:app --host=0.0.0.0 --port=5000 ``` diff --git a/content/language/python/develop.md b/content/language/python/develop.md index a50ae947f80..41eab319ffc 100644 --- a/content/language/python/develop.md +++ b/content/language/python/develop.md @@ -47,7 +47,7 @@ You'll need to clone a new repository to get a sample application that includes ? What application platform does your project use? Python ? What version of Python do you want to use? 3.11.4 - ? What port do you want your app to listen on? 8001 + ? What port do you want your app to listen on? 5001 ? What is the command to run your app? python3 -m uvicorn app:app --host=0.0.0.0 --port=5001 ``` From 16d07d20b737b75b0e052bb988238f285c39f60d Mon Sep 17 00:00:00 2001 From: Esteban Maya Cadavid Date: Sat, 20 Jul 2024 11:18:49 -0500 Subject: [PATCH 4/6] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Edit=20refactor=20to?= =?UTF-8?q?=20map=20container=20ports=20from=205000-5001=20to=208000-8001?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- content/language/python/containerize.md | 16 ++++++++-------- content/language/python/deploy.md | 8 ++++---- content/language/python/develop.md | 22 +++++++++++----------- 3 files changed, 23 insertions(+), 23 deletions(-) diff --git a/content/language/python/containerize.md b/content/language/python/containerize.md index 2036e50bac8..981208b0337 100644 --- a/content/language/python/containerize.md +++ b/content/language/python/containerize.md @@ -55,8 +55,8 @@ Let's get started! ? What application platform does your project use? Python ? What version of Python do you want to use? 3.11.4 -? What port do you want your app to listen on? 5000 -? What is the command to run your app? python3 -m uvicorn app:app --host=0.0.0.0 --port=5000 +? What port do you want your app to listen on? 8000 +? What is the command to run your app? python3 -m uvicorn app:app --host=0.0.0.0 --port=8000 ``` Create a file named `.gitignore` with the following contents. @@ -174,10 +174,10 @@ USER appuser COPY . . # Expose the port that the application listens on. -EXPOSE 5000 +EXPOSE 8000 # Run the application. -CMD python3 -m uvicorn app:app --host=0.0.0.0 --port=5000 +CMD python3 -m uvicorn app:app --host=0.0.0.0 --port=8000 ``` Create a file named `compose.yaml` with the following contents. @@ -197,7 +197,7 @@ services: build: context: . ports: - - 5000:5000 + - 8000:8000 ``` Create a file named `.dockerignore` with the following contents. @@ -329,7 +329,7 @@ terminal. $ docker compose up --build ``` -Open a browser and view the application at [http://localhost:5000](http://localhost:5000). You should see a simple FastAPI application. +Open a browser and view the application at [http://localhost:8000](http://localhost:8000). You should see a simple FastAPI application. In the terminal, press `ctrl`+`c` to stop the application. @@ -343,9 +343,9 @@ in a terminal. $ docker compose up --build -d ``` -Open a browser and view the application at [http://localhost:5000](http://localhost:5000). +Open a browser and view the application at [http://localhost:8000](http://localhost:8000). -To see the OpenAPI docs you can go at [http://localhost:5000/docs](http://localhost:5000/docs) +To see the OpenAPI docs you can go at [http://localhost:8000/docs](http://localhost:8000/docs) You should see a simple FastAPI application. diff --git a/content/language/python/deploy.md b/content/language/python/deploy.md index 46eb3f4dbb6..e606e99315b 100644 --- a/content/language/python/deploy.md +++ b/content/language/python/deploy.md @@ -129,7 +129,7 @@ spec: - name: POSTGRES_PORT value: "5432" ports: - - containerPort: 5001 + - containerPort: 8001 --- apiVersion: v1 kind: Service @@ -141,8 +141,8 @@ spec: selector: service: fastapi ports: - - port: 5001 - targetPort: 5001 + - port: 8001 + targetPort: 8001 nodePort: 30001 ``` @@ -219,7 +219,7 @@ To learn more about Kubernetes objects, see the [Kubernetes documentation](https NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 10.43.0.1 443/TCP 13h postgres ClusterIP 10.43.209.25 5432/TCP 3m10s - service-entrypoint NodePort 10.43.67.120 5001:30001/TCP 79s + service-entrypoint NodePort 10.43.67.120 8001:30001/TCP 79s ``` In addition to the default `kubernetes` service, you can see your `service-entrypoint` service, accepting traffic on port 30001/TCP and the internal `ClusterIP` `postgres` with the port `5432` open to accept connections from you python app. diff --git a/content/language/python/develop.md b/content/language/python/develop.md index 41eab319ffc..713f3a120d4 100644 --- a/content/language/python/develop.md +++ b/content/language/python/develop.md @@ -47,8 +47,8 @@ You'll need to clone a new repository to get a sample application that includes ? What application platform does your project use? Python ? What version of Python do you want to use? 3.11.4 - ? What port do you want your app to listen on? 5001 - ? What is the command to run your app? python3 -m uvicorn app:app --host=0.0.0.0 --port=5001 + ? What port do you want your app to listen on? 8001 + ? What is the command to run your app? python3 -m uvicorn app:app --host=0.0.0.0 --port=8001 ``` Create a file named `.gitignore` with the following contents. @@ -166,10 +166,10 @@ You'll need to clone a new repository to get a sample application that includes COPY . . # Expose the port that the application listens on. - EXPOSE 5001 + EXPOSE 8001 # Run the application. - CMD python3 -m uvicorn app:app --host=0.0.0.0 --port=5001 + CMD python3 -m uvicorn app:app --host=0.0.0.0 --port=8001 ``` Create a file named `compose.yaml` with the following contents. @@ -189,7 +189,7 @@ You'll need to clone a new repository to get a sample application that includes build: context: . ports: - - 5001:5001 + - 8001:8001 # The commented out section below is an example of how to define a PostgreSQL # database that your application can use. `depends_on` tells Docker Compose to @@ -341,7 +341,7 @@ services: build: context: . ports: - - 5001:5001 + - 8001:8001 environment: - POSTGRES_SERVER=db - POSTGRES_USER=postgres @@ -422,7 +422,7 @@ Let's create an object with a post method ```console $ curl -X 'POST' \ - 'http://0.0.0.0:5001/heroes/' \ + 'http://0.0.0.0:8001/heroes/' \ -H 'accept: application/json' \ -H 'Content-Type: application/json' \ -d '{ @@ -448,7 +448,7 @@ Let's make a get request with the next curl command: ```console curl -X 'GET' \ - 'http://0.0.0.0:5001/heroes/' \ + 'http://0.0.0.0:8001/heroes/' \ -H 'accept: application/json' ``` @@ -480,7 +480,7 @@ services: build: context: . ports: - - 5001:5001 + - 8001:8001 environment: - POSTGRES_SERVER=db - POSTGRES_USER=postgres @@ -529,7 +529,7 @@ $ docker compose watch In a terminal, curl the application to get a response. ```console -$ curl http://localhost:5001 +$ curl http://localhost:8001 Hello, Docker! ``` @@ -545,7 +545,7 @@ Open `python-docker-dev-example/app.py` in an IDE or text editor and update the Save the changes to `app.py` and then wait a few seconds for the application to rebuild. Curl the application again and verify that the updated text appears. ```console -$ curl http://localhost:5001 +$ curl http://localhost:8001 Hello, Docker!!! ``` From 125c58bca4def5a21725ffcd1640fe84e47ef52f Mon Sep 17 00:00:00 2001 From: Esteban Maya Cadavid Date: Sat, 20 Jul 2024 11:24:46 -0500 Subject: [PATCH 5/6] =?UTF-8?q?=E2=9C=8F=EF=B8=8F=20Fix=20some=20grammatic?= =?UTF-8?q?al=20structures?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- content/language/python/containerize.md | 2 +- content/language/python/deploy.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/content/language/python/containerize.md b/content/language/python/containerize.md index 981208b0337..95fbe4b44e5 100644 --- a/content/language/python/containerize.md +++ b/content/language/python/containerize.md @@ -345,7 +345,7 @@ $ docker compose up --build -d Open a browser and view the application at [http://localhost:8000](http://localhost:8000). -To see the OpenAPI docs you can go at [http://localhost:8000/docs](http://localhost:8000/docs) +To see the OpenAPI docs you can go to [http://localhost:8000/docs](http://localhost:8000/docs). You should see a simple FastAPI application. diff --git a/content/language/python/deploy.md b/content/language/python/deploy.md index e606e99315b..d3faa63ab0e 100644 --- a/content/language/python/deploy.md +++ b/content/language/python/deploy.md @@ -180,7 +180,7 @@ To learn more about Kubernetes objects, see the [Kubernetes documentation](https secret/postgres-secret created ``` - and let's deploy our python application + Now, deploy your python application. ```console kubectl apply -f docker-python-kubernetes.yaml From 260a39722f5869905a14b527c3ae56cab8bedd17 Mon Sep 17 00:00:00 2001 From: Esteban Maya Cadavid Date: Sat, 20 Jul 2024 11:38:24 -0500 Subject: [PATCH 6/6] =?UTF-8?q?=F0=9F=93=9D=20Add=20some=20notes=20about?= =?UTF-8?q?=20kubernetes=20stuff?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- content/language/python/deploy.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/content/language/python/deploy.md b/content/language/python/deploy.md index d3faa63ab0e..7048c1b2f9b 100644 --- a/content/language/python/deploy.md +++ b/content/language/python/deploy.md @@ -162,6 +162,10 @@ In these Kubernetes YAML file, there are various objects, separated by the `---` To learn more about Kubernetes objects, see the [Kubernetes documentation](https://kubernetes.io/docs/home/). +> **Note** +> +> * The `NodePort` service is good for development/testing purposes. For production you should implement an [ingress-controller](https://kubernetes.io/docs/concepts/services-networking/ingress-controllers/). + ## Deploy and check your application 1. In a terminal, navigate to `python-docker-dev-example` and deploy your database to