Skip to content

Commit

Permalink
Merge pull request #14 from jetbrains-academy/konstantin/deploy_on_do…
Browse files Browse the repository at this point in the history
…cker_fixes

Fixes of Deploy API on Docker lesson
  • Loading branch information
kochaika authored Jul 12, 2024
2 parents 312fcd2 + fec7b00 commit d2b506a
Show file tree
Hide file tree
Showing 57 changed files with 245 additions and 113 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/links_checker.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: LinksChecker

on:
# Triggers the workflow on push or pull request events for specific branches
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]

# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:

jobs:
links-checker-job:
runs-on: ubuntu-latest
steps:
- name: Check out repository code
uses: actions/checkout@v4

- name: Call the action jetbrains-academy/links-checker-action
uses: jetbrains-academy/links-checker-action@main
with:
dir: ./
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ files:
visible: true
- name: requirements.txt
visible: true
- name: Postman1.png
- name: images/httpie_post.png
visible: false
- name: Postman2.png
- name: images/httpie_get.png
visible: false
24 changes: 24 additions & 0 deletions Deploy API on Docker/API Testing/task.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
Developers need to test their APIs. There are plenty of tools for these tasks, allowing you to send a wide variety of HTTP, REST, and other requests. For example, [Postman](https://www.postman.com/) or the integrated [HTTP Client](https://www.jetbrains.com/help/pycharm/http-client-in-product-code-editor.html) in PyCharm Professional.

In this task, we will use the open-source tool [HTTPie](https://httpie.io). All such applications are similar in some way, so you can use HTTPie or any other tool by analogy.

You can download and install a desktop version of HTTPie [here](https://httpie.io/desktop).

### GET request
As with all such programs, the interface allows you to specify the query type, parameters, headers, authorization type, and more. For the very first request, we only need to specify the URL `http://0.0.0.0:5000/items` and press the **Send** button.

Remember to launch our Flask application beforehand!

![](images/httpie_get.png)

HTTPie automatically detects the language of the response, links, and formats text inside the body to make inspection easy.

### POST request
Next, create a POST request. To do this, specify the `POST` request type, the URL `http://0.0.0.0:5000/items`, and the Body of the request. Select `Text` as the Body type.

After executing the request, you should get a response containing the request body.

![](images/httpie_post.png)

### Explore your app
Check that all the other methods defined in your API work as expected (getting a specific device, deleting a device, updating a device).
4 changes: 4 additions & 0 deletions Deploy API on Docker/Docker Intro/images/add.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions Deploy API on Docker/Docker Intro/images/add_dark.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions Deploy API on Docker/Docker Intro/images/edit.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions Deploy API on Docker/Docker Intro/images/edit_dark.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed Deploy API on Docker/Docker Intro/images/img.png
Binary file not shown.
Binary file removed Deploy API on Docker/Docker Intro/images/img_1.png
Binary file not shown.
Binary file removed Deploy API on Docker/Docker Intro/images/img_3.png
Binary file not shown.
Binary file removed Deploy API on Docker/Docker Intro/images/img_4.png
Binary file not shown.
4 changes: 4 additions & 0 deletions Deploy API on Docker/Docker Intro/images/run.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions Deploy API on Docker/Docker Intro/images/run_dark.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 0 additions & 1 deletion Deploy API on Docker/Docker Intro/main.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
if __name__ == "__main__":
# Write your solution here
pass
40 changes: 26 additions & 14 deletions Deploy API on Docker/Docker Intro/task-info.yaml
Original file line number Diff line number Diff line change
@@ -1,16 +1,28 @@
type: theory
files:
- name: main.py
visible: true
- name: __init__.py
visible: false
- name: images/img.png
visible: false
- name: images/img_1.png
visible: false
- name: images/img_2.png
visible: false
- name: images/img_3.png
visible: false
- name: images/img_4.png
visible: false
- name: main.py
visible: true
- name: __init__.py
visible: false
- name: images/docker.png
visible: false
- name: images/edit_dark.svg
visible: false
- name: images/edit.svg
visible: false
- name: images/run.svg
visible: false
- name: images/run_dark.svg
visible: false
- name: images/add_dark.svg
visible: false
- name: images/add.svg
visible: false
- name: images/settings_docker_dark.png
visible: false
- name: images/settings_docker.png
visible: false
- name: images/services.png
visible: false
- name: images/services_dark.png
visible: false
35 changes: 15 additions & 20 deletions Deploy API on Docker/Docker Intro/task.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
Running the app as described before works just fine, but it assumes you have the Python environment set up correctly. We can avoid this issue by running the application in a **Docker container**.

### Docker Containers

Docker containers provide code isolation, independence, and portability. Docker is essentially needed
if one intends to deploy their application into production. Docker containers have fully prescribed
dependencies with which they can be created. These dependencies, as well as the instructions on how
Expand All @@ -11,30 +10,26 @@ A **Dockerfile** is basically a set of instructions for building a container ima

Read more about Docker in [Docker docs](https://docs.docker.com/).

### Enable Docker support in IntelliJ IDEA

1. Install and run Docker. For more information, see the Docker documentation.

2. Configure the Docker daemon connection settings:


a) Press ⌘, to open the IDE settings and select **Build, Execution, Deployment | Docker**.


b) Click **+** to add a Docker configuration and specify how to connect to the Docker daemon.

The connection settings depend on your Docker version and operating system. For more information, see [Docker configuration](https://www.jetbrains.com/help/idea/docker-connection-settings.html).
### Docker support in JetBrains IDEs
JetBrains IDEs allow you to manage Docker containers using a graphical interface. Only a few simple steps are required to set this up.

The **Connection successful** message should appear at the bottom of the dialog.
### 1. Install and run Docker
For more information, see the [Docker documentation](https://docs.docker.com/engine/install/) specific to your operating system.

![](images/img.png)
### 2. Configure the Docker daemon connection settings

3. Connect to the Docker daemon.
The configured Docker connection should appear in the Services tool window (**View | Tool Windows | Services** or ⌘8). Select the Docker node ![](images/img_2.png) and click ![](images/img_3.png), or select **Connect** from the context menu.
1. Open the IDE settings (you can use the shortcut &shortcut:ShowSettings;) and select **Build, Execution, Deployment | Docker**.
2. Click ![](images/add.svg) to add a Docker configuration and specify how to connect to the Docker daemon.
The connection settings depend on your Docker version and operating system. For more information, see [Docker configuration](https://www.jetbrains.com/help/pycharm/settings-docker.html).
The **Connection successful** message should appear at the bottom of the dialog.

![](images/settings_docker.png)

![](images/img_1.png)
### 3. Connect to the Docker daemon
The configured Docker connection should appear in the Services tool window (**View | Tool Windows | Services** or &shortcut:ActivateServicesToolWindow;). Select the Docker node ![](images/docker.png) and click ![](images/run.svg), or select **Connect** from the context menu. Once connected, it will look like this:
<div style="text-align:center;"><img src="images/services.png" style="width:70%;" alt="Run MyRunConfig"></div>

To edit the Docker connection settings, select the Docker node and click ![](images/img_4.png) on the toolbar, or select **Edit Configuration** from the context menu.
To edit the Docker connection settings, select the Docker node and click ![](images/edit.svg) on the toolbar, or select **Edit Configuration** from the context menu.

<style>
img {
Expand Down
Binary file not shown.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
32 changes: 30 additions & 2 deletions Deploy API on Docker/Docker run configuration setup/task-info.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,35 @@ files:
visible: true
- name: requirements.txt
visible: true
- name: config2.png
- name: images/add.svg
visible: false
- name: config1.png
- name: images/add_dark.svg
visible: false
- name: images/browse_dark.svg
visible: false
- name: images/browse.svg
visible: false
- name: images/port_bindings_dark.png
visible: false
- name: images/port_bindings.png
visible: false
- name: images/add_configuration.png
visible: false
- name: images/add_configuration_dark.png
visible: false
- name: images/run.svg
visible: false
- name: images/run_dark.svg
visible: false
- name: images/run_myrunconfig_dark.png
visible: false
- name: images/run_myrunconfig.png
visible: false
- name: images/stop_container_dark.png
visible: false
- name: images/stop_container.png
visible: false
- name: images/delete_image.png
visible: false
- name: images/delete_image_dark.png
visible: false
69 changes: 51 additions & 18 deletions Deploy API on Docker/Docker run configuration setup/task.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,52 @@
## Docker run configuration setup

Another caveat: to connect to the application running in a container, you need to
map specific container ports to specific ports on the host. In the previous task, this setup was already done for you.
To do it yourself, you need to:

- From the main menu, select **Run | Edit Configurations**.
- In the **Run/Debug Configurations dialog**, click **+**, select **Docker**, and then select the **Dockerfile** configuration.
- In **Build**, select the correct Dockerfile path.
![](config1.png)
- Then click **Modify** and select **Bind ports**.
- In **Bind ports**, click the folder symbol, and in the window that opens (Port Bindings), click **+** and add
the ports to be bound. In our case, add `5000` to both Host post and Container port. Leave everything else as is.
![](config2.png)
- Add the name of the container and image tag. Please use "flask-app-invsys" and "flask-app-invsys-img" respectively.
- Click **Ok** two times.

Now, if you run the Dockerfile, you should be able to access your app via `http://0.0.0.0:5000/items`.
So, by now, we know that to run our solution in a container, we need to:
- Build an image according to the Dockerfile.
- Start the container from this image.
- Bind the container's port to our host machine's port so that we can access our application in the container.

The course checking system handles all of this for you, but let's also learn how to do it ourselves.
This is not difficult at all, as all these actions can be delegated to the IDE.

[Run configurations](https://www.jetbrains.com/help/pycharm/run-debug-configuration.html) are specially designed for these tasks. They allow describing exactly how to run an application and specifying various launch parameters. In this lesson, we will focus only on [Docker run configurations](https://www.jetbrains.com/help/pycharm/docker-run-configurations.html).

**Note: In this course, do not remove or modify run configurations that you didn't create on your own!**

### Create Docker run configuration
- From the main menu, select **Run | Edit Configurations…**
- In the opened **Run/Debug Configurations** window, click ![](images/add.svg), select **Docker**, and then select the **Dockerfile** configuration.
- Specify the name of your configuration. It can be anything you like; we chose `MyRunConfig`.
- Specify the path to the required `Dockerfile`. This will be the path to the `Dockerfile` for this lesson.
- Add the **Image tag** and the **Container name**. Please use `flask-app-invsys-img` and `flask-app-invsys`, respectively.

![](images/add_configuration.png)

- Next, click **Modify** and select **Bind ports**.
- In the **Bind ports:** field that appears, click on ![](images/browse.svg) to open the **Port Bindings** window.
- Click ![](images/add.svg) and add the ports to be bound.
- In our case, add `5000` to both **Host port** and **Container port**. Leave everything else as is.
![](images/port_bindings.png)
- Click **OK** two times to save your port binding and run configuration!

### Launch your configuration
In the upper right corner of the IDE, select your run configuration from the list if it is not already selected. Afterward, click on ![](images/run.svg) or use the shortcut &shortcut:Run; to run your configuration.
<div style="text-align:center;"><img src="images/run_myrunconfig.png" style="width:40%;" alt="Run MyRunConfig"></div>

Now, after you run the Dockerfile, you should be able to access your app via http://0.0.0.0:5000/items.
Otherwise, you will get a "Connection refused" error.

### Shut down your configuration
To stop your Docker container, just open the panel [Services](tool_window://Services) (or use the shortcut &shortcut:ActivateServicesToolWindow;), right-click the container, and select **Stop Container**. From this context menu, you can also delete the container using the **Delete...** option. This will delete only the container — no local files will be deleted.
<div style="text-align:center;"><img src="images/stop_container.png" style="width:50%;" alt="Run MyRunConfig"></div>

### Don't forget
A Docker image contains application code, libraries, tools, dependencies, and other files needed to run your application.
When your application does not work as expected and you find an error, correcting it in your project will not save this change to the image you previously created and ran. You should delete an old
Docker image and rebuild it after fixing the problem.

You can also delete your image from the [Services](tool_window://Services) panel:
<div style="text-align:center;"><img src="images/delete_image.png" style="width:60%;" alt="Run MyRunConfig"></div>

<style>
img {
display: inline !important;
}
</style>
Binary file removed Deploy API on Docker/Dockerfile/delete_image.png
Diff not rendered.
2 changes: 1 addition & 1 deletion Deploy API on Docker/Dockerfile/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ services:
container_name: flask-app-invsys
image: flask-app-invsys-img
ports:
- "5000:5000"
- "5000:5000"
2 changes: 1 addition & 1 deletion Deploy API on Docker/Dockerfile/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Flask
flask-restful
marshmallow
marshmallow
8 changes: 3 additions & 5 deletions Deploy API on Docker/Dockerfile/task-info.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ files:
placeholders:
- offset: 0
length: 141
placeholder_text: '# TODO'
placeholder_text: '# Add the contents of the Dockerfile from the task description'
- name: requirements.txt
visible: true
- name: tests/test_task.py
visible: false
- name: __init__.py
Expand All @@ -16,10 +18,6 @@ files:
visible: true
- name: dal.py
visible: true
- name: requirements.txt
visible: true
- name: delete_image.png
visible: false
- name: runConfigurations/Deploy_on_Docker_Dockerfile_Test.run.xml
visible: false
- name: docker-compose.yml
Expand Down
Loading

0 comments on commit d2b506a

Please sign in to comment.