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.

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

While Docker isn't the only container platform, it is by far the most popular, so that's going to be my choice.

To work with Docker, 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 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 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
 Cloud integration: 1.0.14
 Version:           20.10.6
 API version:       1.41
 Go version:        go1.16.3
 Git commit:        370c289
 Built:             Fri Apr  9 22:46:57 2021
 OS/Arch:           darwin/amd64
 Context:           default
 Experimental:      true

Server: Docker Engine - Community
  Version:          20.10.6
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.13.15
  Git commit:       8728dd2
  Built:            Fri Apr  9 22:44:56 2021
  OS/Arch:          linux/amd64
  Experimental:     false
  Version:          1.4.4
  GitCommit:        05f951a3781f4f2c1911b05e61c160e9c30eaa8e
  Version:          1.0.0-rc93
  GitCommit:        12644e614e25b05da6fd08a38ffa0cfe1903fdec
  Version:          0.19.0
  GitCommit:        de40ad0

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:slim

RUN useradd 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

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 slim tag selects a container image that has only the minimal packages required to run the Python interpreter. You can see what other tags are available for Python 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 useradd 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.

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       03978d7e1007    27 seconds ago       283MB
python        slim         c2f204720fdd    11 days ago          115MB

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

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">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

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> \

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> \

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, along with the cryptography package that it uses for authentication against the MySQL server:

Dockerfile: Add pymysql and cryptography to Dockerfile.

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

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 \

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 upgrade 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.

source venv/bin/activate
while true; do
    flask db upgrade
    if [[ "$?" == "0" ]]; then
    echo Upgrade command failed, retrying in 5 secs...
    sleep 5
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" \

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 \

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 now 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.


  • #76 Miguel Grinberg said 2019-03-13T09:34:17Z

    @JJ: are you using docker containers during development? This tutorial shows you how you can deploy your application to containers, but it does not imply you should also develop using them. the "flask db migrate" command is issued during development, so you do not need to run it in a container.

  • #77 spmsh said 2019-03-16T03:55:41Z

    Hi Miguel, I've read and followed your 3 articles on deployment options several times for my projects, however, I have one case that requires a deployment on windows platform.

    My project is using your flask socketio. Since socketio.run(app) runs a production ready server when eventlet or gevent are installed, which is the case for my project with eventlet, I was wondering if you had experience or feedback on potential problem running this on windows ?

    I have installed my flask app as a windows service, it seems working fine, but not sure what the behavior might be in the long run. Thanks !

  • #78 Miguel Grinberg said 2019-03-16T10:31:59Z

    @spmsh: I do not have experience with running production servers on Windows. My recommendation would be that you use one of the several Linux on Windows options and do a deployment using the more robust tools the Unix side offers. For example, you can run Linux based Docker containers on Windows. Or you can create a Linux VM.

  • #79 Eugene said 2019-03-28T00:10:13Z

    During running docker build I have error: "pg_config executable not found" There is similar error on SO: https://stackoverflow.com/questions/46711990/error-pg-config-executable-not-found-when-installing-psycopg2-on-alpine-in-dock

  • #80 Miguel Grinberg said 2019-03-28T10:05:21Z

    @Eugene: My guess is that there are no wheel packages for psycopg2 that work on Alpine Linux, so the package needs to be compiled from source. And to compile it from source you need to install the postgres libraries. This is shown in the SO question that you linked.

  • #81 Евгений said 2019-03-28T12:08:24Z

    But what about how to configure saving data outer from container?

  • #82 Miguel Grinberg said 2019-03-28T19:54:09Z

    @Евгений: The MySQL docker image is configured to save data to a volume that is outside of the MySQL container. See the "Where to store data" section of the MySQL image documentation: https://hub.docker.com/_/mysql. This solution uses the first option discussed there.

  • #83 Jo said 2019-04-30T07:54:17Z

    What I see when running flask db upgrade on a fresh mySQL database is an error message like sqlalchemy.exc.InternalError: (pymysql.err.InternalError) (1050, "Table 'user' already exists").

    Is there something I could do about that? I assume it is related with the alembic database migrations but my knowledge comes only from your tutorial and seems not to be sufficient to resolve this. Shouldn't alembic check if a table exists already and skip this step?

  • #84 Miguel Grinberg said 2019-04-30T11:24:06Z

    @Jo: it's not a fresh database, the error indicates that your database already contains a table named "users".

  • #85 Vincent Liagre said 2019-06-12T20:58:38Z


    Thank you so much for all the work. This is of great value. I have encountered 3 problems in this chapter:

    I am having difficulties installing the spirit package when building the image ; the version is 1.8.8. Is there another, more stable version? In the meantime I have chosen to skip it.

    Collecting spirit==1.8.8 (from -r requirements.txt (line 33)) Downloading https://files.pythonhosted.org/packages/bd/da/543150556d8b91f58ec089eae34a857a34c9e454a22a1345d07f38fb54d2/spirit-1.8.8.tar.gz Complete output from command python setup.py egg_info: Traceback (most recent call last): File "", line 1, in File "/tmp/pip-install-unrup76p/spirit/setup.py", line 13, in from wheel.bdist_wheel import bdist_wheel as bdist_wheel_ ModuleNotFoundError: No module named 'wheel'


    Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-install-unrup76p/spirit/

    It seems that all containers that I am launching are exiting right after I launch them, with status code other than 0 (1 or 2). Do you have any idea why this might be?

    CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 4e9fb2662802 mysql/mysql-server:5.7 "/entrypoint.sh mysq…" 3 hours ago Exited (1) 3 hours ago mysql dc163dd9eb97 ac94059bfada "/bin/sh -c 'venv/bi…" 7 hours ago Exited (2) 7 hours ago determined_ardinghelli d6605e15d868 a9bde9d463d0 "/bin/sh -c 'venv/bi…" 7 hours ago Exited (1) 7 hours ago gifted_payne 1d53163009eb 80c8ff81e909 "/bin/sh -c 'venv/bi…" 2 days ago Exited (1) 2 days ago loving_ellis

    Therefore, when I am trying to launch the microblog container linked to the MySQL container, I am getting the following error: 'file not found'. I guess this is related to my point above.

    I can't seem to run the ElasticSearch container ; I am getting the following error:

    7.1.1: Pulling from elasticsearch/elasticsearch 8ba884070f61: Pull complete bc017bf6a4d5: Pull complete 66463b85d389: Pull complete 837fa1486a34: Pull complete 4e79d4f9236b: Pull complete fca20e6168be: Pull complete d286e388da12: Pull complete Digest: sha256:1084c64eed7d9318d028361c9aee398afdeb70d1816ce81d590b9450ec542c08 Status: Downloaded newer image for docker.elastic.co/elasticsearch/elasticsearch:7.1.1 OpenJDK 64-Bit Server VM warning: Option UseConcMarkSweepGC was deprecated in version 9.0 and will likely be removed in a future release. OpenJDK 64-Bit Server VM warning: INFO: os::commit_memory(0x00000000ca660000, 899284992, 0) failed; error='Not enough space' (errno=12)

    There is insufficient memory for the Java Runtime Environment to continue. Native memory allocation (mmap) failed to map 899284992 bytes for committing reserved memory. An error report file with more information is saved as: logs/hs_err_pid1.log

    Any idea why this might be ?

    Thank you for all the help,



  • #86 Miguel Grinberg said 2019-06-12T21:52:50Z

    @Vincent: 1. There is no package called "spirit" in this project. There is one called "guess-language_spirit", however. 2. If you run "docker logs " you will see any error messages that are causing your container(s) to exit. 3. You need to solve #2. 4. You are running out of RAM in your computer. Elasticsearch requires a lot of RAM, unfortunately.

  • #87 John said 2019-07-27T16:11:30Z

    I tried using this with "mysql/mysql-server:latest" (5.7 works fine)

    Any idea how to fix this? sqlalchemy.exc.OperationalError: (pymysql.err.OperationalError) (1045, "Access denied for user 'microblog'@'' (using password: NO)")

  • #88 Miguel Grinberg said 2019-07-27T17:02:59Z

    @John: do you have a fairly recent version of the pymysql package? The latest release of MySQL is version 8, I would imagine an older pymysql would not be able to connect to it.

  • #89 John said 2019-07-27T18:35:08Z

    Thank you for your very quick reply. With latest versions of all packages in requirements.txt as well as pymysql I get this from docker logs:

    ERROR [root] Error: cryptography is required for sha256_password or caching_sha2_password

    I did some googling and understood that the authentication has been changed, some recommended to "install default-authentication-plugin=mysql_native_password ", but I do not quite understand to figure out how to install this, Ideally I would go with the new authentication, but as the logs says: adding cryptography to the log file results in this:

    Command "/home/microblog/venv/bin/python /home/microblog/venv/lib/python3.7/site-packages/pip install --ignore-installed --no-user --prefix /tmp/pip-build-env-p3mfmhk1/overlay --no-warn-script-location --no-binary :none: --only-binary :none: -i https://pypi.org/simple -- setuptools>=40.6.0 wheel "cffi>=1.8,!=1.11.3; python_implementation != 'PyPy'"" failed with error code 1 in None

  • #90 Miguel Grinberg said 2019-07-27T22:01:10Z

    @John: Your install is trying to build the cryptography package from source instead of installing a wheel package. I'm not sure why that is, it could be a problem with pip being too old (try "pip install --upgrade pip"), or else it could also be a problem with Alpine Linux, maybe try an Ubuntu container image instead.

  • #91 John said 2019-07-28T11:11:15Z

    Thanks! Upgrading pip didn't work with alipine; still wouldn't install cryptography.

    However with the following changes it worked:

    Dockerfile: FROM python:3.7.4-buster RUN adduser --disabled-password --gecos '' microblog

    requirements.txt: cryptography



    Only problem now is that the buster image is quite huge. As i am planning to start with some free hosting services with a limitation of the image size. Can you think of any other images that might are a little bit smaller? I guess the best option in this case is to stick with Alpine and MYSQL 5.7? However my concern is to use an old MYSQL in terms of security, or shouldn't I be concerned about this as long as it is mainted (which I guess it is?)

    Thanks again!

  • #92 John said 2019-07-28T11:30:38Z

    Hello again,

    Adding this to dockerfile did the trick with alipine and MYSQL 8.0:

    RUN apk add gcc musl-dev python3-dev libffi-dev openssl-dev

  • #93 Miguel Grinberg said 2019-07-28T11:35:37Z

    @John: Alpine is the standard image when size matters, there might be others, but I don't really know any that is widely used. There are some configurations for building cryptography inside Alpine in this GitHub issue: https://github.com/pyca/cryptography/issues/4264. Also note that MySQL 5.7 is going to be maintained until 2023 (see https://endoflife.software/applications/databases/mysql).

  • #94 Miguel Grinberg said 2019-07-28T14:44:30Z

    @John: great, but be aware that installing the C++ compiler and additional libraries will also make your container bigger, so it is still better to use a wheel package once one is released that works well under Alpine.

  • #95 adnan said 2019-08-20T10:02:00Z

    Thank you but I noticed that you did not executed flask init and flask migrate anywhere, why? where I should place them?

  • #96 Miguel Grinberg said 2019-08-20T15:07:16Z

    @adnan: you need to go back to Chapter 4 for how to work with the migration repository. This chapter is about deployment, not database setup.

  • #97 Andres Mata said 2019-08-21T19:07:47Z

    Hi Miguel,

    Thanks a lot for you awesome tutorial. I try to do a implementation very close to yours. But I need to deploy it, in Azure cloud. I can run my web app in one container and the mysql in another. In my local machine it works fine. But, in the Azure cloud it doesn't. I follow the indications in https://docs.microsoft.com/en-us/azure/app-service/containers/tutorial-multi-container-app. They have a wordpress image running using a mysql container as backend. I made a similar configuration for mi app:

    version: '3.3'

    services: db: image: mysql:5.7 volumes: - db_data:/var/lib/mysql restart: always environment: #MYSQL_RANDOM_ROOT_PASSWORD: 'yes' MYSQL_ROOT_PASSWORD: prueba_app MYSQL_DATABASE: twitter_app MYSQL_USER: twitter_app MYSQL_PASSWORD: my_password

    twitter_app: depends_on: - db image: xxxxxxxxxx.azurecr.io/twitter_app:latest ports: - "80:5000" restart: always environment: FLASK_APP: twitter_app.py FLASK_CONFIG: docker SECRET_KEY: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx MAIL_USERNAME: a_valid_mail@gmail.com MAIL_PASSWORD: a_valid_mail_password DATABASE_URL: mysql+pymysql://twitter_app:my_password@db:3306/twitter_app volumes: db_data:

    I tried with mysql+pymysql://twitter_app:my_password@db/twitter_app and with mysql+pymysql://twitter_app:my_password@db:3306/twitter_app, but neither works. It seems a problem with pymsql:

    2019-08-21 18:59:00.140 INFO - Container logs from twitterapp07_twitter_app_0 = 2019-08-21T18:55:32.869232053Z docker 2019-08-21T18:55:32.940945190Z Traceback (most recent call last): 2019-08-21T18:55:32.940981592Z File "/twitter_app/venv/lib/python3.6/site-packages/pymysql/connections.py", line 916, in connect 2019-08-21T18:55:32.940989692Z kwargs) 2019-08-21T18:55:32.940993992Z File "/usr/local/lib/python3.6/socket.py", line 724, in create_connection 2019-08-21T18:55:32.940998592Z raise err 2019-08-21T18:55:32.941002792Z File "/usr/local/lib/python3.6/socket.py", line 713, in create_connection 2019-08-21T18:55:32.941007293Z sock.connect(sa) 2019-08-21T18:55:32.941011393Z ConnectionRefusedError: [Errno 111] Connection refused 2019-08-21T18:55:32.941015293Z 2019-08-21T18:55:32.941019093Z During handling of the above exception, another exception occurred: 2019-08-21T18:55:32.941023093Z 2019-08-21T18:55:32.941027093Z Traceback (most recent call last): 2019-08-21T18:55:32.941030993Z File "/twitter_app/venv/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 2262, in _wrap_pool_connect 2019-08-21T18:55:32.941035294Z return fn() 2019-08-21T18:55:32.941039094Z File "/twitter_app/venv/lib/python3.6/site-packages/sqlalchemy/pool/base.py", line 303, in unique_connection 2019-08-21T18:55:32.941043294Z return _ConnectionFairy._checkout(self) 2019-08-21T18:55:32.941047194Z File "/twitter_app/venv/lib/python3.6/site-packages/sqlalchemy/pool/base.py", line 760, in _checkout 2019-08-21T18:55:32.941051994Z fairy = _ConnectionRecord.checkout(pool) 2019-08-21T18:55:32.941055894Z File "/twitter_app/venv/lib/python3.6/site-packages/sqlalchemy/pool/base.py", line 492, in checkout 2019-08-21T18:55:32.941060095Z rec = pool._do_get() 2019-08-21T18:55:32.941064095Z File "/twitter_app/venv/lib/python3.6/site-packages/sqlalchemy/pool/impl.py", line 238, in _do_get 2019-08-21T18:55:32.941068395Z return self._create_connection() 2019-08-21T18:55:32.941072295Z File "/twitter_app/venv/lib/python3.6/site-packages/sqlalchemy/pool/base.py", line 308, in _create_connection 2019-08-21T18:55:32.941076495Z return _ConnectionRecord(self) 2019-08-21T18:55:32.941080595Z File "/twitter_app/venv/lib/python3.6/site-packages/sqlalchemy/pool/base.py", line 437, in __init__ 2019-08-21T18:55:32.941084595Z self.__connect(first_connect_check=True) 2019-08-21T18:55:32.941088596Z File "/twitter_app/venv/lib/python3.6/site-packages/sqlalchemy/pool/base.py", line 639, in __connect 2019-08-21T18:55:32.941092696Z connection = pool._invoke_creator(self) 2019-08-21T18:55:32.941096796Z File "/twitter_app/venv/lib/python3.6/site-packages/sqlalchemy/engine/strategies.py", line 114, in connect 2019-08-21T18:55:32.941100896Z return dialect.connect(*cargs, cparams) 2019-08-21T18:55:32.941104996Z File "/twitter_app/venv/lib/python3.6/site-packages/sqlalchemy/engine/default.py", line 453, in connect 2019-08-21T18:55:32.941115496Z return self.dbapi.connect(*cargs, cparams) 2019-08-21T18:55:32.941119897Z File "/twitter_app/venv/lib/python3.6/site-packages/pymysql/__init__.py", line 90, in Connect 2019-08-21T18:55:32.941125497Z return Connection(*args, kwargs) 2019-08-21T18:55:32.941129697Z File "/twitter_app/venv/lib/python3.6/site-packages/pymysql/connections.py", line 706, in init 2019-08-21T18:55:32.941133797Z self.connect() 2019-08-21T18:55:32.941137797Z File "/twitter_app/venv/lib/python3.6/site-packages/pymysql/connections.py", line 963, in connect 2019-08-21T18:55:32.941141897Z raise exc 2019-08-21T18:55:32.941145698Z pymysql.err.OperationalError: (2003, "Can't connect to MySQL server on 'db' ([Errno 111] Connection refused)")

    Do you have any suggestion o pointer to try to solve the problem? Thanks in advance!
  • #98 Miguel Grinberg said 2019-08-21T22:26:10Z

    @Andres: Looks like you did not set up a link in your app container to the database container.

  • #99 David said 2019-08-27T14:55:09Z

    Many thanks for this tutorial...I've learned a bunch. But now I'm stuck. I have all three containers running and the logs are showing no errors. But when I go to I get "Site can't be reached". And the same for How can I go about debugging this?

  • #100 Miguel Grinberg said 2019-08-28T07:18:55Z

    The isn's a valid IP address. Use the IP address assigned to your Docker host instead.

Leave a Comment