Flask Vue.js全栈开发|第21章:Docker容器部署

  • 原创
  • Madman
  • /
  • /
  • 9
  • 18753 次阅读

flask vuejs 全栈开发-min.png

Synopsis: 第一部分我们手动启动 MySQL、Redis、Elasticsearch、Python、Nginx 等容器,第二部分使用 Docker Compose 来编排我们的服务,非常推荐大家试一下 Docker 容器来运行我们的微型博客应用,到此为止,本系列就全部完结了,感谢大家的关注,接下来将开始 Golang 微服务实战系列,期待您的加入

请确保已正确安装并启动了 Docker 服务,关于 Docker 基本使用方法,请参考: http://www.madmalls.com/blog/post/visualizing-docker-containers-and-images/

假设你已将本项目 最新完整代码 下载到 /home/www 目录下:

[root@CentOS www]# pwd
/home/www
[root@CentOS www]# ls -l
total 12
drwxr-xr-x 5 root root  182 Apr 23 09:44 back-end
drwxr-xr-x 6 root root  214 Apr 23 09:44 front-end
-rw-r--r-- 1 root root 1104 Apr 23 09:44 LICENSE
-rw-r--r-- 1 root root 5389 Apr 23 09:44 README.md

1. MySQL 容器

https://hub.docker.com/_/mysql

创建 MySQL 配置文件,指定字符集为 UTF-8

[root@CentOS www]# mkdir -p $PWD/docker/mysql/conf.d
[root@CentOS www]# vim $PWD/docker/mysql/conf.d/custom.cnf
内容如下:

[mysqld]
character-set-server = utf8
collation-server = utf8_general_ci
skip-character-set-client-handshake

注意: utf8_general_ci 数据表中的字段值不区分大小写(ci, case insensitive),比如 Madmanmadman 被认为是一样的。如果你想区分大小写,则要设置为 utf8_bin(不能设置为 utf8_general_cs,容器会无法启动)

然后将宿主机上的 配置文件数据目录 链接到 MySQL 容器,这样就算后续不小心删除了此容器,数据库中的数据文件还存在于本地,下次再重新启一个 MySQL 容器即可:

[root@CentOS www]# docker run -d \
  --name mysql \
  -e MYSQL_ROOT_PASSWORD=123456 \
  -e MYSQL_DATABASE=madblog \
  -e MYSQL_USER=testuser \
  -e MYSQL_PASSWORD=password \
  -p 3306:3306 \
  --rm \
  -v $PWD/docker/mysql/conf.d:/etc/mysql/conf.d \
  -v $PWD/docker/mysql/data:/var/lib/mysql \
  mysql:5.6

让我们测试一下连接,再启动一个 MySQL 容器,需要先 链接 上面的 MySQL Server 容器,并设置主机名为 mysql-server,这样 -hmysql-server 才能解析到 MySQL Server 容器所绑定的 IP 地址:

[root@CentOS www]# docker run -it --link mysql:mysql-server --rm mysql:5.6 mysql -hmysql-server -utestuser -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.25 MySQL Community Server (GPL)

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| madblog            |
+--------------------+
2 rows in set (0.00 sec)

mysql> quit
Bye

2. Redis 容器

https://hub.docker.com/_/redis

[root@CentOS www]# docker run -d \
  --name redis \
  -p 6379:6379 \
  --rm \
  -v $PWD/docker/redis/data:/data \
  redis redis-server --appendonly yes

启动此容器时要运行 redis-server --appendonly yes 命令,表示启用了 Redis 数据持久性,所以指定了映射宿主机上的 $PWD/docker/redis/data 目录到容器的 /data 目录

测试连接:

[root@CentOS www]# docker run -it --link redis:redis-server --rm redis redis-cli -h redis-server
redis-server:6379> keys *
(empty list or set)
redis-server:6379> quit

3. Elasticsearch 容器

https://www.elastic.co/guide/en/elasticsearch/reference/current/docker.html

由于我们的代码中使用了 ik 分词器,所以需要在 Elasticsearch 官方提供的镜像中先安装 ik,再保存为修改后的镜像:

# 1. 启动容器
[root@CentOS www]# docker pull docker.elastic.co/elasticsearch/elasticsearch:7.0.0
[root@CentOS www]# docker run -d \
  --name elasticsearch \
  -p 9200:9200 \
  -p 9300:9300 \
  -e "discovery.type=single-node" \
  -e ES_JAVA_OPTS="-Xms256m -Xmx256m" \
  docker.elastic.co/elasticsearch/elasticsearch:7.0.0

# 2. 安装 IK 分词器
[root@CentOS www]# docker exec -it elasticsearch /bin/bash
[root@da774b5b5d99 elasticsearch]# elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.0.0/elasticsearch-analysis-ik-7.0.0.zip
.0.0.zip-> Downloading https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.0.0/elasticsearch-analysis-ik-7.0.0.zip
[=================================================] 100%?? 
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@     WARNING: plugin requires additional permissions     @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
* java.net.SocketPermission * connect,resolve
See http://docs.oracle.com/javase/8/docs/technotes/guides/security/permissions.html
for descriptions of what these permissions allow and the associated risks.

Continue with installation? [y/N]y
-> Installed analysis-ik
[root@da774b5b5d99 elasticsearch]# exit
exit

# 3. 保存为新的镜像
[root@CentOS www]# docker commit elasticsearch my-elasticsearch-ik
sha256:65cca25b877d7c904c7d3b8aa1e36edd1c20e79f614e052a0918eed163f047ad

# 4. 删除容器
[root@CentOS www]# docker rm -f elasticsearch 
elasticsearch

启动我们自己的镜像:

  • 126039858
  • zhuyulin
  • ygren
  • wulvtutu
  • 99857443
  • kasbar
  • nickzxhfjsm
  • que-ai2023
  • team12
  • reinxd
  • 109202918
  • 11757580
  • qiu-chen-100
  • 61450478
  • 19979360
  • 93176860
  • 96768625
  • luohuai1q84
  • binrr
  • zscsd
  • mingyun
  • ddcddc
  • 415670177
  • 49409355
未经允许不得转载: LIFE & SHARE - 王颜公子 » Flask Vue.js全栈开发|第21章:Docker容器部署

分享

作者

作者头像

Madman

如需 Linux / Python 相关问题付费解答,请按如下方式联系我

9 条评论

FutureSenzhong
FutureSenzhong
madblog-api_1    | INFO  [alembic.runtime.migration] Context impl MySQLImpl.
madblog-api_1    | INFO  [alembic.runtime.migration] Will assume non-transactional DDL.
madblog-api_1    | Failed to apply the migration to the database, retrying in 3 secs...
madblog-api_1    | ./boot.sh: 5: ./boot.sh: [[: not found
madblog-api_1    | [2020-02-09 13:14:04,671] INFO in __init__: Flask API Startup
madblog-api_1    | INFO  [alembic.runtime.migration] Context impl MySQLImpl.
madblog-api_1    | INFO  [alembic.runtime.migration] Will assume non-transactional DDL.
madblog-api_1    | ./boot.sh: 5: ./boot.sh: [[: not found
madblog-api_1    | Failed to apply the migration to the database, retrying in 3 secs...
FutureSenzhong
FutureSenzhong FutureSenzhong

容器部署一直报错找不到原因求解答

Madman
Madman FutureSenzhong Author

检查你的后端代码,手动执行 flask db upgrade 应该是失败的,自己对比附件的最新代码

FutureSenzhong
FutureSenzhong Madman

用的就是的代码

FutureSenzhong
FutureSenzhong FutureSenzhong

你的最新的代码

Madman
Madman FutureSenzhong Author
  1. 上面说了让你执行命令,你试了吗?
  2. 文章我都是测试过的,另外这么多其他读者都没问题
  3. 要想及时解决这种代码问题,不加我微信在这评论效率很低
lpchg1992
lpchg1992
此评论包含不良信息,已被禁止显示.
Oday1024
Oday1024

友情提醒: 第4.2章节 Dockerfile pip安装修改为:

FROM python:3.6-alpine

COPY ./back-end /usr/src/app
WORKDIR /usr/src/app
RUN echo "http://mirrors.aliyun.com/alpine/v3.11/main/" > /etc/apk/repositories
RUN echo "http://mirrors.aliyun.com/alpine/v3.11/community/" >> /etc/apk/repositories
RUN apk add --no-cache gcc mailcap python3-dev build-base linux-headers pcre-dev postgresql-dev libffi-dev libressl-dev
RUN pip install -r requirements.txt -i https://pypi.mirrors.ustc.edu.cn/simple  && pip install pymysql gunicorn -i https://pypi.mirrors.ustc.edu.cn/simple
ENV FLASK_APP madblog.py
EXPOSE 5000
ENTRYPOINT ["./boot.sh"]

否则pip install cryptography==2.7时会报错

bhb603
bhb603

请问能提供一下这份Docker教程的源码吗?

专题系列


Logo - LIFE & SHARE - 王颜公子

Madman 2021. © All Rights Reserved.

粤ICP备18040049号

  • About
  • RSS
  • Sitemap

联系我