赞
踩
本文章仅个人学习笔记,内容来源b站up主:
遇见狂神说
第3P Compose 安装
https://www.bilibili.com/video/BV1kv411q7Qc?p=3
看官方文档
1、下载
curl -L https://get.daocloud.io/docker/compose/releases/download/1.25.0/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
2、授权
sudo chmod +x docker-compose
3、查看版本
docker-compose version
第4P Compose 初体验
https://www.bilibili.com/video/BV1kv411q7Qc?p=3
官方文档:https://docs.docker.com/compose/gettingstarted/
1、创建文件夹 composetest
$ mkdir composetest
$ cd composetest
2、创建 app.py
$ vim 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
$ vim requirements.txt
flask
redis
4、创建 Dockerfile
$ vim Dockerfile
FROM python:3.7-alpine
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories
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"]
5、创建 docker-compose.yml
$ vim docker-compose.yml
version: "3.8"
services:
web:
build: .
ports:
- "5000:5000"
redis:
image: "redis:alpine"
6、启动
$ docker-compose build
$ docker-compose up
7、停止
8、小结
第5P Compose 初体验
https://www.bilibili.com/video/BV1kv411q7Qc?p=3
docker-compose.yaml 核心!
官方文档:https://docs.docker.com/compose/compose-file/
第6P 使用Compose一键部署WP博客
https://www.bilibili.com/video/BV1kv411q7Qc?p=3
官方文档:https://docs.docker.com/compose/wordpress/
1、创建文件
$ mkdir my_wordpress
$ cd my_wordpress
$ vim docker-compose.yml
version: '3.3'
services:
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: somewordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
wordpress:
depends_on:
- db
image: wordpress:latest
ports:
- "8000:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
WORDPRESS_DB_NAME: wordpress
volumes:
db_data: {}
2、启动
# 前台启动
$ docker-compose up
# 后台启动
$ docker-compose up -d
第7P 实战:自己编写微服务上线
https://www.bilibili.com/video/BV1kv411q7Qc?p=3
1、编写微服务项目(带redis)
HelloController.java
@RestController
public class HelloController {
@Autowired
StringRedisTemplate redisTemplate;
@GetMapping("/hello")
public String hello(){
Long views = redisTemplate.opsForValue().increment("views");
return "hello:"+views;
}
}
2、dockerfile构建镜像
FROM java:8
COPY *.jar /app.jar
CMD ["--server.port=8080"]
EXPOSE 8080
ENTRYPOINT ["java","-jar","/app.jar"]
3、docker-compose.yaml 编排项目
version: '3.3'
services:
wengapp:
build: .
image: wengiapp
depends_on:
- redis
ports:
- "8080:8080"
redis:
image: "library/redis:alpine"
4、丢到服务器 docker-compose up
总结:
第8P Compose 初体验
https://www.bilibili.com/video/BV1kv411q7Qc?p=3
第9P Compose 初体验
https://www.bilibili.com/video/BV1kv411q7Qc?p=3
第10P Compose 初体验
https://www.bilibili.com/video/BV1kv411q7Qc?p=3
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。