Running the App Image - Debug and Production Modes

Running images in Docker

In the last chapter, we built our app image. Now, it’s time to run that image using docker run command. This command has multiple options. If you type docker run --help in the terminal, you’ll get plenty of options for this command, but the right syntax is below.

Usage: docker run [OPTIONS] IMAGE [COMMAND] [ARG…]

[OPTIONS] - any argument related to running the image

[COMMAND] - any command to override CMD if ENTRYPOINT is implemented in Dockerfile

[ARG] - arguments for container shell

Running the app image

If you remember, we previously built our image using flask_app:1.0 tag. Type docker images into the terminal and verify that your images exist on your system.

The output will be similar to

REPOSITORY                                 TAG                 IMAGE ID            CREATED             SIZE
flask_app                                  1.0                 3edb8369bc83        8 minutes ago       952MB

The debug mode

It’s time to run our app using Docker.

Type docker run flask_app:1.0

You’ll get output something like so:

 * Serving Flask app "app.py" (lazy loading)
 * Environment: development
 * Debug mode: on
 * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 132-810-040

At this point, you won’t be able to access the app from your browser.

Now, stop the app using ctrl + c and rerun using docker run -p 5000:5000 flask_app:1.0 . There will be no change in output, but try accessing your app in the browser using localhost:5000.

Cheers!! You got it running.

What just happened? What makes the app accessible?

-p 5000:5000 option in run command

Before adding this option, the app was running in the container on the container’s 5000 port. There is no way the host machine will reach into the container. However, the -p 5000:5000 option will create a pipe/tunnel on the host machine’s 5000 port to the container’s 5000 port.

This is called port mapping. You can create interfaces to establish communication between host and container. We will soon see how to make the host’s file system accessible from the container.

Notice a few things in the output:

Debug mode: on
Environment: development

Since we set FLASK_ENV=development in .flaskenv file. Our app is running in debug mode.

Get hands-on with 1400+ tech skills courses.