Skip to content

Commit dc5ec37

Browse files
committed
完成了Docker笔记Version1(19352字符)
1 parent a070762 commit dc5ec37

File tree

1 file changed

+115
-3
lines changed

1 file changed

+115
-3
lines changed

Solutions/Other-Docker-Note.md

Lines changed: 115 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -532,18 +532,130 @@ docker compose [OPTIONS] [COMMAND]
532532
533533
|docker run 参数|docker compose 指令|说明|
534534
|:--:|:--:|:--:|
535-
|--name|container_name|容器名称|
535+
|––name|container_name|容器名称|
536536
|-p|ports|端口映射|
537537
|-e|environment|环境变量|
538538
|-v|volumes|数据卷配置|
539-
|--network|networks|网络|
539+
|––network|networks|网络|
540540
541541
实例:
542542
543-
```bash
543+
新建一个文件夹并在终端中进入,新建```server.py```文件并写入以下内容:
544+
545+
```python
546+
from flask import Flask
547+
import pymysql
548+
549+
app = Flask('demo')
550+
MYSQL_HOST = 'mysql'
551+
inited = False
552+
553+
554+
def initdb(): # 创建数据表
555+
conn = pymysql.connect(host=MYSQL_HOST, user='root', passwd='123')
556+
cursor = conn.cursor()
557+
cursor.execute('CREATE DATABASE IF NOT EXISTS demo;')
558+
cursor.execute('USE demo;')
559+
cursor.execute('CREATE TABLE IF NOT EXISTS times(num INT, times INT);')
560+
cursor.execute('SELECT * FROM times;')
561+
if not cursor.fetchone():
562+
cursor.execute('INSERT INTO times (num, times) VALUES (1, 1)')
563+
conn.commit()
564+
cursor.close()
565+
conn.close()
566+
567+
568+
@app.route('/')
569+
def count():
570+
global inited
571+
if not inited:
572+
try:
573+
initdb()
574+
inited = True
575+
except Exception as e:
576+
print(e)
577+
try:
578+
conn = pymysql.connect(host=MYSQL_HOST, user='root', database='demo', passwd='123')
579+
cursor = conn.cursor()
580+
cursor.execute('SELECT * FROM times;')
581+
now = cursor.fetchone()[1]
582+
cursor.execute(f'UPDATE times SET times = {now + 1} WHERE num = 1;')
583+
conn.commit()
584+
cursor.close()
585+
conn.close()
586+
return f'the {now}-th'
587+
except Exception as e:
588+
return f'{e}\n请耐心等待至Mysql初始化完成后重试'
589+
590+
591+
if __name__ == '__main__':
592+
app.run(host='0.0.0.0')
593+
```
594+
595+
新建```Dockerfile```文件并写入以下内容:
596+
597+
```docker
598+
FROM python
599+
LABEL maintainer="LetMeFly"
600+
WORKDIR /root
601+
COPY ./server.py /root/
602+
RUN pip3 install pymysql -i https://mirrors.aliyun.com/pypi/simple
603+
RUN pip3 install flask -i https://mirrors.aliyun.com/pypi/simple
604+
RUN pip3 install cryptography -i https://mirrors.aliyun.com/pypi/simple
605+
ENTRYPOINT ["python3", "server.py"]
606+
EXPOSE 5000
607+
```
608+
609+
新建```docker-compose.yml```文件并写入以下内容:
610+
611+
```yaml
612+
version: "1.0"
613+
614+
services:
615+
mysql:
616+
image: mysql
617+
container_name: mysql1
618+
ports:
619+
- "3306:3306"
620+
environment:
621+
TZ: Asia/Shanghai
622+
MYSQL_ROOT_PASSWORD: 123
623+
networks:
624+
- flask
625+
626+
python:
627+
build:
628+
context: .
629+
image: flask-img-counttime
630+
container_name: flask-server
631+
ports:
632+
- "80:5000"
633+
networks:
634+
- flask
635+
depends_on:
636+
- mysql
637+
638+
networks:
639+
flask:
640+
name: flask
641+
```
642+
643+
接着在终端中执行命令
544644
645+
```bash
646+
docker compose up -d
545647
```
546648
649+
等待容器构建完成,访问[localhost](http://localhost/)即可看到Flask中的信息。
650+
651+
Mysql容器启动后可能需要很久才能建立连接,因此刚开始访问[localhost](http://localhost/)时,看到的是“connection refused 请耐心等待mysql初始化完成”。
652+
653+
过了可能好几分钟,硬盘占用突然降低,Mysql初始化完成,容器```flask-img-counttime```能够访问到容器```mysql1```,再次访问[localhost](http://localhost/),可以看到“the 1-th”。刷新后变成了“the 2-th”,再刷新“the 3-th”,......。
654+
655+
这说明我们使用```docker compose```成功实现了关联容器的快速部署。(若想一键清除,可以在当前目录下```docker compose down```
656+
657+
另附:将docker commands转为docker-compose的[在线网站](https://www.composerize.com/)
658+
547659
## 其他设置
548660
549661
编辑```deamon.json```或者直接在Docker Desktop的settings中修改配置文件即可更改docker的一些设置。

0 commit comments

Comments
 (0)