mirror of https://github.com/docker/docs.git
Merge pull request #12559 from StefanScherer/clean-python
Fix Python tutorial
This commit is contained in:
commit
9e5ddcbaf1
|
@ -38,20 +38,19 @@ $ docker network create mysqlnet
|
|||
Now we can run MySQL in a container and attach to the volumes and network we created above. Docker pulls the image from Hub and runs it for you locally.
|
||||
|
||||
```shell
|
||||
$ docker run -it --rm -d -v mysql:/var/lib/mysql \
|
||||
$ docker run --rm -d -v mysql:/var/lib/mysql \
|
||||
-v mysql_config:/etc/mysql -p 3306:3306 \
|
||||
--network mysqlnet \
|
||||
--name mysqldb \
|
||||
-e MYSQL_ALLOW_EMPTY_PASSWORD=true \
|
||||
-e MYSQL_ROOT_PASSWORD=p@ssw0rd1 \
|
||||
mysql
|
||||
```
|
||||
|
||||
Now, let’s make sure that our MySQL database is running and that we can connect to it. Connect to the running MySQL database inside the container using the following command:
|
||||
Now, let’s make sure that our MySQL database is running and that we can connect to it. Connect to the running MySQL database inside the container using the following command and enter "p@ssw0rd1" when prompted for the password:
|
||||
|
||||
```shell
|
||||
$ docker run -it --network mysqlnet --rm mysql mysql -hmysqldb
|
||||
Enter password: ********
|
||||
|
||||
$ docker exec -ti mysqldb mysql -u root -p
|
||||
Enter password:
|
||||
Welcome to the MySQL monitor. Commands end with ; or \g.
|
||||
Your MySQL connection id is 8
|
||||
Server version: 8.0.23 MySQL Community Server - GPL
|
||||
|
@ -69,13 +68,11 @@ mysql>
|
|||
|
||||
### Connect the application to the database
|
||||
|
||||
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.
|
||||
In the above command, we logged in to the MySQL database by passing the ‘mysql’ command to the `mysqldb` container. Press CTRL-D to exit the MySQL interactive terminal.
|
||||
|
||||
Next, we'll update the sample application we created in the [Build images](build-images.md#sample-application) module. To see the directory structure of the Python app, see [Python application directory structure](build-images.md#directory-structure).
|
||||
|
||||
Okay, now that we have a running MySQL, let’s update the `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.
|
||||
Okay, now that we have a running MySQL, let’s update the `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.
|
||||
|
||||
```shell
|
||||
import mysql.connector
|
||||
|
@ -149,7 +146,7 @@ First, let’s add the `mysql-connector-python` module to our application using
|
|||
|
||||
```shell
|
||||
$ pip3 install mysql-connector-python
|
||||
$ pip3 freeze -r requirements.txt
|
||||
$ pip3 freeze > requirements.txt
|
||||
```
|
||||
|
||||
Now we can build our image.
|
||||
|
@ -162,7 +159,7 @@ Now, let’s add the container to the database network and then run our containe
|
|||
|
||||
```shell
|
||||
$ docker run \
|
||||
-it --rm -d \
|
||||
--rm -d \
|
||||
--network mysqlnet \
|
||||
--name rest-server \
|
||||
-p 5000:5000 \
|
||||
|
@ -172,18 +169,14 @@ $ docker run \
|
|||
Let’s test that our application is connected to the database and is able to add a note.
|
||||
|
||||
```shell
|
||||
$ curl http://localhost:5000/db
|
||||
$ curl --request POST \
|
||||
--url http://localhost:5000/widgets \
|
||||
--header 'Content-Type: application/x-www-form-urlencoded' \
|
||||
--data 'name=widget01' \
|
||||
--data 'description=this is a test widget'
|
||||
$ curl http://localhost:5000/initdb
|
||||
$ curl http://localhost:5000/widgets
|
||||
```
|
||||
|
||||
You should receive the following JSON back from our service.
|
||||
|
||||
```shell
|
||||
[{"name": "widget01", "description": "this is a test widget"}]
|
||||
[]
|
||||
```
|
||||
|
||||
## Use Compose to develop locally
|
||||
|
@ -236,8 +229,8 @@ We pass the `--build` flag so Docker will compile our image and then starts the
|
|||
Now let’s test our API endpoint. Run the following curl commands:
|
||||
|
||||
```shell
|
||||
$ curl --request GET --url http://localhost:5000/db
|
||||
$ curl --request GET --url http://localhost:5000/widgets
|
||||
$ curl http://localhost:5000/initdb
|
||||
$ curl http://localhost:5000/widgets
|
||||
```
|
||||
|
||||
You should receive the following response:
|
||||
|
|
|
@ -27,30 +27,30 @@ After running this command, you’ll notice that you were not returned to the co
|
|||
Let’s make a `GET` request to the server using the `curl` command.
|
||||
|
||||
```shell
|
||||
$ curl localhost:8000
|
||||
curl: (7) Failed to connect to localhost port 8000: Connection refused
|
||||
$ curl localhost:5000
|
||||
curl: (7) Failed to connect to localhost port 5000: Connection refused
|
||||
```
|
||||
|
||||
As you can see, our `curl` command failed because the connection to our server was refused. This means, we were not able to connect to the localhost on `port 8000`. This is expected because our container is run in isolation which includes networking. Let’s stop the container and restart with `port 8000` published on our local network.
|
||||
As you can see, our `curl` command failed because the connection to our server was refused. This means, we were not able to connect to the localhost on port 5000. This is expected because our container is run in isolation which includes networking. Let’s stop the container and restart with port 5000 published on our local network.
|
||||
|
||||
To stop the container, press ctrl-c. This will return you to the terminal prompt.
|
||||
|
||||
To publish a port for our container, we’ll use the `--publish flag` (`-p` for short) on the `docker run` command. The format of the `--publish` command is `[host port]:[container port]`. So, if we wanted to expose port 8000 inside the container to port 3000 outside the container, we would pass `3000:8000` to the `--publish` flag.
|
||||
To publish a port for our container, we’ll use the `--publish flag` (`-p` for short) on the `docker run` command. The format of the `--publish` command is `[host port]:[container port]`. So, if we wanted to expose port 5000 inside the container to port 5000 outside the container, we would pass `3000:5000` to the `--publish` flag.
|
||||
|
||||
We did not specify a port when running the flask application in the container and the default is 5000. If we want our previous request going to port 8000 to work we can map the host's port 8000 to the container's port 5000:
|
||||
We did not specify a port when running the flask application in the container and the default is 5000. If we want our previous request going to port 5000 to work we can map the host's port 5000 to the container's port 5000:
|
||||
|
||||
```shell
|
||||
$ docker run --publish 8000:5000 python-docker
|
||||
$ docker run --publish 5000:5000 python-docker
|
||||
```
|
||||
|
||||
Now, let’s rerun the curl command from above:
|
||||
|
||||
```shell
|
||||
$ curl localhost:8000
|
||||
$ curl localhost:5000
|
||||
Hello, Docker!
|
||||
```
|
||||
|
||||
Success! We were able to connect to the application running inside of our container on port 8000. Switch back to the terminal where your container is running and you should see the POST request logged to the console.
|
||||
Success! We were able to connect to the application running inside of our container on port 5000. Switch back to the terminal where your container is running and you should see the POST request logged to the console.
|
||||
|
||||
```shell
|
||||
[31/Jan/2021 23:39:31] "GET / HTTP/1.1" 200 -
|
||||
|
@ -63,7 +63,7 @@ Press ctrl-c to stop the container.
|
|||
This is great so far, but our sample application is a web server and we don't have to be connected to the container. Docker can run your container in detached mode or in the background. To do this, we can use the `--detach` or `-d` for short. Docker starts your container the same as before but this time will “detach” from the container and return you to the terminal prompt.
|
||||
|
||||
```shell
|
||||
$ docker run -d -p 8000:5000 python-docker
|
||||
$ docker run -d -p 5000:5000 python-docker
|
||||
ce02b3179f0f10085db9edfccd731101868f58631bdf918ca490ff6fd223a93b
|
||||
```
|
||||
|
||||
|
@ -72,7 +72,7 @@ Docker started our container in the background and printed the Container ID on t
|
|||
Again, let’s make sure that our container is running properly. Run the same curl command from above.
|
||||
|
||||
```shell
|
||||
$ curl localhost:8000
|
||||
$ curl localhost:5000
|
||||
Hello, Docker!
|
||||
```
|
||||
|
||||
|
@ -83,7 +83,7 @@ Since we ran our container in the background, how do we know if our container is
|
|||
```shell
|
||||
$ docker ps
|
||||
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
|
||||
ce02b3179f0f python-docker "docker-entrypoint.s…" 6 minutes ago Up 6 minutes 0.0.0.0:8000->5000/tcp wonderful_kalam
|
||||
ce02b3179f0f python-docker "python3 -m flask ru…" 6 minutes ago Up 6 minutes 0.0.0.0:5000->5000/tcp wonderful_kalam
|
||||
```
|
||||
|
||||
The `docker ps` command provides a bunch of information about our running containers. We can see the container ID, The image running inside the container, the command that was used to start the container, when it was created, the status, ports that exposed and the name of the container.
|
||||
|
@ -109,9 +109,9 @@ You can start, stop, and restart Docker containers. When we stop a container, it
|
|||
```shell
|
||||
$ docker ps -a
|
||||
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
|
||||
ce02b3179f0f python-docker "docker-entrypoint.s…" 16 minutes ago Exited (0) 5 minutes ago wonderful_kalam
|
||||
ec45285c456d python-docker "docker-entrypoint.s…" 28 minutes ago Exited (0) 20 minutes ago agitated_moser
|
||||
fb7a41809e5d python-docker "docker-entrypoint.s…" 37 minutes ago Exited (0) 36 minutes ago goofy_khayyam
|
||||
ce02b3179f0f python-docker "python3 -m flask ru…" 16 minutes ago Exited (0) 5 minutes ago wonderful_kalam
|
||||
ec45285c456d python-docker "python3 -m flask ru…" 28 minutes ago Exited (0) 20 minutes ago agitated_moser
|
||||
fb7a41809e5d python-docker "python3 -m flask ru…" 37 minutes ago Exited (0) 36 minutes ago goofy_khayyam
|
||||
```
|
||||
|
||||
You should now see several containers listed. These are containers that we started and stopped but have not been removed.
|
||||
|
@ -127,12 +127,12 @@ Now list all the containers again using the `docker ps` command.
|
|||
```shell
|
||||
$ docker ps --all
|
||||
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
|
||||
ce02b3179f0f python-docker "docker-entrypoint.s…" 19 minutes ago Up 8 seconds 0.0.0.0:8000->8000/tcp wonderful_kalam
|
||||
ec45285c456d python-docker "docker-entrypoint.s…" 31 minutes ago Exited (0) 23 minutes ago agitated_moser
|
||||
fb7a41809e5d python-docker "docker-entrypoint.s…" 40 minutes ago Exited (0) 39 minutes ago goofy_khayyam
|
||||
ce02b3179f0f python-docker "python3 -m flask ru…" 19 minutes ago Up 8 seconds 0.0.0.0:5000->5000/tcp wonderful_kalam
|
||||
ec45285c456d python-docker "python3 -m flask ru…" 31 minutes ago Exited (0) 23 minutes ago agitated_moser
|
||||
fb7a41809e5d python-docker "python3 -m flask ru…" 40 minutes ago Exited (0) 39 minutes ago goofy_khayyam
|
||||
```
|
||||
|
||||
Notice that the container we just restarted has been started in detached mode and has port 8000 exposed. Also, observe the status of the container is “Up X seconds”. When you restart a container, it starts with the same flags or commands that it was originally started with.
|
||||
Notice that the container we just restarted has been started in detached mode and has port 5000 exposed. Also, observe the status of the container is “Up X seconds”. When you restart a container, it starts with the same flags or commands that it was originally started with.
|
||||
|
||||
Now, let’s stop and remove all of our containers and take a look at fixing the random naming issue. Stop the container we just started. Find the name of your running container and replace the name in the command below with the name of the container on your system.
|
||||
|
||||
|
@ -146,9 +146,9 @@ Now that all of our containers are stopped, let’s remove them. When you remove
|
|||
```shell
|
||||
$ docker ps --all
|
||||
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
|
||||
ce02b3179f0f python-docker "docker-entrypoint.s…" 19 minutes ago Up 8 seconds 0.0.0.0:8000->5000/tcp wonderful_kalam
|
||||
ec45285c456d python-docker "docker-entrypoint.s…" 31 minutes ago Exited (0) 23 minutes ago agitated_moser
|
||||
fb7a41809e5d python-docker "docker-entrypoint.s…" 40 minutes ago Exited (0) 39 minutes ago goofy_khayyam
|
||||
ce02b3179f0f python-docker "python3 -m flask ru…" 19 minutes ago Up 8 seconds 0.0.0.0:5000->5000/tcp wonderful_kalam
|
||||
ec45285c456d python-docker "python3 -m flask ru…" 31 minutes ago Exited (0) 23 minutes ago agitated_moser
|
||||
fb7a41809e5d python-docker "python3 -m flask ru…" 40 minutes ago Exited (0) 39 minutes ago goofy_khayyam
|
||||
```
|
||||
|
||||
To remove a container, simple run the `docker rm` command passing the container name. You can pass multiple container names to the command using a single command. Again, replace the container names in the following command with the container names from your system.
|
||||
|
@ -167,11 +167,11 @@ Now, let’s address the random naming issue. Standard practice is to name your
|
|||
To name a container, we just need to pass the `--name` flag to the `docker run` command.
|
||||
|
||||
```shell
|
||||
$ docker run -d -p 8000:5000 --name rest-server python-docker
|
||||
$ docker run -d -p 5000:5000 --name rest-server python-docker
|
||||
1aa5d46418a68705c81782a58456a4ccdb56a309cb5e6bd399478d01eaa5cdda
|
||||
$ docker ps
|
||||
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
|
||||
1aa5d46418a6 python-docker "docker-entrypoint.s…" 3 seconds ago Up 3 seconds 0.0.0.0:8000->5000/tcp rest-server
|
||||
1aa5d46418a6 python-docker "python3 -m flask ru…" 3 seconds ago Up 3 seconds 0.0.0.0:5000->5000/tcp rest-server
|
||||
```
|
||||
|
||||
That’s better! We can now easily identify our container based on the name.
|
||||
|
|
Loading…
Reference in New Issue