- Notifications
You must be signed in to change notification settings - Fork1.5k
Add Docker Compose to the course!#147
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
jslvtr merged 6 commits intodevelopfromjose/cou-323-add-information-on-docker-compose-to-the-courseApr 4, 2024
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
6 commits Select commitHold shift + click to select a range
a1baec9
🐛 fix(Alembic): Remove `compare_type` references
jslvtrc4768ad
🚸 ux(Lecture code): Add missing start/end lecture codes in 04_02
jslvtrfbc9a81
✨ feat(Docker compose): Add Docker compose lectures
jslvtr9ba7bb6
Remove `-` from docker compose command
jslvtrad4f496
🐛 fix(Indentation): Fix indentation in `env.py`
jslvtr369e132
🐛 fix(JS): Fix VideoEmbed component allowfullscreen
jslvtrFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
6 changes: 6 additions & 0 deletionsdocs/docs/04_docker_intro/02_run_docker_container/end/Dockerfile
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
FROM python:3.10 | ||
EXPOSE 5000 | ||
WORKDIR /app | ||
RUN pip install flask | ||
COPY . . | ||
CMD ["flask", "run", "--host", "0.0.0.0"] |
45 changes: 45 additions & 0 deletionsdocs/docs/04_docker_intro/02_run_docker_container/end/app.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
from flask import Flask, request | ||
app = Flask(__name__) | ||
stores = [{"name": "My Store", "items": [{"name": "Chair", "price": 15.99}]}] | ||
@app.get("/store") | ||
def get_stores(): | ||
return {"stores": stores} | ||
@app.post("/store") | ||
def create_store(): | ||
request_data = request.get_json() | ||
new_store = {"name": request_data["name"], "items": []} | ||
stores.append(new_store) | ||
return new_store, 201 | ||
@app.post("/store/<string:name>/item") | ||
def create_item(name): | ||
request_data = request.get_json() | ||
for store in stores: | ||
if store["name"] == name: | ||
new_item = {"name": request_data["name"], "price": request_data["price"]} | ||
store["items"].append(new_item) | ||
return new_item, 201 | ||
return {"message": "Store not found"}, 404 | ||
@app.get("/store/<string:name>") | ||
def get_store(name): | ||
for store in stores: | ||
if store["name"] == name: | ||
return store | ||
return {"message": "Store not found"}, 404 | ||
@app.get("/store/<string:name>/item") | ||
def get_item_in_store(name): | ||
for store in stores: | ||
if store["name"] == name: | ||
return {"items": store["items"]} | ||
return {"message": "Store not found"}, 404 |
45 changes: 45 additions & 0 deletionsdocs/docs/04_docker_intro/02_run_docker_container/start/app.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
from flask import Flask, request | ||
app = Flask(__name__) | ||
stores = [{"name": "My Store", "items": [{"name": "Chair", "price": 15.99}]}] | ||
@app.get("/store") | ||
def get_stores(): | ||
return {"stores": stores} | ||
@app.post("/store") | ||
def create_store(): | ||
request_data = request.get_json() | ||
new_store = {"name": request_data["name"], "items": []} | ||
stores.append(new_store) | ||
return new_store, 201 | ||
@app.post("/store/<string:name>/item") | ||
def create_item(name): | ||
request_data = request.get_json() | ||
for store in stores: | ||
if store["name"] == name: | ||
new_item = {"name": request_data["name"], "price": request_data["price"]} | ||
store["items"].append(new_item) | ||
return new_item, 201 | ||
return {"message": "Store not found"}, 404 | ||
@app.get("/store/<string:name>") | ||
def get_store(name): | ||
for store in stores: | ||
if store["name"] == name: | ||
return store | ||
return {"message": "Store not found"}, 404 | ||
@app.get("/store/<string:name>/item") | ||
def get_item_in_store(name): | ||
for store in stores: | ||
if store["name"] == name: | ||
return {"items": store["items"]} | ||
return {"message": "Store not found"}, 404 |
57 changes: 57 additions & 0 deletionsdocs/docs/04_docker_intro/04_run_with_docker_compose/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# Run the REST API using Docker Compose | ||
Now that we've got a Docker container for our REST API, we can set up Docker Compose to run the container. | ||
Docker Compose is most useful to start multiple Docker containers at the same time, specifying configuration values for them and dependencies between them. | ||
Later on, I'll show you how to use Docker Compose to start both a PostgreSQL database and the REST API. For now, we'll use it only for the REST API, to simplify starting its container up. | ||
If you have Docker Desktop installed, you already have Docker Compose. If you want to install Docker Compose in a system without Docker Desktop, please refer to the [official installation instructions](https://docs.docker.com/compose/install/). | ||
## How to write a `docker-compose.yml` file | ||
Create a file called `docker-compose.yml` in the root of your project (alongside your `Dockerfile`). Inside it, add the following contents: | ||
```yaml | ||
version: '3' | ||
services: | ||
web: | ||
build: . | ||
ports: | ||
- "5000:5000" | ||
volumes: | ||
- .:/app | ||
``` | ||
This small file is all you need to tell Docker Compose that you have a service, called `web`, which is built using the current directory (by default, that looks for a file called `Dockerfile`). | ||
Other settings provided are: | ||
- `ports`, used to map a port in your local computer to one in the container. Since our container runs the Flask app on port 5000, we're targeting that port so that any traffic we access in port 5000 of our computer is sent to the container's port 5000. | ||
- `volumes`, to map a local directory into a directory within the container. This makes it so you don't have to rebuild the image each time you make a code change. | ||
## How to run the Docker Compose services | ||
Simply type: | ||
``` | ||
docker compose up | ||
``` | ||
And that will start all your services. For now, there's just one service, but later on when we add a database, this command will start everything. | ||
When the services are running, you'll start seeing logs appear. These are the same logs as for running the `Dockerfile` on its own, but preceded by the service name. | ||
In our case, we'll see `web-1 | ...` and the logs saying the service is running on `http://127.0.0.1:5000`. When you access that URL, you'll see the request logs printed in the console. | ||
Congratulations, you've ran your first Docker Compose service! | ||
## Rebuilding the Docker image | ||
If you need to rebuild the Docker image of your REST API service for whatever reason (e.g. configuration changes), you can run: | ||
``` | ||
docker compose up --build --force-recreate --no-deps web | ||
``` | ||
More information [here](https://stackoverflow.com/a/50802581). |
6 changes: 6 additions & 0 deletionsdocs/docs/04_docker_intro/04_run_with_docker_compose/end/Dockerfile
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
FROM python:3.10 | ||
EXPOSE 5000 | ||
WORKDIR /app | ||
RUN pip install flask | ||
COPY . . | ||
CMD ["flask", "run", "--host", "0.0.0.0"] |
45 changes: 45 additions & 0 deletionsdocs/docs/04_docker_intro/04_run_with_docker_compose/end/app.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
from flask import Flask, request | ||
app = Flask(__name__) | ||
stores = [{"name": "My Store", "items": [{"name": "Chair", "price": 15.99}]}] | ||
@app.get("/store") | ||
def get_stores(): | ||
return {"stores": stores} | ||
@app.post("/store") | ||
def create_store(): | ||
request_data = request.get_json() | ||
new_store = {"name": request_data["name"], "items": []} | ||
stores.append(new_store) | ||
return new_store, 201 | ||
@app.post("/store/<string:name>/item") | ||
def create_item(name): | ||
request_data = request.get_json() | ||
for store in stores: | ||
if store["name"] == name: | ||
new_item = {"name": request_data["name"], "price": request_data["price"]} | ||
store["items"].append(new_item) | ||
return new_item, 201 | ||
return {"message": "Store not found"}, 404 | ||
@app.get("/store/<string:name>") | ||
def get_store(name): | ||
for store in stores: | ||
if store["name"] == name: | ||
return store | ||
return {"message": "Store not found"}, 404 | ||
@app.get("/store/<string:name>/item") | ||
def get_item_in_store(name): | ||
for store in stores: | ||
if store["name"] == name: | ||
return {"items": store["items"]} | ||
return {"message": "Store not found"}, 404 |
8 changes: 8 additions & 0 deletionsdocs/docs/04_docker_intro/04_run_with_docker_compose/end/docker-compose.yml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
version: '3' | ||
services: | ||
web: | ||
build: . | ||
ports: | ||
- "5000:5000" | ||
volumes: | ||
- .:/app |
6 changes: 6 additions & 0 deletionsdocs/docs/04_docker_intro/04_run_with_docker_compose/start/Dockerfile
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
FROM python:3.10 | ||
EXPOSE 5000 | ||
WORKDIR /app | ||
RUN pip install flask | ||
COPY . . | ||
CMD ["flask", "run", "--host", "0.0.0.0"] |
45 changes: 45 additions & 0 deletionsdocs/docs/04_docker_intro/04_run_with_docker_compose/start/app.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
from flask import Flask, request | ||
app = Flask(__name__) | ||
stores = [{"name": "My Store", "items": [{"name": "Chair", "price": 15.99}]}] | ||
@app.get("/store") | ||
def get_stores(): | ||
return {"stores": stores} | ||
@app.post("/store") | ||
def create_store(): | ||
request_data = request.get_json() | ||
new_store = {"name": request_data["name"], "items": []} | ||
stores.append(new_store) | ||
return new_store, 201 | ||
@app.post("/store/<string:name>/item") | ||
def create_item(name): | ||
request_data = request.get_json() | ||
for store in stores: | ||
if store["name"] == name: | ||
new_item = {"name": request_data["name"], "price": request_data["price"]} | ||
store["items"].append(new_item) | ||
return new_item, 201 | ||
return {"message": "Store not found"}, 404 | ||
@app.get("/store/<string:name>") | ||
def get_store(name): | ||
for store in stores: | ||
if store["name"] == name: | ||
return store | ||
return {"message": "Store not found"}, 404 | ||
@app.get("/store/<string:name>/item") | ||
def get_item_in_store(name): | ||
for store in stores: | ||
if store["name"] == name: | ||
return {"items": store["items"]} | ||
return {"message": "Store not found"}, 404 |
109 changes: 1 addition & 108 deletionsdocs/docs/11_deploy_to_render/05_environment_variables_and_migrations/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.