Build Flask App With Docker Mac

Using docker to deploy flask application

Jun 05, 2018  CMD 'python', 'app.py' – This sets the command to be executed when running the image python app.py (the command required to run our Flask application) Now that we understand exactly what’s in here, let’s make our new Docker image. We’ll build the docker image locally with: sudo docker build -t cda-flask-app. Then let’s test our image. To build we use this Docker CLI command: docker build -t vmdockerflask. The command docker build will build our image, with the flag -t it put the tag vmdockerflask in our image and, finally, the last part of the CLI is. (dot) that inform to Docker that the Dockerfile is in the current directory. In this project you will containerize and deploy a Flask API to a Kubernetes cluster using Docker, AWS EKS, CodePipeline, and CodeBuild. The Flask app that will be used for this project consists of a simple API with three endpoints: GET '/': This is a simple health check, which returns the. Multi-container Environments. In the last section, we saw how easy and fun it is to run applications with Docker. We started with a simple static website and then tried a Flask app. FROM ubuntu:latest RUN apt-get update -y RUN apt-get install -y python-pip python-dev build-essential COPY. /app WORKDIR /app RUN pip install -r /app/requirements.txt ENTRYPOINT 'python' CMD 'app.py' The Docker image was built using docker build -t flask-app. Python 3, specifically the slim-3.6.5 version from Docker Hub; Flask version 1.0.2; All of the code for the Dockerfile and the Flask app are available open source under the MIT license on GitHub under the docker-flask-mac directory of the blog-code-examples repository. Use the code for your own purposes as much as you like.

1. First, prepare a small flash application, as follows. Easy enough! Note that this deployment does not involve databases and nginx agents. In fact, these services should be deployed to different servers.

2. Create and write in the project directoryDockerfileFile, the name can’t be wrong. Because there are few Python packages involved in this flash application, there is no production requirements.txt Documents. The following are the contents of the project directory structure and dockerfile file:

  • Copy: when copy, only the contents of the folder will be copied to workdir, and the folder flaskapp will not be copied
  • Expose: 5000 port for external exposure
  • CMD: the container executes the command of the shell when it runs. Here, gunicorn is used as the WSGI server

3. Shell terminal CD to flash_ In the test directory, execute the create image command first. After the creation is successful, run the container according to the image.

The results are as follows:

4. At this time, enter 127.0.0.1:5000 in the browser to access the flask application in the container.

Estimated reading time: 8 minutes

Prerequisites

Work through the orientation and setup in Part 1.

Introduction

Now that you’ve set up your development environment, you can begin to develop containerized applications. In general, the development workflow looks like this:

  1. Create and test individual containers for each component of your application by first creating Docker images.

  2. Assemble your containers and supporting infrastructure into a complete application.

  3. Test, share, and deploy your complete containerized application.

In this stage of the tutorial, let’s focus on step 1 of this workflow: creating the images that your containers will be based on. Remember, a Docker image captures the private filesystem that your containerized processes will run in; you need to create an image that contains just what your application needs to run.

Set up

Let us download the node-bulletin-board example project. This is a simple bulletin board application written in Node.js.

Git

If you are using Git, you can clone the example project from GitHub:

Build Flask App With Docker Mac

Windows (without Git)

If you are using a Windows machine and prefer to download the example project without installing Git, run the following commands in PowerShell:

Mac or Linux (without Git)

If you are using a Mac or a Linux machine and prefer to download the example project without installing Git, run the following commands in a terminal:

Define a container with Dockerfile

After downloading the project, take a look at the file called Dockerfile in the bulletin board application. Dockerfiles describe how to assemble a private filesystem for a container, and can also contain some metadata describing how to run a container based on this image.

For more information about the Dockerfile used in the bulletin board application, see Sample Dockerfile.

Build and test your image

Now that you have some source code and a Dockerfile, it’s time to build your first image, and make sure the containers launched from it work as expected.

Make sure you’re in the directory node-bulletin-board/bulletin-board-app in a terminal or PowerShell using the cd command. Run the following command to build your bulletin board image:

You’ll see Docker step through each instruction in your Dockerfile, building up your image as it goes. If successful, the build process should end with a message Successfully tagged bulletinboard:1.0.

Windows users:

This example uses Linux containers. Make sure your environment is running Linux containers by right-clicking on the Docker logo in your system tray, and clicking Switch to Linux containers. Don’t worry - all the commands in this tutorial work the exact same way for Windows containers.

You may receive a message titled ‘SECURITY WARNING’ after running the image, noting the read, write, and execute permissions being set for files added to your image. We aren’t handling any sensitive information in this example, so feel free to disregard the warning in this example.

Run your image as a container

  1. Run the following command to start a container based on your new image:

    There are a couple of common flags here:

    • --publish asks Docker to forward traffic incoming on the host’s port 8000 to the container’s port 8080. Containers have their own private set of ports, so if you want to reach one from the network, you have to forward traffic to it in this way. Otherwise, firewall rules will prevent all network traffic from reaching your container, as a default security posture.
    • --detach asks Docker to run this container in the background.
    • --name specifies a name with which you can refer to your container in subsequent commands, in this case bb.
  2. Visit your application in a browser at localhost:8000. You should see your bulletin board application up and running. At this step, you would normally do everything you could to ensure your container works the way you expected; now would be the time to run unit tests, for example.

  3. Once you’re satisfied that your bulletin board container works correctly, you can delete it:

    The --force option stops a running container, so it can be removed. If you stop the container running with docker stop bb first, then you do not need to use --force to remove it.

Conclusion

At this point, you’ve successfully built an image, performed a simple containerization of an application, and confirmed that your app runs successfully in its container. The next step will be to share your images on Docker Hub, so they can be easily downloaded and run on any destination machine.

Deploying to the cloud

To run your containers in the cloud with either Azure or AWS, check out our docs on getting started with cloud deployments.

Sample Dockerfile

Docker Hub Flask

Writing a Dockerfile is the first step to containerizing an application. You can think of these Dockerfile commands as a step-by-step recipe on how to build up your image. The Dockerfile in the bulletin board app looks like this:

The dockerfile defined in this example takes the following steps:

  • Start FROM the pre-existing node:current-slim image. This is an official image, built by the node.js vendors and validated by Docker to be a high-quality image containing the Node.js Long Term Support (LTS) interpreter and basic dependencies.
  • Use WORKDIR to specify that all subsequent actions should be taken from the directory /usr/src/appin your image filesystem (never the host’s filesystem).
  • COPY the file package.json from your host to the present location (.) in your image (so in this case, to /usr/src/app/package.json)
  • RUN the command npm install inside your image filesystem (which will read package.json to determine your app’s node dependencies, and install them)
  • COPY in the rest of your app’s source code from your host to your image filesystem.

You can see that these are much the same steps you might have taken to set up and install your app on your host. However, capturing these as a Dockerfile allows you to do the same thing inside a portable, isolated Docker image.

Docker Flask Mysql

The steps above built up the filesystem of our image, but there are other lines in your Dockerfile.

The CMD directive is the first example of specifying some metadata in your image that describes how to run a container based on this image. In this case, it’s saying that the containerized process that this image is meant to support is npm start.

Python docker flask

The EXPOSE 8080 informs Docker that the container is listening on port 8080 at runtime.

What you see above is a good way to organize a simple Dockerfile; always start with a FROM command, follow it with the steps to build up your private filesystem, and conclude with any metadata specifications. There are many more Dockerfile directives than just the few you see above. For a complete list, see the Dockerfile reference.

CLI references

Build Flask App With Docker Mac Os

Further documentation for all CLI commands used in this article are available here:

Docker Flask App

containers, images, dockerfiles, node, code, coding, build, push, run

Comments are closed.