(10) Docker compose installation and creation of flask Web Apps

Source: Internet
Author: User
Tags control characters docker hub docker compose

Docker Compose is one of the Docker orchestration (Orchestration) projects that are responsible for quickly deploying distributed applications in a cluster.

Dockerfile lets users manage a single application container, while Compose allows the user to define a set of associated application containers (called a project, i.e., projects) in a template (YAML format), such as a Web service container plus a backend database service container.

Installation

The project was written by Python and actually invokes the API provided by Docker. by pip Installing.

$ python3 -m pip install docker-compose

After the installation is successful, you can view docker-compose the use of the command.

and run multi-container applications with Docker.Usage:  docker-compose [-f <arg>...] [options] [COMMAND] [ARGS...]  docker-compose -h|--helpOptions:  -f, --file FILE             Specify an alternate compose file (default: docker-compose.yml)  -p, --project-name NAME     Specify an alternate project name (default: directory name)  --verbose                   Show more output  --no-ansi                   Do not print ANSI control characters  -v, --version               Print version and exit -H, --host HOST Daemon socket to connect to ……
Create a Flask app

Create a web app that uses flask and count the values into Redis.

1. Create a Web app

Create a Flask web App, app.py file:

 from flask import Flaskfrom redis import Redisapp = Flask (__name__) Redis = Redis ( Host= ' Redis ', Port=6379)  @app. Route ( '/') def hello  (): REDIS.INCR ( ' hits ') return Span class= "hljs-string" > ' Hello world! I have been seen%s times. '% Redis.get ( ' hits ') if __name__ =  "__main__": App.run (Host= "0.0.0.0", debug= true)             

Create a dependent file requirements.txt with the following content:

flaskredis
2. Create Dockerfile

Under the same directory, create the Dockerfile .

FROM python:3.5ADD . /codeWORKDIR /codeRUN python3 -m pip install -r requirements.txtCMD python3 app.py

Make a brief description of the above dockerfile:

    • The container uses a Python 3.5 mirror
    • Copy the files in the current directory into the container/code
    • Specify the working directory as/code
    • Libraries required for installing Python: flask, Redis
    • Container execution Command Python3 app.py
3. Create an Orchestration Script

Under the same directory, createdocker-compose.yml

version: ‘1‘services:  web:    build: . ports: - "5000:5000" volumes: - .:/code depends_on: - redis redis: image: redis

Make a brief description of the above orchestration script:

    • This app defines two services: Web, Redis
    • The Web container is generated from the Dockerfile under the current path
    • 5000 ports within the Web container are mapped to 5000 ports on the host
    • Mount the current directory inside the Web container/code
    • The Web container relies on the Redis container
    • Redis container gets mirroring from Docker hub

All files are ready.

$ lsapp.py  docker-compose.yml  Dockerfile  requirements.txt
4. Start the application

docker-composeExecute the Orchestration script, create and crawl the Web,redis image, and start the container separately.

$ sudo docker-compose up……redis_1  | 1:M 02 Feb 04:13:15.129 * Ready to accept connectionsweb_1    |  * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)web_1    |  * Restarting with statweb_1    |  * Debugger is active!web_1    | * Debugger PIN: 170-376-971

The whole process will be long ...

5. Access to Applications

Open Browser Orientation: http://0.0.0.0:5000/

The effect is as follows:

(10) Docker compose installation and creation of flask Web Apps

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.