首页 > 其他分享 >Docker Compose Test

Docker Compose Test

时间:2022-09-28 15:35:38浏览次数:30  
标签:Compose 06 Sep redis 28 2022 32 Test Docker

Docker Compose Test

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.

Step1. Setup

1. Define the application dependencies.

# mkdir composetest
# cd composetest

2. 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 network. We use the default port for Redis, 6379.

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

flask
redis

Step2. Create a Dockerfile

# syntax=docker/dockerfile:1
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 musl-dev linux-headers
COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt
EXPOSE 5000
COPY . .
CMD ["flask", "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 and other dependencies
  • Copy requirements.txt and install the Python dependencies.
  • Add metadata to the image to describe that the container is listening on port 5000
  • Copy the current directory . in the project to the workdir . in the image.
  • Set the default command for the container to flask run.

Step 3: Define services in a Compose file

Create a file called docker-compose.yml

version: "3.9"
services:
  web:
    build: .
    ports:
      - "8000:5000"
  redis:
    image: "redis:alpine"

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, 8000. 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

Start up the application by running docker compose up

[root@test composetest]# docker-compose up
Starting composetest_web_1   ... done
Starting composetest_redis_1 ... done
Attaching to composetest_web_1, composetest_redis_1
redis_1  | 1:C 28 Sep 2022 06:32:07.293 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
redis_1  | 1:C 28 Sep 2022 06:32:07.294 # Redis version=7.0.5, bits=64, commit=00000000, modified=0, pid=1, just started
redis_1  | 1:C 28 Sep 2022 06:32:07.294 # Warning: no config file specified, using the default config. In order to specify a conf                        ig file use redis-server /path/to/redis.conf
redis_1  | 1:M 28 Sep 2022 06:32:07.294 * monotonic clock: POSIX clock_gettime
redis_1  | 1:M 28 Sep 2022 06:32:07.295 * Running mode=standalone, port=6379.
redis_1  | 1:M 28 Sep 2022 06:32:07.295 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/s                        omaxconn is set to the lower value of 128.
redis_1  | 1:M 28 Sep 2022 06:32:07.295 # Server initialized
redis_1  | 1:M 28 Sep 2022 06:32:07.295 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condit                        ion. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommi                        t_memory=1' for this to take effect.
redis_1  | 1:M 28 Sep 2022 06:32:07.295 * Loading RDB produced by version 7.0.5
redis_1  | 1:M 28 Sep 2022 06:32:07.295 * RDB age 60 seconds
redis_1  | 1:M 28 Sep 2022 06:32:07.295 * RDB memory usage when created 0.82 Mb
redis_1  | 1:M 28 Sep 2022 06:32:07.295 * Done loading RDB, keys loaded: 0, keys expired: 0.
redis_1  | 1:M 28 Sep 2022 06:32:07.295 * DB loaded from disk: 0.000 seconds
redis_1  | 1:M 28 Sep 2022 06:32:07.295 * Ready to accept connections
web_1    |  * Serving Flask app 'app.py'
web_1    |  * Debug mode: off
web_1    | WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
web_1    |  * Running on all addresses (0.0.0.0)
web_1    |  * Running on http://127.0.0.1:5000
web_1    |  * Running on http://172.19.0.2:5000
web_1    | Press CTRL+C to quit
web_1    | 172.19.0.1 - - [28/Sep/2022 06:33:07] "GET / HTTP/1.1" 200 -

标签:Compose,06,Sep,redis,28,2022,32,Test,Docker
From: https://www.cnblogs.com/comecc/p/16738249.html

相关文章

  • ansible 二进制安装docker
     首先,上传文件docker-20.10.9.tgz到/data/docker/下 1、编辑docker.service文件docker的配置文件vim/data/docker/docker.service【[Unit]Description=DockerA......
  • prometheus export 各种服务 docker-compose文件
    redis-exporter配置docker-composeversion:'3'services:redis-exporter:image:oliver006/redis_exporterrestart:alwayscommand:-'--redis......
  • k8s集群上 docker 镜像编译
    因工作需要,在k8s集群上程序构建镜像。调研了dockerindocker方案。见链接。https://applatix.com/case-docker-docker-kubernetes-part-2/怎么都感觉不够智能,而且有点......
  • docker配置阿里云加速器(修改daemon.json后缀为conf)
    问题:docker无法拉取镜像,根据网上教程添加 /etc/docker/daemon.json后仍然失败。解决方法:将daemon.json文件名改为daemon.conf 后成功解决问题。网上常见配置方......
  • Docker常用命令及参数
    1、https://baijiahao.baidu.com/s?id=1692361731135557712&wfr=spider&for=pcDocker是一个被广泛使用的开源容器引擎,是一种操作系统级别的虚拟化技术,它以一种特殊进程......
  • docker 安装 redis
    1、启动镜像dockerrun--restart=always--log-optmax-size=100m--log-optmax-file=2-p6379:6379--namemyredis-v/opt/myredis/redis.conf:/etc/redis/redis.co......
  • 什么是docker swarm configs?及其在service中的使用?
    今天,来说一个在service中非常高级的知识点,configs. 然后,通过一些示例,来一步一步的演示,如何在service中使用,有什么关键的注意事项。什么是configs? configs的准确说......
  • docker 安装
    #!/bin/bash#删除已安装的Dockersudoyum-yremovedocker\docker-client\docker-client-latest\doc......
  • docker部署、运行tomcat问题
    1.dockertomcat拉取进入dockerHub选取自己需要的tomcat版本。docker拉取命令dockerpulltomcat:tagtag为选择的版本号2.tomcat启动dockerrun-d--nametomcat01......
  • docker出现Error response from daemon: driver failed programming external connect
    1.出现了如下问题点击查看代码docker:Errorresponsefromdaemon:driverfailedprogrammingexternalconnectivityonendpointtomcat01(00028237b8dd7b21dbce7......