Docker Step-by-Step: The Ultimate Guide From Beginner to Expert. Learn & Master The Platform and Containerize, Create, Deploy and Run Your Application Like a Professional by Shaw Brandon

Docker Step-by-Step: The Ultimate Guide From Beginner to Expert. Learn & Master The Platform and Containerize, Create, Deploy and Run Your Application Like a Professional by Shaw Brandon

Author:Shaw, Brandon [Shaw, Brandon]
Language: eng
Format: epub
Published: 2019-12-04T16:00:00+00:00


How to deploy with docker-compose

On this page, you build a simple Python web application running on Docker Compose. The application uses the Flask framework and maintains a hit counter in Redis. While the sample uses Python, the concepts demonstrated here should be understandable even if you’re not familiar with it.

Prerequisites

Make sure you have already installed both Docker Engine and Docker Compose. You don’t need to install Python or Redis, as both are provided by Docker images.

Step 1: Setup

Define the application dependencies.

Create a directory for the project:

$ mir compose test

$ cd compose test

Create a file called app.py in your project directory and paste this in:

import time

import Redis

from flask import Flask

app = Flask(__name__)

cache = redis.Redis(host='redis', port=6379)

def get_hit_count():

retries = 5

while True:

try:

return cache.incr('hits')

except redis.exceptions.ConnectionError as exc:

if retries == 0:

raise exc

retries -= 1

time.sleep(0.5)

@app.route('/')

def hello():

count = get_hit_count()

return 'Hello World! I have been seen {} times.\n'.format(count)

In this example, redis is the hostname of the redis container on the application’s nerk. We use the default port for Redis, 6379.

Handling transient errors

Note the way the get_hit_count function is written. This basic retry loop lets us attempt our request multiple times if the Redis service is not available. This is useful at startup while the application comes online but also makes our application more resilient if the Redis service needs to be restarted anytime during the app’s lifetime. In a cluster, this also helps to handle momentary connection drops between nodes.

Create another file called requirements.txt in your project directory and paste this in:

flask

Redis

Step 2: Create a Dockerfile

In this step, you write a Dockerfile that builds a Docker image. The image contains all the dependencies the Python application requires, including Python itself.

In your project directory, create a file named Dockerfile and paste the following:

FROM python:3.7-alpine

WORKDIR /code

ENV FLASK_APP app.py

ENV FLASK_RUN_HOST 0.0.0.0

RUN Apk. add --no-cache GCC -dev Linux-headers

COPY requirements.txt requirements.txt

RUN pip install -r requirements.txt

COPY.

CMD ["fl ask", "run"]

This tells Docker to:

Build an image starting with the Python 3.7 image.

Set the working directory to /code.

Set environment variables used by the flask command.

Install GCC so Python packages such as MarkupSafe and SQLAlchemy can compile speedups.

Copy requirements.txt and install the Python dependencies.

Copy the current directory . in the project to the work dir . in the image.

Set the default command for the container to flask run.

For more information on how to write Dockerfiles, see the Docker user guide and the Dockerfile reference.

Step 3: Define services in a Compose file

Create a file called docker-compose.yml in your project directory and paste the following:

version: '3'

services:

web:

build:

ports:

- "5000:5000"

Redis:

image: "alpine"

This Compose file defines services: web and Redis.

Web service

The web service uses an image that’s built from the Dockerfile in the current directory. It then binds the container and the host machine to the exposed port, 5000. This example service uses the default port for the Flask web server, 5000.

Redis service

The Redis service uses a public Redis image pulled from the Docker Hub registry.

Step 4: Build and run your app with Compose

From your project directory, start up your application by running docker-compose up.

$ docker-compose up

Creating "composetest_default" with the default driver

Creating composetest_web_1 ...

Creating composetest_redis_1 ...

Creating composetest_web_1

Creating composetest_redis_1 .



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.