language/python: use "console" for shell examples

This allows for easier copying of the commands, without selecting the
prompt.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2021-08-06 17:08:26 +02:00
parent 60bb48a6a1
commit 74460241f8
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C
4 changed files with 41 additions and 41 deletions

View File

@ -26,7 +26,7 @@ To complete this tutorial, you need the following:
Lets create a simple Python application using the Flask framework that well use as our example. Create a directory in your local machine named `python-docker` and follow the steps below to create a simple web server.
```shell
```console
$ cd /path/to/python-docker
$ pip3 install Flask
$ pip3 freeze > requirements.txt
@ -35,7 +35,7 @@ $ touch app.py
Now, lets add some code to handle simple web requests. Open this working directory in your favorite IDE and enter the following code into the `app.py` file.
```shell
```python
from flask import Flask
app = Flask(__name__)
@ -48,7 +48,7 @@ def hello_world():
Lets start our application and make sure its running properly. Open your terminal and navigate to the working directory you created.
```shell
```console
$ python3 -m flask run
```
@ -170,7 +170,7 @@ The build command optionally takes a `--tag` flag. The tag is used to set the na
Lets build our first Docker image.
```shell
```console
$ docker build --tag python-docker .
[+] Building 2.7s (10/10) FINISHED
=> [internal] load build definition from Dockerfile
@ -198,7 +198,7 @@ To see a list of images we have on our local machine, we have two options. One i
To list images, simply run the `docker images` command.
```shell
```console
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
python-docker latest 8cae92a8fbd6 3 minutes ago 123MB
@ -215,7 +215,7 @@ An image is made up of a manifest and a list of layers. Do not worry too much ab
To create a new tag for the image weve built above, run the following command.
```shell
```console
$ docker tag python-docker:latest python-docker:v1.0.0
```
@ -223,7 +223,7 @@ The `docker tag` command creates a new tag for an image. It does not create a ne
Now, run the `docker images` command to see a list of our local images.
```shell
```console
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
python-docker latest 8cae92a8fbd6 4 minutes ago 123MB
@ -235,14 +235,14 @@ You can see that we have two images that start with `python-docker`. We know the
Lets remove the tag that we just created. To do this, well use the `rmi` command. The `rmi` command stands for remove image.
```shell
```console
$ docker rmi python-docker:v1.0.0
Untagged: python-docker:v1.0.0
```
Note that the response from Docker tells us that the image has not been removed but only “untagged”. You can check this by running the `docker images` command.
```shell
```console
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
python-docker latest 8cae92a8fbd6 6 minutes ago 123MB

View File

@ -187,9 +187,9 @@ on:
This ensures that the main CI will only trigger if we tag our commits with `Vn.n.n.` Lets test this. For example, run the following command:
```bash
git tag -a v1.0.2
git push origin v1.0.2
```console
$ git tag -a v1.0.2
$ git push origin v1.0.2
```
Now, go to GitHub and check your Actions

View File

@ -24,21 +24,21 @@ Before we run MySQL in a container, we'll create a couple of volumes that Docker
Lets create our volumes now. Well create one for the data and one for configuration of MySQL.
```shell
```console
$ docker volume create mysql
$ docker volume create mysql_config
```
Now well create a network that our application and database will use to talk to each other. The network is called a user-defined bridge network and gives us a nice DNS lookup service which we can use when creating our connection string.
```shell
```console
$ 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.
In the following command, option `-v` is for starting the container with volumes. For more information, see [Docker volumes](../../storage/volumes.md).
```shell
```console
$ docker run --rm -d -v mysql:/var/lib/mysql \
-v mysql_config:/etc/mysql -p 3306:3306 \
--network mysqlnet \
@ -49,7 +49,7 @@ $ docker run --rm -d -v mysql:/var/lib/mysql \
Now, lets 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
```console
$ docker exec -ti mysqldb mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
@ -75,7 +75,7 @@ Next, we'll update the sample application we created in the [Build images](build
Okay, now that we have a running MySQL, lets update the `app.py` to use MySQL as a datastore. Lets also add some routes to our server. One for fetching records and one for inserting records.
```shell
```python
import mysql.connector
import json
from flask import Flask
@ -145,20 +145,20 @@ Weve added the MySQL module and updated the code to connect to the database s
First, lets add the `mysql-connector-python` module to our application using pip.
```shell
```console
$ pip3 install mysql-connector-python
$ pip3 freeze > requirements.txt
```
Now we can build our image.
```shell
```console
$ docker build --tag python-docker-dev .
```
Now, lets add the container to the database network and then run our container. This allows us to access the database by its container name.
```shell
```console
$ docker run \
--rm -d \
--network mysqlnet \
@ -169,14 +169,14 @@ $ docker run \
Lets test that our application is connected to the database and is able to add a note.
```shell
```console
$ curl http://localhost:5000/initdb
$ curl http://localhost:5000/widgets
```
You should receive the following JSON back from our service.
```shell
```json
[]
```
@ -221,7 +221,7 @@ Another really cool feature of using a Compose file is that we have service reso
Now, to start our application and to confirm that it is running properly, run the following command:
```shell
```console
$ docker-compose -f docker-compose.dev.yml up --build
```
@ -229,14 +229,14 @@ We pass the `--build` flag so Docker will compile our image and then starts the
Now lets test our API endpoint. Open a new terminal then make a GET request to the server using the curl commands:
```shell
```console
$ curl http://localhost:5000/initdb
$ curl http://localhost:5000/widgets
```
You should receive the following response:
```shell
```json
[]
```

View File

@ -18,7 +18,7 @@ A container is a normal operating system process except that this process is iso
To run an image inside of a container, we use the `docker run` command. The `docker run` command requires one parameter which is the name of the image. Lets start our image and make sure it is running correctly. Run the following command in your terminal.
```shell
```console
$ docker run python-docker
```
@ -26,7 +26,7 @@ After running this command, youll notice that you were not returned to the co
Lets open a new terminal then make a `GET` request to the server using the `curl` command.
```shell
```console
$ curl localhost:5000
curl: (7) Failed to connect to localhost port 5000: Connection refused
```
@ -39,13 +39,13 @@ To publish a port for our container, well use the `--publish flag` (`-p` for
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
```console
$ docker run --publish 5000:5000 python-docker
```
Now, lets rerun the curl command from above. Remember to open a new terminal.
```shell
```console
$ curl localhost:5000
Hello, Docker!
```
@ -62,7 +62,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
```console
$ docker run -d -p 5000:5000 python-docker
ce02b3179f0f10085db9edfccd731101868f58631bdf918ca490ff6fd223a93b
```
@ -71,7 +71,7 @@ Docker started our container in the background and printed the Container ID on t
Again, lets make sure that our container is running properly. Run the same curl command from above.
```shell
```console
$ curl localhost:5000
Hello, Docker!
```
@ -80,7 +80,7 @@ Hello, Docker!
Since we ran our container in the background, how do we know if our container is running or what other containers are running on our machine? Well, we can run the `docker ps` command. Just like on Linux to see a list of processes on your machine, we would run the `ps` command. In the same spirit, we can run the `docker ps` command which displays a list of containers running on our machine.
```shell
```console
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ce02b3179f0f python-docker "python3 -m flask ru…" 6 minutes ago Up 6 minutes 0.0.0.0:5000->5000/tcp wonderful_kalam
@ -90,14 +90,14 @@ The `docker ps` command provides a bunch of information about our running contai
You are probably wondering where the name of our container is coming from. Since we didnt provide a name for the container when we started it, Docker generated a random name. Well fix this in a minute, but first we need to stop the container. To stop the container, run the `docker stop` command which does just that, stops the container. You need to pass the name of the container or you can use the container ID.
```shell
```console
$ docker stop wonderful_kalam
wonderful_kalam
```
Now, rerun the `docker ps` command to see a list of running containers.
```shell
```console
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
```
@ -106,7 +106,7 @@ CONTAINER ID IMAGE COMMAND CREATED
You can start, stop, and restart Docker containers. When we stop a container, it is not removed, but the status is changed to stopped and the process inside the container is stopped. When we ran the `docker ps` command in the previous module, the default output only shows running containers. When we pass the `--all` or `-a` for short, we see all containers on our machine, irrespective of their start or stop status.
```shell
```console
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ce02b3179f0f python-docker "python3 -m flask ru…" 16 minutes ago Exited (0) 5 minutes ago wonderful_kalam
@ -118,13 +118,13 @@ You should now see several containers listed. These are containers that we start
Lets restart the container that we just stopped. Locate the name of the container we just stopped and replace the name of the container below in the restart command.
```shell
```console
$ docker restart wonderful_kalam
```
Now list all the containers again using the `docker ps` command.
```shell
```console
$ docker ps --all
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ce02b3179f0f python-docker "python3 -m flask ru…" 19 minutes ago Up 8 seconds 0.0.0.0:5000->5000/tcp wonderful_kalam
@ -136,14 +136,14 @@ Notice that the container we just restarted has been started in detached mode an
Now, lets 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.
```shell
```console
$ docker stop wonderful_kalam
wonderful_kalam
```
Now that all of our containers are stopped, lets remove them. When you remove a container, it is no longer running, nor it is in the stopped status, but the process inside the container has been stopped and the metadata for the container has been removed.
```shell
```console
$ docker ps --all
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
ce02b3179f0f python-docker "python3 -m flask ru…" 19 minutes ago Up 8 seconds 0.0.0.0:5000->5000/tcp wonderful_kalam
@ -153,7 +153,7 @@ fb7a41809e5d python-docker "python3 -m flask ru…" 40 minutes
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.
```shell
```console
$ docker rm wonderful_kalam agitated_moser goofy_khayyam
wonderful_kalam
agitated_moser
@ -166,7 +166,7 @@ Now, lets 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
```console
$ docker run -d -p 5000:5000 --name rest-server python-docker
1aa5d46418a68705c81782a58456a4ccdb56a309cb5e6bd399478d01eaa5cdda
$ docker ps