Skip to content

Conversation

@usha-mandya
Copy link
Member

Signed-off-by: Usha Mandya usha.mandya@docker.com

Adding getting started docs for Python

@netlify
Copy link

netlify bot commented Jan 26, 2021

Deploy preview for docsdocker ready!

Built with commit acf969c

https://deploy-preview-12184--docsdocker.netlify.app


In the above command, we used the same MySQL image to connect to the database but this time, we passed the ‘mysql’ command to the container with the `-h` flag containing the name of our MySQL container name. Press CTRL-D to exit the MySQL interactive terminal.

Okay, now that we have a running MySQL, let’s update `app.py` to use MySQL as a datastore. Let’s also add some routes to our server. One for fetching records and one for inserting records.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we add here a link to the app setup in the Build images page as a reminder? Just in case we land directly on this page or forgot our application setup 😅
I would find useful also to have the directory structure/tree for the python app as we have to edit the files next:

python-docker
|____ app.py
|____ requirements.txt
|____ Dockerfile

```

In the above command, we used the same MySQL image to connect to the database but this time, we passed the ‘mysql’ command to the container with the `-h` flag containing the name of our MySQL container name. Press CTRL-D to exit the MySQL interactive terminal.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would add a new section title here to make it clear we change focus. We discuss next how to connect our python application to the database running in a container.


In this module, we’ll walk through setting up a local development environment for the application we built in the previous modules. We’ll use Docker to build our images and Docker Compose to make everything a whole lot easier.

## Local database and containers
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we change section title to Running a database in a container/ containerized database or something else to quickly describe what we talk about in this part? And maybe we can move the first paragraph in the intro part.

First, let’s add the `mysql-connector-python `module to our application using pip.
```shell
$ pip install mysql-connector-python
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it would be easier to manually add the dependency to requirements.txt. Same as in https://github.com/docker/awesome-compose/blob/master/nginx-flask-mysql/backend/requirements.txt

From what I understand pip freeze command dumps all installed local packages, not only the ones we need for the app. I may be wrong, I have never used pip freeze before 😅

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flask==1.1.1
mysql-connector==2.2.9

Now, let’s run our container.
```shell
$ docker run \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe outline here that we add the container to the DB network. This way we can access the DB by its container name. We can also add a comment in the python code where we connect to the DB.

Signed-off-by: Usha Mandya <usha.mandya@docker.com>
Signed-off-by: Usha Mandya <usha.mandya@docker.com>
@usha-mandya
Copy link
Member Author

@aiordache Addressed the review comments we discussed earlier:

  • I've updated the section heading in develop.md so users can get a better understanding of the task they perform
  • Created a new section in build-images.md on the Python app directory structure
  • Added a link from the develop.md page to the directory structure and sample application to help users get a background of the updates in build-images.

PTAL.

@usha-mandya usha-mandya requested a review from aiordache January 28, 2021 13:26
@usha-mandya usha-mandya marked this pull request as ready for review January 28, 2021 15:13

```shell
$ cd /path/to/python-docker
$ pip install Flask
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest to consistently use pip3 instead of pip. We suggest to install Python 3.8 or later and I'm not sure about if the magic "pip3 = pip" on all Mac and Windows platforms. Maybe @aiordache can confirm?

Let’s start our application and make sure it’s running properly. Open your terminal and navigate to the working directory you created.

```shell
$ python -m flask run
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same suggestion to use python3 instead of python.
On my Mac I have eg. two version of Pythons installed, don't know when and why I have them, more because it was needed to run some tools. I'm not a Python developer, so I'm always confused 😅

Signed-off-by: Usha Mandya <usha.mandya@docker.com>
@usha-mandya
Copy link
Member Author

Thanks @StefanScherer. That's right. We do have pip3 within some of the examples. I've updated pip to pip3 to be consistent.

Signed-off-by: Usha Mandya <usha.mandya@docker.com>

Switch back to the terminal where our server is running and you should see the following requests in the server logs. The data and timestamp will be different on your machine.

```shel
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: small typo

Copy link
Member

@StefanScherer StefanScherer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 🐍

@usha-mandya usha-mandya merged commit c23d1da into docker:master Jan 28, 2021
@usha-mandya usha-mandya deleted the python-get-started branch January 28, 2021 17:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants