Docker进阶篇

接上篇,继续学习docker进阶一些的内容。

Docker Compose

官方文档:https://docs.docker.com/compose

安装

安装

sudo curl -L https://get.daocloud.io/docker/compose/releases/download/1.25.1/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose

授权

sudo chmod +x /usr/local/bin/docker-compose

测试

官方文档:Get started with Docker Compose | Docker Documentation

Step 1

1、创建文件夹

mkdir composetest
cd composetest

2、创建app.py

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)

3、创建依赖配置文件requirements.txt

flask
redis

Step 2

1、创建dockerfile

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"]

Step 3

编写docker-compose.yml

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

Step 4

创建并运行docker-compose

docker compose up

compose启动后,会将每个服务生成对应的镜像,并且所有服务都在一个网络中

Docker进阶篇_第1张图片

停止

docker-compose down
ctrl+c 

yaml规则

https://www.runoob.com/w3cnote/yaml-intro.html

三层结构

version:
services:
	服务1:
		#服务配置
	服务2:
		#服务配置	
		
# 其他配置 网络、券、全局规则
volumes:
network:
configs:

version是向下兼容的

Docker进阶篇_第2张图片

开源项目部署博客

创建项目文件

mkdir my_wordpress
cd my_wordpress

编写docker-compose.yml

version: '3'
services:
  mysql:
    image: mysql:latest
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: my_password
      MYSQL_DATABASE: wordpress
      MYSQL_USER: wordpress_user
      MYSQL_PASSWORD: wordpress_password
    volumes:
      - mysql_data:/var/lib/mysql
  wordpress:
    image: wordpress:latest
    depends_on:
      - mysql
    ports:
      - 8080:80
    restart: always
    environment:
      WORDPRESS_DB_HOST: mysql:3306
      WORDPRESS_DB_USER: wordpress_user
      WORDPRESS_DB_PASSWORD: wordpress_password

volumes:
  mysql_data:

启动使用即可

docker-compose up

微服务上线

建个springboot项目,用redis记录请求次数。将文件打成jar包

package com.example.demo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {
    @Autowired
    StringRedisTemplate redisTemplate;

    @GetMapping("/hello")
    public String hell(){
        Long views = redisTemplate.opsForValue().increment("views");
        return "hello,Sentiment,views:"+views;
    }
}

dockerfile

FROM openjdk:8

COPY *.jar /app.jar

CMD ["--server.port=8080"]

EXPOSE 8080

ENTRYPOINT ["java","-jar","/app.jar"]

docker-compose.yml

version: '3.3'
services:
  sentimentdemo:
    build: .
    image: demo
    depends_on:
      - redis
    ports:
      - "4000:8080"
  redis:
    image: "library/redis:alpine"

docker启动项目

docker-compose up

此时请求主页便会记录次数

在这里插入图片描述

后记

后边还有Swarm等集群内容,但由于内容需求和服务器问题docker就了解到这里。。。

你可能感兴趣的:(Docker,docker,flask,python)