Mac 版Docker 客户端下载:
https://docs.docker.com/docker-for-mac/install/
安装之后打开preference,增加镜像源,这样下载需要的镜像时速度会很快,这里装的是清华镜像源。
清华源链接:
https://mirrors.tuna.tsinghua.edu.cn
在Docker Engine中添加如下内容:
{
"registry-mirrors": [
"https://mirrors.tuna.tsinghua.edu.cn"
],
"debug": true,
"experimental": true
}
进入项目docker,启动docker。
可以在终端查看docker包含的容器,有各种数据库等。
在我们自己的项目中,运行时需要先启动基础数据服务 mysql、redis、rabbitmq 等容器,然后启动项目代码容器,最后启动nginx容器做静态资源的代理。
使用docker ps -a 命令:
morika@care docker % docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
19b05ad84638 mysql:5.6 "docker-entrypoint.s…" 31 seconds ago Up 29 seconds 0.0.0.0:3308->3306/tcp pf_mysql_1
3e05edb89860 mysql "docker-entrypoint.s…" 33 minutes ago Exited (1) 33 minutes ago mystifying_newton
3d722e4dd6fd mysql "docker-entrypoint.s…" About an hour ago Up About an hour 33060/tcp, 0.0.0.0:3307->3306/tcp bnc-mysql
b31675a41338 mysql "docker-entrypoint.s…" About an hour ago Exited (1) About an hour ago hungry_jennings
aa862bf1047a nginx:latest "/docker-entrypoint.…" 2 hours ago Up 2 hours 0.0.0.0:80->80/tcp web
7b3c60749318 hello-world "/hello" 2 hours ago Exited (0) 2 hours ago happy_sammet
cad402b13e39 hello-world "/hello" 2 hours ago Exited (0) 2 hours ago sweet_keller
7becb19467df hello-world "/hello" 2 hours ago Exited (0) 2 hours ago stoic_ride
46c3ee4ba13a pf_app "python ./app.py --d…" 3 months ago Exited (1) 3 months ago pf_app_1
f5d4d3b53c84 mongo "docker-entrypoint.s…" 3 months ago Exited (255) 3 months ago 0.0.0.0:27018->27017/tcp f5d4d3b53c84_pf_mongo_1
a72bbebcbb96 redis:4.0-alpine "docker-entrypoint.s…" 3 months ago Exited (255) 3 months ago 0.0.0.0:6381->6379/tcp a72bbebcbb96_pf_redis_1
f1f0e3f011dc nginx:1.13-alpine "nginx -g 'daemon of…" 3 months ago Exited (255) 3 months ago 0.0.0.0:80->80/tcp pf_nginx_1
0aa94c626f98 pf_app "python ./app.py --d…" 4 months ago Exited (0) 4 months ago pf_app_run_a431cd592c38
93b4827601fe pf_app "python ./app.py --d…" 4 months ago Exited (1) 4 months ago pf_app_run_13c715a35836
79886b2b15a3 rabbitmq:3.6-alpine "docker-entrypoint.s…" 4 months ago Up 30 seconds 4369/tcp, 5671/tcp, 25672/tcp, 0.0.0.0:5674->5672/tcp pf_rabbitmq_1
e75aa861cda3 sg_app "python ./app.py --d…" 5 months ago Exited (1) 5 months ago sg_app_1
fe0a028234bd redis:4.0-alpine "docker-entrypoint.s…" 5 months ago Exited (255) 5 months ago 0.0.0.0:6380->6379/tcp sg_redis_1
3d6c51f390dd rabbitmq:3.6-alpine "docker-entrypoint.s…" 5 months ago Exited (255) 5 months ago 4369/tcp, 5671/tcp, 25672/tcp, 0.0.0.0:5673->5672/tcp sg_rabbitmq_1
eb3ef6d89d9d mysql:5.6 "docker-entrypoint.s…" 5 months ago Exited (255) 5 months ago 0.0.0.0:3307->3306/tcp sg_mysql_1
293ed94bce59 abaffb51bd40 "/bin/sh -c 'apk add…" 5 months ago Exited (1) 5 months ago jovial_bell
morika@care docker % docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
19b05ad84638 mysql:5.6 "docker-entrypoint.s…" 4 minutes ago Up 4 minutes 0.0.0.0:3308->3306/tcp pf_mysql_1
3d722e4dd6fd mysql "docker-entrypoint.s…" About an hour ago Up About an hour 33060/tcp, 0.0.0.0:3307->3306/tcp bnc-mysql
aa862bf1047a nginx:latest "/docker-entrypoint.…" 2 hours ago Up 2 hours 0.0.0.0:80->80/tcp web
79886b2b15a3 rabbitmq:3.6-alpine "docker-entrypoint.s…" 4 months ago Up 4 minutes 4369/tcp, 5671/tcp, 25672/tcp, 0.0.0.0:5674->5672/tcp pf_rabbitmq_1
启动docker:
morika@care docker % ls
app-only.yml conf docker-compose.yml
morika@care docker % docker-compose up app
如果某个数据库未启动:如mysql,选择对应的names登录:
morika@care docker % docker exec -it pf_mysql_1 bash
root@19b05ad84638:/# mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 6
Server version: 5.6.46 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;
mongo 数据库的启动与登录:
morika@care Desktop % docker exec -it pf_mongo_1 bash
root@9325f4da89f1:/# mongo -u senguo_test -p
MongoDB shell version v4.0.18
Enter password:
connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("c412562d-19c6-481b-8fdb-448442dcbbee") }
MongoDB server version: 4.0.18
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
http://docs.mongodb.org/
Questions? Try the support group
http://groups.google.com/group/mongodb-user
Server has startup warnings:
2020-06-16T16:21:33.450+0800 I STORAGE [initandlisten]
2020-06-16T16:21:33.450+0800 I STORAGE [initandlisten] ** WARNING: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine
2020-06-16T16:21:33.450+0800 I STORAGE [initandlisten] ** See http://dochub.mongodb.org/core/prodnotes-filesystem
---
Enable MongoDB's free cloud-based monitoring service, which will then receive and display
metrics about your deployment (disk utilization, CPU, operation statistics, etc).
The monitoring data will be available on a MongoDB website with a unique URL accessible to you
and anyone you share the URL with. MongoDB may use this information to make product
improvements and to suggest MongoDB products and deployment options to you.
To enable free monitoring, run the following command: db.enableFreeMonitoring()
To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
---
> use senguopf
switched to db senguopf
> db.createUser({user: "local", pwd: "loval", roles: [{role: "readWrite", db: "senguopf"}]})
Successfully added user: {
"user" : "local",
"roles" : [
{
"role" : "readWrite",
"db" : "senguopf"
}
]
}
> db.auth("local", "local")
Error: Authentication failed.
0
> db.auth("local", "loval")
1
>
使用docker compose启动:
docker-compose up
注意事项:
1、进入项目根目录下的 docker 目录并执行 docker-compose up app 即可自动构建整个项目的环境并以默认端口运行项目。可通过浏览器访问 127.0.0.1:[项目默认端口号] 测试运行情况。
2、可以执行 docker-compose up -d nginx 后台启动 nginx 容器,此时该项目可通过 80 端口访问
3、可以执行 docker-compose exec [容器名] [命令]对正在运行的容器执行命令,例如:docker-compose exec app /bin/sh(进入app这个容器后执行/bin/sh开启一个可交互终端)
4、docker的目录结构:
Docker
├── .env // 默认环境配置文件,用于对 docker-compose 的运行环境做定义
├── docker-compose.yml // 默认容器编排配置文件,包含项目运行时需要的数据服务等所有依赖
├── app-only.yml // 仅运行app容器的编排配置文件,此时数据服务需自行配置
├── conf
│ ├── app
│ │ ├── Dockerfile // 构建项目镜像的 Dockerfile 文件
│ │ └── requirements.txt // 构建项目镜像时需要安装的 python 包列表
│ └── nginx
│ └── nginx.conf // docker 容器中的 nginx 使用的配置
├── data // docker 容器中的数据库数据目录(该目录不同步到 git)
│ ├── mysql
│ └── redis
└── logs // docker 容器中应用日志目录(该目录不同步到 git)
└── nginx
5、docker compose 配置文件
version: '2' # 配置文件兼容版本
services: # 定义该容器编排中不同容器的配置
app: # 容器名称,下面是app容器的详细配置
build: ./conf/app # app容器使用./conf/app目录下的Dockerfile来构建自定义镜像
stdin_open: true
tty: true
ports: # 端口映射
- "8887:8887" # "宿主机端口:容器内端口",将容器内的8887端口映射到宿主机8887端口,这样本机就可以通过127.0.0.1:8887访问到容器内的8887端口上运行的应用
volumes: # 文件/目录映射
- ../:/usr/src/pyDemo:rw # - 宿主机目录:容器内目录:读写权限,将容器内的/usr/src/pyDemo目录映射到宿主机的../目录(当前配置文件所在目录的前一级目录)
working_dir: /usr/src/pyDemo/pyDemo/admin # 环境目录,所有命令都在/usr/src/pyDemo/pyDemo/admin这个目录下执行
networks: # 加入的网络,加入同名网络的不同容器间可以通过容器名称来相互访问
- front-tier
- back-tier # app容器和下面的mysql容器,都加入了back-tier这个网络,所以app中可以使用mysql:3306来连接数据库
depends_on: # 容器依赖,app容器依赖mysql、redis、rabbitmq三个容器,所以在启动app的时候,会先启动其依赖的容器,以保证app容器可以正常访问其依赖的容器中的服务
- mysql
- redis
- rabbitmq
command: python ./app.py --debug=1 # 容器启动时默认执行的命令,这里是以debug模式启动项目,注意这里默认在上面配置的working_dir目录下运行
nginx: # 容器名称,下面是nginx容器的详细配置
image: nginx:1.13-alpine # nginx容器使用nginx:1.13-alpine镜像,本地不存在时会自动从镜像源拉取
ports:
- "80:80"
volumes:
- ./conf/nginx/nginx.conf:/etc/nginx/nginx.conf:ro # - 将容器内的/etc/nginx/nginx.conf映射到宿主机的./conf/nginx/nginx.conf上,实现nginx容器使用本机存储的配置文件
- ./logs/nginx/:/var/log/nginx:rw
- ../:/usr/src/pyDemo:rw
networks:
- front-tier
depends_on:
- app
mysql:
image: mysql:5.6
ports:
- "3307:3306" # "宿主机端口:容器内端口",将容器内的3306端口映射到宿主机3307端口,这样本机可以直接通过3307端口连接容器内的mysql数据库
volumes:
- ./data/mysql:/var/lib/mysql:rw # 容器内/var/lib/mysql是mysql数据库的实际数据存储目录,映射到本机目录,这样mysql数据不会随容器的停止而消失,下面的redis容器同理
environment: # 容器运行环境变量设置
MYSQL_ROOT_PASSWORD: "test123" # mysql数据库root密码设置为test123
MYSQL_USER: "local" # mysql数据库增加local用户
MYSQL_PASSWORD: "py_mysql" # mysql数据库local用户密码设置为py_mysql
networks:
- back-tier
redis:
image: redis:4.0-alpine
ports:
- "6380:6379"
volumes:
- ./data/redis:/data:rw
networks:
- back-tier
rabbitmq:
image: rabbitmq:3.6-alpine
ports:
- "5673:5672"
networks:
- back-tier
networks: # 定义该容器编排中的网络配置
front-tier:
back-tier:
6、docker常用命令
docker run [选项] [镜像名] [命令] [命令参数...] # 创建一个新的容器并运行一个命令
选项:
-a stdin: # 指定标准输入输出内容类型,可选 STDIN/STDOUT/STDERR 三项
-d: # 后台运行容器,并返回容器ID
-i: # 以交互模式运行容器,通常与 -t 同时使用
-t: # 为容器重新分配一个伪输入终端,通常与 -i 同时使用
--name="my-nginx": # 为容器指定一个名称
-h "hostname": # 指定容器的hostname
-e username="ritchie": # 设置环境变量
--link=[]: # 添加链接到另一个容器
--expose=[]: # 开放一个端口或一组端口
-p hostport:containerport: # 指定容器暴露的端口对应宿主机的端口
-P: # 暴露容器端口对应宿主机的随机端口
-v: # 给容器挂载存储卷,挂载到容器的某个目录
docker start/stop/restart [容器名/容器ID] # 运行/停止/重启某个容器
docker exec [容器名/容器ID] [命令] [命令参数...] # 在一个运行的容器中执行命令
docker logs [容器名/容器ID] # 查看容器的标准输出
docker images # 查看所有本地镜像
dodker ps # 查看所有运行中的容器
docker ps -a # 查看所有容器
docker rm [容器名/容器ID] # 删除某个容器
docker rmi [镜像名/镜像ID] # 删除某个本地镜像
docker build [选项] [路径] # 根据Dockerfile构建镜像
docker port [容器名/容器ID] # 列出容器的端口映射
7、docker compose常用命令
docker-compose up # 使用当前目录下的默认编排配置文件docker-compose.yml,运行其中定义的所有容器并将所有容器的输出集中展示到当前shell
docker-compose up app # 使用当前目录下的默认编排配置文件docker-compose.yml,但仅运行其中定义的app这个容器,其依赖容器会自动运行,但只有app容器的输出会展示到当前shell
docker-compose up -d mysql redis rabbitmq # 使用当前目录下的默认编排配置文件docker-compose.yml,后台运行其中定义的mysql redis rabbitmq三个容器
docker-compose up -d nginx # 使用当前目录下的默认编排配置文件docker-compose.yml,后台运行其中定的nginx容器
docker-compose exec app /bin/sh # 对正在运行的app容器执行命令/bin/sh,执行后会得到app这个容器的交互shell
docker-compose -f app-only.yml up # 使用指定的编排配置文件app-only.yml来运行容器,该文件一般只运行app容器,其数据依赖等需要自行配置
# 开启一个新的容器并执行一个命令,这里开启了一个app容器来运行celery队列
docker-compose run \
--rm \
-w /usr/src/pyDemo/pyDemo/admin/handlers \
-e C_FORCE_ROOT=True \
app \
celery -A celery_barcode_query worker -Q barcode_query --loglevel=info
docker-compose ps # 查看当前编排中的容器状态
docker-compose down # 停止当前编排中的所有容器