2018-04-10T17:58:26Z

The Flask Mega-Tutorial Part XIX: Deployment on Docker Containers

This is the nineteenth installment of the Flask Mega-Tutorial series, in which I'm going to deploy Microblog to the Docker container platform.

For your reference, below is a list of the articles in this series.

Note 1: If you are looking for the legacy version of this tutorial, it's here.

Note 2: If you would like to support my work on this blog, or just don't have patience to wait for weekly articles, I am offering the complete version of this tutorial packaged as an ebook or a set of videos. For more information, visit courses.miguelgrinberg.com.

In Chapter 17 you learned about traditional deployments, in which you have to take care of every little aspect of the server configuration. Then in Chapter 18 I took you to the other extreme when I introduced you to Heroku, a service that takes complete control of the configuration and deployment tasks, allowing you to fully concentrate on your application. In this chapter you are going to learn about a third application deployment strategy based on containers, more particularly on the Docker container platform. This third option sits somewhere in between the other two in terms of the amount of deployment work needed on your part.

Containers are built on a lightweight virtualization technology that allows an application, along with its dependencies and configuration to run in complete isolation, but without the need to use a full blown virtualization solution such as virtual machines, which need a lot more resources and can sometimes have a significant performance degradation in comparison to the host. A system configured as a container host can execute many containers, all of them sharing the host's kernel and direct access to the host's hardware. This is in contrast to virtual machines, which have to emulate a complete system, including CPU, disk, other hardware, kernel, etc.

In spite of having to share the kernel, the level of isolation in a container is pretty high. A container has its own file system, and can be based on an operating system that is different than the one used by the container host. For example, you can run containers based on Ubuntu Linux on a Fedora host, or vice versa. While containers are a technology that is native to the Linux operating system, thanks to virtualization it is also possible to run Linux containers on Windows and Mac OS X hosts. This allows you to test your deployments on your development system, and also incorporate containers in your development workflow if you wish to do so.

The GitHub links for this chapter are: Browse, Zip, Diff.

Installing Docker CE

While Docker isn't the only container platform, it is by far the most popular, so that's going to be my choice. There are two editions of Docker, a free community edition (CE) and a subscription based enterprise edition (EE). For the purposes of this tutorial Docker CE is perfectly adequate.

To work with Docker CE, you first have to install it on your system. There are installers for Windows, Mac OS X and several Linux distributions available at the Docker website. If you are working on a Microsoft Windows system, it is important to note that Docker CE requires Hyper-V. The installer will enable this for you if necessary, but keep in mind that enabling Hyper-V prevents other virtualization technologies such as VirtualBox from working.

Once Docker CE is installed on your system, you can verify that the install was successful by typing the following command on a terminal window or command prompt:

$ docker version
Client:
 Version:      17.09.0-ce
 API version:  1.32
 Go version:   go1.8.3
 Git commit:   afdb6d4
 Built:        Tue Sep 26 22:40:09 2017
 OS/Arch:      darwin/amd64

Server:
 Version:      17.09.0-ce
 API version:  1.32 (minimum version 1.12)
 Go version:   go1.8.3
 Git commit:   afdb6d4
 Built:        Tue Sep 26 22:45:38 2017
 OS/Arch:      linux/amd64
 Experimental: true

Building a Container Image

The first step in creating a container for Microblog is to build an image for it. A container image is a template that is used to create a container. It contains a complete representation of the container file system, along with various settings pertaining to networking, start up options, etc.

The most basic way to create a container image for your application is to start a container for the base operating system you want to use (Ubuntu, Fedora, etc.), connect to a bash shell process running in it, and then manually install your application, maybe following the guidelines I presented in Chapter 17 for a traditional deployment. After you install everything, you can take a snapshot of the container and that becomes the image. This type of workflow is supported with the docker command, but I'm not going to discuss it because it is not convenient to have to manually install the application every time you need to generate a new image.

A better approach is to generate the container image through a script. The command that creates scripted container images is docker build. This command reads and executes build instructions from a file called Dockerfile, which I will need to create. The Dockerfile is basically an installer script of sorts that executes the installation steps to get the application deployed, plus some container specific settings.

Here is a basic Dockerfile for Microblog:

Dockerfile: Dockerfile for Microblog.

FROM python:3.6-alpine

RUN adduser -D microblog

WORKDIR /home/microblog

COPY requirements.txt requirements.txt
RUN python -m venv venv
RUN venv/bin/pip install -r requirements.txt
RUN venv/bin/pip install gunicorn

COPY app app
COPY migrations migrations
COPY microblog.py config.py boot.sh ./
RUN chmod +x boot.sh

ENV FLASK_APP microblog.py

RUN chown -R microblog:microblog ./
USER microblog

EXPOSE 5000
ENTRYPOINT ["./boot.sh"]

Each line in the Dockerfile is a command. The FROM command specifies the base container image on which the new image will be built. The idea is that you start from an existing image, add or change some things, and you end up with a derived image. Images are referenced by a name and a tag, separated by a colon. The tag is used as a versioning mechanism, allowing a container image to provide more than one variant. The name of my chosen image is python, which is the official Docker image for Python. The tags for this image allow you to specify the interpreter version and base operating system. The 3.6-alpine tag selects a Python 3.6 interpreter installed on Alpine Linux. The Alpine Linux distribution is often used instead of more popular ones such as Ubuntu because of its small size. You can see what tags are available for the Python image in the Python image repository.

The RUN command executes an arbitrary command in the context of the container. This would be similar to you typing the command in a shell prompt. The adduser -D microblog command creates a new user named microblog. Most container images have root as the default user, but it is not a good practice to run an application as root, so I create my own user.

The WORKDIR command sets a default directory where the application is going to be installed. When I created the microblog user above, a home directory was created, so now I'm making that directory the default. The new default directory is going to apply to any remaining commands in the Dockerfile, and also later when the container is executed.

The COPY command transfers files from your machine to the container file system. This command takes two or more arguments, the source and destination files or directories. The source file(s) must be relative to the directory where the Dockerfile is located. The destination can be an absolute path, or a path relative to the directory that was set in a previous WORKDIR command. In this first COPY command, I'm copying the requirements.txt file to the microblog user's home directory in the container file system.

Now that I have the requirements.txt file in the container, I can create a virtual environment, using the RUN command. First I create it, and then I install all the requirements in it. Because the requirements file contains only generic dependencies, I then explicitly install gunicorn, which I'm going to use as a web server. Alternatively, I could have added gunicorn to my requirements.txt file.

The three COPY commands that follow install the application in the container, by copying the app package, the migrations directory with the database migrations, and the microblog.py and config.py scripts from the top-level directory. I'm also copying a new file, boot.sh that I will discuss below.

The RUN chmod command ensures that this new boot.sh file is correctly set as an executable file. If you are in a Unix based file system and your source file is already marked as executable, then the copied file will also have the executable bit set. I added an explicit set because on Windows it is harder to set executable bits. If you are working on Mac OS X or Linux you probably don't need this statement, but it does not hurt to have it anyway.

The ENV command sets an environment variable inside the container. I need to set FLASK_APP, which is required to use the flask command.

The RUN chown command that follows sets the owner of all the directories and files that were stored in /home/microblog as the new microblog user. Even though I created this user near the top of the Dockerfile, the default user for all the commands remained root, so all these files need to be switched to the microblog user so that this user can work with them when the container is started.

The USER command in the next line makes this new microblog user the default for any subsequent instructions, and also for when the container is started.

The EXPOSE command configures the port that this container will be using for its server. This is necessary so that Docker can configure the network in the container appropriately. I've chosen the standard Flask port 5000, but this can be any port.

Finally, the ENTRYPOINT command defines the default command that should be executed when the container is started. This is the command that will start the application web server. To keep things well organized, I decided to create a separate script for this, and this is the boot.sh file that I copied to the container earlier. Here are the contents of this script:

boot.sh: Docker container start-up script.

#!/bin/sh
source venv/bin/activate
flask db upgrade
flask translate compile
exec gunicorn -b :5000 --access-logfile - --error-logfile - microblog:app

This is a fairly standard start up script that is fairly similar to how the deployments in Chapter 17 and Chapter 18 were started. I activate the virtual environment, upgrade the database though the migration framework, compile the language translations, and finally run the server with gunicorn.

Note the exec that precedes the gunicorn command. In a shell script, exec triggers the process running the script to be replaced with the command given, instead of starting it as a new process. This is important, because Docker associates the life of the container to the first process that runs on it. In cases like this one, where the start up process is not the main process of the container, you need to make sure that the main process takes the place of that first process to ensure that the container is not terminated early by Docker.

An interesting aspect of Docker is that anything that the container writes to stdout or stderr will be captured and stored as logs for the container. For that reason, the --access-logfile and --error-logfile are both configured with a -, which sends the log to standard output so that they are stored as logs by Docker.

With the Dockerfile created, I can now build a container image:

$ docker build -t microblog:latest .

The -t argument that I'm giving to the docker build command sets the name and tag for the new container image. The . indicates the base directory where the container is to be built. This is the directory where the Dockerfile is located. The build process is going to evaluate all the commands in the Dockerfile and create the image, which will be stored on your own machine.

You can obtain a list of the images that you have locally with the docker images command:

$ docker images
REPOSITORY    TAG          IMAGE ID        CREATED              SIZE
microblog     latest       54a47d0c27cf    About a minute ago   216MB
python        3.6-alpine   a6beab4fa70b    3 months ago         88.7MB

This listing will include your new image, and also the base image on which it was built. Any time you make changes to the application, you can update the container image by running the build command again.

Starting a Container

With an image already created, you can now run the container version of the application. This is done with the docker run command, which usually takes a large number of arguments. I'm going to start by showing you a basic example:

$ docker run --name microblog -d -p 8000:5000 --rm microblog:latest
021da2e1e0d390320248abf97dfbbe7b27c70fefed113d5a41bb67a68522e91c

The --name option provides a name for the new container. The -d option tells Docker to run the container in the background. Without -d the container runs as a foreground application, blocking your command prompt. The -p option maps container ports to host ports. The first port is the port on the host computer, and the one on the right is the port inside the container. The above example exposes port 5000 in the container on port 8000 in the host, so you will access the application on 8000, even though internally the container is using 5000. The --rm option will delete the container once it is terminated. While this isn't required, containers that finish or are interrupted are usually not needed anymore, so they can be automatically deleted. The last argument is the container image name and tag to use for the container. After you run the above command, you can access the application at http://localhost:8000.

The output of docker run is the ID assigned to the new container. This is a long hexadecimal string, that you can use whenever you need to refer to the container in subsequent commands. In fact, only the first few characters are necessary, enough to make the ID unique.

If you want to see what containers are running, you can use the docker ps command:

$ docker ps
CONTAINER ID  IMAGE             COMMAND      PORTS                   NAMES
021da2e1e0d3  microblog:latest  "./boot.sh"  0.0.0.0:8000->5000/tcp  microblog

You can see that even the docker ps command shortens container IDs. If you now want to stop the container, you can use docker stop:

$ docker stop 021da2e1e0d3
021da2e1e0d3

If you recall, there are a number of options in the application's configuration that are sourced from environment variables. For example, the Flask secret key, database URL and email server options are all imported from environment variables. In the docker run example above I have not worried about those, so all those configuration options are going to use defaults.

In a more realistic example, you will be setting those environment variables inside the container. You saw in the previous section that the ENV command in the Dockerfile sets environment variables, and it is a handy option for variables that are going to be static. For variables that depend on the installation, however, it isn't convenient to have them as part of the build process, because you want to have a container image that is fairly portable. If you want to give your application to another person as a container image, you would want that person to be able to use it as is, and not have to rebuild it with different variables.

So build-time environment variables can be useful, but there is also a need to have run-time environment variables that can be set via the docker run command, and for these variables, the -e option can be used. The following example sets a secret key and sends email through a gmail account:

$ docker run --name microblog -d -p 8000:5000 --rm -e SECRET_KEY=my-secret-key \
    -e MAIL_SERVER=smtp.googlemail.com -e MAIL_PORT=587 -e MAIL_USE_TLS=true \
    -e MAIL_USERNAME=<your-gmail-username> -e MAIL_PASSWORD=<your-gmail-password> \
    microblog:latest

It is not uncommon for docker run command lines to be extremely long due to having many environment variable definitions.

Using Third-Party "Containerized" Services

The container version of Microblog is looking good, but I haven't really thought much about storage yet. In fact, since I haven't set a DATABASE_URL environment variable, the application is using the default SQLite database, which is supported by a file on disk. What do you think is going to happen to that SQLite file when you stop and delete the container? The file is going to disappear!

The file system in a container is ephemeral, meaning that it goes away when the container goes away. You can write data to the file system, and the data is going to be there if the container needs to read it, but if for any reason you need to recycle your container and replace it with a new one, any data that the application saved to disk is going to be lost forever.

A good design strategy for a container application is to make the application containers stateless. If you have a container that has application code and no data, you can throw it away and replace it with a new one without any problems, the container becomes truly disposable, which is great in terms of simplifying the deployment of upgrades.

But of course, this means that the data must be put somewhere outside of the application container. This is where the fantastic Docker ecosystem comes into play. The Docker Container Registry contains a large variety of container images. I have already told you about the Python container image, which I'm using as a base image for my Microblog container. In addition to that, Docker maintains images for many other languages, databases and other services in the Docker registry and if that isn't enough, the registry also allows companies to publish container images for their products, and also regular users like you or me to publish your own images. That means that the effort to install third party services is reduced to finding an appropriate image in the registry, and starting it with a docker run command with proper arguments.

So what I'm going to do now is create two additional containers, one for a MySQL database, and another one for the Elasticsearch service, and then I'm going to make the command line that starts the Microblog container even longer with options that enable it to access these two new containers.

Adding a MySQL Container

Like many other products and services, MySQL has public container images available on the Docker registry. Like my own Microblog container, MySQL relies on environment variables that need to be passed to docker run. These configure passwords, database names etc. While there are many MySQL images in the registry, I decided to use one that is officially maintained by the MySQL team. You can find detailed information about the MySQL container image in its registry page: https://hub.docker.com/r/mysql/mysql-server/.

If you remember the laborious process to set up MySQL in Chapter 17, you are going to appreciate Docker when you see how easy it is to deploy MySQL. Here is the docker run command that starts a MySQL server:

$ docker run --name mysql -d -e MYSQL_RANDOM_ROOT_PASSWORD=yes \
    -e MYSQL_DATABASE=microblog -e MYSQL_USER=microblog \
    -e MYSQL_PASSWORD=<database-password> \
    mysql/mysql-server:5.7

That is it! On any machine that you have Docker installed, you can run the above command and you'll get a fully installed MySQL server with a randomly generated root password, a brand new database called microblog, and a user with the same name that is configured with full permissions to access the database. Note that you will need to enter a proper password as the value for the MYSQL_PASSWORD environment variable.

Now on the application side, I need to add a MySQL client package, like I did for the traditional deployment on Ubuntu. I'm going to use pymysql once again, which I can add to the Dockerfile:

Dockerfile: Add pymysql to Dockerfile.

# ...
RUN venv/bin/pip install gunicorn pymysql
# ...

Any time a change is made to the application or the Dockerfile, the container image needs to be rebuilt:

$ docker build -t microblog:latest .

Any now I can start Microblog again, but this time with a link to the database container so that both can communicate through the network:

$ docker run --name microblog -d -p 8000:5000 --rm -e SECRET_KEY=my-secret-key \
    -e MAIL_SERVER=smtp.googlemail.com -e MAIL_PORT=587 -e MAIL_USE_TLS=true \
    -e MAIL_USERNAME=<your-gmail-username> -e MAIL_PASSWORD=<your-gmail-password> \
    --link mysql:dbserver \
    -e DATABASE_URL=mysql+pymysql://microblog:<database-password>@dbserver/microblog \
    microblog:latest

The --link option tells Docker to make another container accessible to this one. The argument contains two names separated by a colon. The first part is the name or ID of the container to link, in this case the one named mysql that I created above. The second part defines a hostname that can be used in this container to refer to the linked one. Here I'm using dbserver as generic name that represents the database server.

With the link between the two containers established, I can set the DATABASE_URL environment variable so that SQLAlchemy is directed to use the MySQL database in the other container. The database URL is going to use dbserver as the database hostname, microblog as the database name and user, and the password that you selected when you started MySQL.

One thing I noticed when I was experimenting with the MySQL container is that it takes a few seconds for this container to be fully running and ready to accept database connections. If you start the MySQL container and then start the application container immediately after, when the boot.sh script tries to run flask db migrate it may fail due to the database not being ready to accept connections. To make my solution more robust, I decided to add a retry loop in boot.sh:

boot.sh: Retry database connection.

#!/bin/sh
source venv/bin/activate
while true; do
    flask db upgrade
    if [[ "$?" == "0" ]]; then
        break
    fi
    echo Upgrade command failed, retrying in 5 secs...
    sleep 5
done
flask translate compile
exec gunicorn -b :5000 --access-logfile - --error-logfile - microblog:app

This loop checks the exit code of the flask db upgrade command, and if it is non-zero it assumes that something went wrong, so it waits five seconds and then retries.

Adding a Elasticsearch Container

The Elasticsearch documentation for Docker shows how to run the service as a single-node for development, and as a two-node production-ready deployment. For now I'm going to go with the single-node option and use the "oss" image, which only has the open source engine. The container is started with the following command:

$ docker run --name elasticsearch -d -p 9200:9200 -p 9300:9300 --rm \
    -e "discovery.type=single-node" \
    docker.elastic.co/elasticsearch/elasticsearch-oss:6.1.1

This docker run command has many similarities with the ones I've used for Microblog and MySQL, but there are a couple of interesting differences. First, there are two -p options, which means that this container is going to listen on two ports instead of just one. Both ports 9200 and 9300 are mapped to the same ports in the host machine.

The other difference is in the syntax used to refer to the container image. For the images that I've been building locally, the syntax was <name>:<tag>. The MySQL container uses a slightly more complete syntax with the format <account>/<name>:<tag>, which is appropriate to reference container images on the Docker registry. The Elasticsearch image that I'm using follows the pattern <registry>/<account>/<name>:<tag>, which includes the address of the registry as the first component. This syntax is used for images that are not hosted in the Docker registry. In this case Elasticsearch runs their own container registry service at docker.elastic.co instead of using the main registry maintained by Docker.

So now that I have the Elasticsearch service up and running, I can modify the start command for my Microblog container to create a link to it and set the Elasticsearch service URL:

$ docker run --name microblog -d -p 8000:5000 --rm -e SECRET_KEY=my-secret-key \
    -e MAIL_SERVER=smtp.googlemail.com -e MAIL_PORT=587 -e MAIL_USE_TLS=true \
    -e MAIL_USERNAME=<your-gmail-username> -e MAIL_PASSWORD=<your-gmail-password> \
    --link mysql:dbserver \
    -e DATABASE_URL=mysql+pymysql://microblog:<database-password>@dbserver/microblog \
    --link elasticsearch:elasticsearch \
    -e ELASTICSEARCH_URL=http://elasticsearch:9200 \
    microblog:latest

Before you run this command, remember to stop your previous Microblog container if you still have it running. Also be careful in setting the correct passwords for the database and the Elasticsearch service in the proper places in the command.

Now you should be able to visit http://localhost:8000 and use the search feature. If you experience any errors, you can troubleshoot them by looking at the container logs. You'll most likely want to see logs for the Microblog container, where any Python stack traces will appear:

$ docker logs microblog

The Docker Container Registry

So now I have the complete application up and running on Docker, using three containers, two of which come from publicly available third-party images. If you would like to make your own container images available to others, then you have to push them to the Docker registry from where anybody can obtain images.

To have access to the Docker registry you need to go to https://hub.docker.com and create an account for yourself. Make sure you pick a username that you like, because that is going to be used in all the images that you publish.

To be able to access your account from the command line, you need to log in with the docker login command:

$ docker login

If you've been following my instructions, you now have an image called microblog:latest stored locally on your computer. To be able to push this image to the Docker registry, it needs to be renamed to include the account, like the image from MySQL. This is done with the docker tag command:

$ docker tag microblog:latest <your-docker-registry-account>/microblog:latest

If you list your images again with docker images you are not going to see two entries for Microblog, the original one with the microblog:latest name, and a new one that also includes your account name. These are really two alias for the same image.

To publish your image to the Docker registry, use the docker push command:

$ docker push <your-docker-registry-account>/microblog:latest

Now your image is publicly available and you can document how to install it and run from the Docker registry in the same way MySQL and others do.

Deployment of Containerized Applications

One of the best things about having your application running in Docker containers is that once you have the containers tested locally, you can take them to any platform that offers Docker support. For example, you could use the same servers I recommended in Chapter 17 from Digital Ocean, Linode or Amazon Lightsail. Even the cheapest offering from these providers is sufficient to run Docker with a handful of containers.

The Amazon Container Service (ECS) gives you the ability to create a cluster of container hosts on which to run your containers, in a fully integrated AWS environment, with support for scaling and load balancing, plus the option to use a private container registry for your container images.

Finally, a container orchestration platform such as Kubernetes provides an even greater level of automation and convenience, by allowing you to describe your multi-container deployments in simple text files in YAML format, with load balancing, scaling, secure management of secrets and rolling upgrades and rollbacks.

50 comments

  • #26 Miguel Grinberg said 2018-07-15T16:35:20Z

    @Rumolu: the compilers need to be installed in the container, not in your local machine.

  • #27 Alexandr Tedeev said 2018-09-03T14:51:17Z

    It was helped to me to get rid of that problem: Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-install-_tu8nom_/cffi/ The command '/bin/sh -c venv/bin/pip install gunicorn pymysql' returned a non-zero code: 1 add in Docerfile that command setting additional packages to the image RUN apk add -U --no-cache gcc build-base linux-headers ca-certificates python3 python3-dev libffi-dev libressl-dev i put it before adduser command from this article: https://github.com/docker-library/python/issues/294

  • #28 Imam Digmi said 2018-09-06T13:32:53Z

    Great tutorial sir and I love it because this tutorial help me lot to learning Flask, but now i have some problem with docker build "docker build -t microblog:latest ." and i got this error: Step 8/18 : RUN venv/bin/pip install -r requirements.txt ---> Running in 581bcb877d3b Collecting alembic==1.0.0 (from -r requirements.txt (line 1)) Downloading https://files.pythonhosted.org/packages/96/c7/a4129db460c3e0ea8fea0c9eb5de6680d38ea6b6dcffcb88898ae42e170a/alembic-1.0.0-py2.py3-none-any.whl (158kB) Collecting asn1crypto==0.24.0 (from -r requirements.txt (line 2)) Downloading https://files.pythonhosted.org/packages/ea/cd/35485615f45f30a510576f1a56d1e0a7ad7bd8ab5ed7cdc600ef7cd06222/asn1crypto-0.24.0-py2.py3-none-any.whl (101kB) Collecting blinker==1.4 (from -r requirements.txt (line 3)) Downloading https://files.pythonhosted.org/packages/1b/51/e2a9f3b757eb802f61dc1f2b09c8c99f6eb01cf06416c0671253536517b6/blinker-1.4.tar.gz (111kB) Collecting certifi==2018.4.16 (from -r requirements.txt (line 4)) Downloading https://files.pythonhosted.org/packages/7c/e6/92ad559b7192d846975fc916b65f667c7b8c3a32bea7372340bfe9a15fa5/certifi-2018.4.16-py2.py3-none-any.whl (150kB) Collecting cffi==1.11.5 (from -r requirements.txt (line 5)) Downloading https://files.pythonhosted.org/packages/e7/a7/4cd50e57cc6f436f1cc3a7e8fa700ff9b8b4d471620629074913e3735fb2/cffi-1.11.5.tar.gz (438kB) Complete output from command python setup.py egg_info: No working compiler found, or bogus compiler options passed to the compiler from Python's standard "distutils" module. See the error messages above. Likely, the problem is not related to CFFI but generic to the setup.py of any Python package that tries to compile C code. (Hints: on OS/X 10.8, for errors about -mno-fused-madd see http://stackoverflow.com/questions/22313407/ Otherwise, see https://wiki.python.org/moin/CompLangPython or the IRC channel #python on irc.freenode.net.) ---------------------------------------- Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-install-7432kpk0/cffi/ You are using pip version 10.0.1, however version 18.0 is available. You should consider upgrading via the 'pip install --upgrade pip' command. The command '/bin/sh -c venv/bin/pip install -r requirements.txt' returned a non-zero code: 1 Thank a lot.

  • #29 Miguel Grinberg said 2018-09-06T17:56:01Z

    @Imam: your project needs to install the cffi package, which has a C portion that needs to be compiled. The error occurs because your container image does not have a C compiler available. Do you know which package depends on cffi? I don't recall it being a dependency in my project.

  • #30 Gerardo said 2018-09-21T22:52:12Z

    Miguel, Thanks for the great explanation. I am still confused on the persistence of the MySQL data. Wouldn't you need to run your "docker run" command with a -v my-local-folder:/path/to/db/data option? Can the mysql container autmatically create a non-ephemeral volume? Thanks, Gerardo

  • #31 Miguel Grinberg said 2018-09-22T13:34:55Z

    @Gerardo: this is explained in the MySQL container readme file, section "Where to Store Data". The default option does what you have described, the container creates and mounts a new volume in the host system. You can also provide your own volume if you want.

  • #32 Tej said 2018-10-10T06:55:00Z

    How to setup whole in windows as server? What are the extra feature should need to add?/ Can you please guide me? I can't access my page from someother system

  • #33 Miguel Grinberg said 2018-10-10T09:52:53Z

    @Tej: Windows is not a good server platform, I intentionally avoided it for deployment for that reason. You may be able to deploy using Docker containers following this chapter, that should work in the same way as in Unix, since the Windows version of Docker fully support unix based container images.

  • #34 William Mitchell said 2018-10-19T22:10:14Z

    I had the same sort of issue as a few of the others with the docker build of the pymysql step failing. After a bunch of googling and changes I came up with the following... FROM python:3.6-alpine RUN adduser -D microblog RUN apk update RUN apk add --no-cache binutils RUN apk add --no-cache gcc RUN apk add --no-cache musl-dev RUN apk add --no-cache mariadb-dev RUN apk add --no-cache mariadb-client RUN apk add --no-cache mysql-client ENV PYTHONUNBUFFERED 1 RUN pip install mysqlclient WORKDIR /home/microblog COPY requirements.txt requirements.txt RUN python -m venv venv RUN venv/bin/pip install -r requirements.txt RUN venv/bin/pip install gunicorn RUN apk add --no-cache --virtual .pynacl_deps build-base python3-dev libffi-dev RUN venv/bin/pip install setuptools RUN venv/bin/pip install PyMySQL COPY app app COPY migrations migrations COPY microblog.py config.py boot.sh ./ RUN chmod +x boot.sh ENV FLASK_APP microblog.py RUN chown -R microblog:microblog ./ USER microblog EXPOSE 5000 ENTRYPOINT ["./boot.sh"]

  • #35 Miguel Grinberg said 2018-10-20T09:03:30Z

    @William: while your solution works, you are now installing a lot of things in your container image that are not needed at run time. The main reason to use Alpine Linux container images is because they are small, with all the things you are installing you cancel out that big benefit. Instead, consider installing a slightly older version of pymysql, which does not have this problem. Change "pip install pymysql" to "pip install pymysql<0.9" and your container will build without having to install all these extra dependencies.

  • #36 Daniel D said 2018-10-22T02:16:49Z

    Thanks for the wonderful work! I followed the instructions carefully and even fixed some issues I was having with Windows 10 and Docker/Git. I created all the containers and they are running but I still can't connect to the host. Any thoughts on how I can find what the exact issue is?

  • #37 JP Centeno said 2018-10-23T10:17:23Z

    Hi Miguel, my application will run a GPU process in the background and update the user as a long list of GPU tasks is complete. Do you think the NVIDIA-DOCKER (https://github.com/NVIDIA/nvidia-docker/blob/master/README.md) container could be linked to the app container just as the MySQL container in your example? This is my first time using containers. Thanks for the great tutorial!

  • #38 Miguel Grinberg said 2018-10-23T17:52:09Z

    @Daniel: you need to be more specific. What do you mean by "can't connect to the host"? How do you connect? What happens? Do you get any errors?

  • #39 Miguel Grinberg said 2018-10-23T18:04:24Z

    @JP: you should be able to link your container to the main app container, just like with the database. Your app container will then have access to the hostname of the nvidia-docker container. I have never used GPU containers myself, I assume having the hostname is enough to query the container for the status of the GPU process?

  • #40 Dmytro said 2018-10-27T15:27:25Z

    Hello Miguel. Thank you for tutorial! One question: how what about adding chapter with docker-compose explanation? As for me that more friendly than linking

  • #41 Miguel Grinberg said 2018-10-27T18:15:41Z

    @Dmytro: I prefer to show the more basic form of linking, because once you understand that you can easily migrate to the higher-level docker-compose without a problem. I may consider replacing the linking with a network, but I think I would still use the docker command.

  • #42 Gang Chen said 2018-10-31T07:47:33Z

    Mr. Grinberg, thank you so much for the updated tutorial. I really enjoyed learning Flask from zero to here. I am recommending your tutorials to my whole team, persuading my boss to purchase copies of your book. I see that you are using three containers in this chapter, is it possible that you add a docker-compose file to get them all together, instead of using command lines? I would be really interested in contents like this. Thanks.

  • #43 Miguel Grinberg said 2018-10-31T08:17:56Z

    @Gang: Docker compose files are simply representations of regular docker command lines. It would be fairly easy to build a docker compose file that matches the container set up in this article. I decided not to include it because a) this chapter is fairly long as it is, and b) docker-compose encourages people to not really understand how everything works, and I prefer that you learn how to design a container solution piece by piece. But you should be able to create a docker compose file easily from the command lines I present here.

  • #44 Abhishek meena said 2018-11-25T02:48:53Z

    hello miguel first of all i want to thankyou for your work and dedication of this flask tutorial . i am following the exact tutorial steps to build docker image although my image was successfully build but when i do docker ps it shows an empty list . please help

  • #45 Miguel Grinberg said 2018-11-25T08:37:43Z

    @Abhishek: the "docker ps" commands shows actively running containers. To see the list of built images use "docker images".

  • #46 yawpitch said 2018-12-01T12:52:48Z

    For all those having issues with CFFI building on alpine, adding: RUN apk add gcc musl-dev python3-dev libffi-dev openssl-dev To the Dockerfile soemwhere before attempting to install pymysql fixed this. The version of pip on the python:3.6-alpine image is rather old and I thought that might be why it's not picking up a wheel -- this was the issue with the Vagrant box in Chapter 17 -- but apparently there isn't a wheel compatible with Alpine and you have to build from source. BTW, that's got to have been the most ridiculous CAPTCHA I've ever had to deal with.

  • #47 Miguel Grinberg said 2018-12-02T08:53:35Z

    @yawpitch: your solution is a quick workaround, but not something I would use for real. Adding all those dependencies to your container will bloat your image. A much better solution is to install an older version of pymysql, from before this problem was introduced. Hopefully they'll address the problem soon: pip install pymysql<0.9

  • #48 Ka Chun said 2018-12-02T11:23:35Z

    HI miguel, Thanks a lot for your great tutorial. I had the same error code as Imam Digmi raised above ***************** Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-install-1sg4i_2h/cffi/ You are using pip version 10.0.1, however version 18.1 is available. You should consider upgrading via the 'pip install --upgrade pip' command ******************** I read your answer -> "your project needs to install the cffi package, which has a C portion that needs to be compiled. " I checked that i already have the updated cffi package installed. But the error code is still the same. I have listed my requirements.txt below. Can you advise how can i work alrdy? Thanks. requirements.txt alembic==1.0.0 amqp==2.2.2 anyjson==0.3.3 Babel==2.6.0 beautifulsoup4==4.6.0 billiard==3.5.0.3 blinker==1.4 celery==4.1.0 certifi==2018.4.16 cffi==1.11.5 chardet==3.0.4 click==6.7 DateTime==4.2 Django==2.0.3 django-adaptors==0.2.5 django-bootstrap3==10.0.1 django-braces==1.13.0 django-celery==3.2.2 django-celery-results==1.0.1 dominate==2.3.4 elasticsearch==6.3.1 fake-useragent==0.1.10 Flask==0.12.2 Flask-Babel==0.11.2 Flask-Bootstrap==3.3.7.1 Flask-HTTPAuth==3.2.4 Flask-Login==0.4.1 Flask-Mail==0.9.1 Flask-Migrate==2.2.1 Flask-Moment==0.6.0 Flask-SocketIO==3.0.0 Flask-SQLAlchemy==2.3.2 Flask-WTF==0.14.2 guess-language-spirit==0.5.3 gunicorn==19.9.0 httpie==1.0.0 idna==2.6 itsdangerous==0.24 Jinja2==2.9.6 kombu==4.1.0 lxml==4.1.1 Mako==1.0.7 MarkupSafe==1.0 misaka==2.1.0 numpy==1.13.1 opencv-python==3.2.0.8 pandas==0.22.0 pycparser==2.18 Pygments==2.2.0 PyJWT==1.6.4 python-dateutil==2.6.1 python-dotenv==0.9.1 python-editor==1.0.3 python-engineio==2.3.2 python-socketio==2.0.0 pytz==2018.3 redis==2.10.6 requests==2.18.4 rq==0.12.0 selenium==3.12.0 six==1.11.0 SQLAlchemy==1.2.12 urllib3==1.22 vine==1.1.4 virtualenv==15.1.0 visitor==0.1.3 Werkzeug==0.12.2 WTForms==2.2.1 yandex.translate==0.3.5 zope.interface==4.4.3

  • #49 Miguel Grinberg said 2018-12-02T17:06:27Z

    @Ka Chun: As I said a few times in previous comments, use "pip install pymysql<0.9" to avoid this problem. Hopefully the pymysql project will address this problem soon and release a fix.

  • #50 yawpitch said 2018-12-03T11:48:09Z

    Hi Miguel, fair enough on using the lower version of pymysql, but might be helpful to make clear that if you do the command inside the Dockerfile it will need to be: RUN venv/bin/pip/install gunicorn 'pymysql<0.9' It's not entirely obvious to someone new to docker that you've got to have the single quotes around it or the underlying shell thinks you're trying to do redirection.

Leave a Comment