作用
docker compose 是基于python开发的,用于Docker的服务编排工具,在构建基于Docker的复杂应用的时候通过Compose编写docker-compose.yml一个配置文件来管理多个Docker容器,或对容器集群的管理和编排,非常适合组合使用多个容器进行开发的场景。
Dockerfile 用来构建 Docker 镜像,那么 docker-compose 则是用来创建容器。 docker-compose一般是主要用于我们在构建 Docker Run 的一些了的命令封装执行等的问题。
作用示例说明
通常我们的一个完整的项目其实是需要整合几个服务的节点包括App服务,数据库服务,还有缓存服务等,我们再进行项目初期的开发的时候,我们为了开发环境的方便的测试,其实可以编排好我们的服务节点,把所有的服务都整合到一个容器内运行,这样也方便我们的开发环境的迁移和构建,不需要话费太多的时间再环境的搭建上。
当然线上生产环境,肯定是不推荐这么搞得,毕竟,本身Docker存在的目的就是为了服务独立和环境隔离!
测试环境搭建:
线上环境的话肯定都是独立的容器
安装
1下载安装:
[root@localhost ~]# curl -L https://get.daocloud.io/docker/compose/releases/download/1.24.0/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 423 100 423 0 0 221 0 0:00:01 0:00:01 --:--:-- 221
100 15.4M 100 15.4M 0 0 4427k 0 0:00:03 0:00:03 --:--:-- 10.0M
2:给权限
[root@localhost ~]# chmod +x /usr/local/bin/docker-compose
可以给软件接
创建软链:
[root@localhost ~]# ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
3:查看版本信息
[root@localhost ~]# docker-compose version
docker-compose version 1.24.0, build 0aa59064
docker-py version: 3.7.2
CPython version: 3.6.8
OpenSSL version: OpenSSL 1.1.0j 20 Nov 2018
[root@localhost ~]# docker-compose -v
docker-compose version 1.24.0, build 0aa59064
[root@localhost ~]#
实践1
扩展使用docker-compose.yml来部署运行Flaskweb
上一节:https://www.jianshu.com/p/5fa896ac3add
基于上节的Flaskweb部署的情况下我们的使用docker-compose.yml来进行更优雅的部署。
步骤
- 1)进入到我们的宿主节点的上传的目录环境下,创建和编写docker-compose.yaml文件(记得同步到我们的虚拟机里面)
查看已有的镜像
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
flask_web_stj latest 1410b6bc6d9d 21 hours ago 913MB
newmynginx latest cd903b9a327f 24 hours ago 146MB
python 3.6 1297140c6dd2 33 hours ago 874MB
tomcat latest 891fcd9c5b3a 3 weeks ago 647MB
nginx latest f35646e83998 3 weeks ago 133MB
redis 4.0.14 191c4017dcdd 6 months ago 89.3MB
hell02 latest bf756fb1ae65 10 months ago 13.3kB
centos centos7.1.1503 e1430271e2f9 20 months ago 212MB
ubuntu 15.10 9b9cb95443b5 4 years ago 137MB
[root@localhost ~]#
查看当前已存在的容器信息
[root@localhost ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9b8ca21c49e2 flask_web_stj "gunicorn main:app -…" 21 hours ago Exited (0) About an hour ago flask02
4cfef408a947 flask_web_stj "gunicorn main:app -…" 21 hours ago Exited (0) About an hour ago flask01
cb252d0bb06e newmynginx "/docker-entrypoint.…" 24 hours ago Exited (0) About an hour ago daorunginx
26c93b36d5e3 f35646e83998 "/docker-entrypoint.…" 24 hours ago Exited (0) 24 hours ago funny_shamir
8bd4c69fec01 191c4017dcdd "docker-entrypoint.s…" 25 hours ago Exited (0) About an hour ago myredis
a90e1eb657cb nginx "/docker-entrypoint.…" 28 hours ago Exited (0) 25 hours ago gifted_ride
27dec980370f e1430271e2f9 "/bin/bash" 2 days ago Exited (137) 25 hours ago vigorous_varahamihira
3b8b439a5155 e1430271e2f9 "/bin/bash" 2 days ago Exited (0) 2 days ago thirsty_easley
0590fcd8ef46 e1430271e2f9 "/bin/bash" 2 days ago Exited (0) 2 days ago amazing_liskov
60e127550b03 9b9cb95443b5 "/bin/bash" 2 days ago Exited (0) 2 days ago cool_fermi
ba9eda20f5eb 9b9cb95443b5 "/bin/bash" 2 days ago Exited (130) 2 days ago peaceful_tesla
c6aa103b9b79 9b9cb95443b5 "/bin/bash" 2 days ago Exited (127) 2 days ago eloquent_kowalevski
509bd82ac9b4 9b9cb95443b5 "/bin/bash" 2 days ago Exited (0) 2 days ago wonderful_merkle
6199fa3fd9d1 9b9cb95443b5 "/bin/bash" 2 days ago Exited (130) 2 days ago determined_austin
d85396b481fe 9b9cb95443b5 "/bin/bash" 2 days ago Exited (0) 2 days ago jolly_hypatia
7271d413dfee hell02 "/hello" 2 weeks ago Exited (0) 2 weeks ago heuristic_kowalevski
c9374334c848 hell02 "/hello" 2 weeks ago Exited (0) 2 weeks ago quizzical_euclid
f1b40b91bb48 ubuntu:15.10 "/bin/sh -c 'while t…" 2 weeks ago Exited (137) 2 weeks ago great_tereshkova
a2cc1c83603a ubuntu:15.10 "/bin/bash" 2 weeks ago Exited (0) 2 weeks ago elated_cartwright
0a22083fa318 ubuntu:15.10 "/bin/bash" 2 weeks ago Exited (127) 2 weeks ago epic_galileo
e0069b97f043 ubuntu:15.10 "/bin/echo 'Hello wo…" 2 weeks ago Exited (0) 2 weeks ago intelligent_lamport
209fc9dd3324 bf756fb1ae65 "/hello" 2 weeks ago Exited (0) 2 weeks ago flamboyant_knuth
d6087f654d9e bf756fb1ae65 "/hello" 2 weeks ago Exited (0) 2 weeks ago practical_wozniak
[root@localhost ~]#
先删除已存在的容器名称或下面使用新的
- 2)编写docker-compose.yaml内容(文件名称固定)
version: "3.3"
services:
flask_web_stj_01:
image: flask_web_stj:latest
build: .
container_name: flask_new01
restart: always
ports:
- "1245:80"
flask_web_stj_02:
image: flask_web_stj:latest
build: .
container_name: flask_new02
restart: always
ports:
- "1246:80"
配置文件说明
version:docker-compose的版本
services:需要管理的服务
flask_web_stj_01:FlaskApp服务应用的节点名称
image:flask_web_stj_01服务来源镜像
build:如果镜像不存在,当前位置构建镜像。存在则跳过
container_name:启动的容器名称
restart:容器启动属性, always一直重启
ports:端口映射
-
3)上传docker-compose.yaml同步更新到我的虚拟机的项目目录下
4)控制台执行命令docker-compose up -d启动容器
提示错误
[root@localhost web_statistics]# docker-compose up -d
ERROR: yaml.scanner.ScannerError: mapping values are not allowed here
in "./docker-compose.yaml", line 13, column 25
[root@localhost web_statistics]#
修改配置
version: "3.3"
services:
flask_web_stj_01:
image: flask_web_stj:latest
build: .
container_name: flask_new01
restart: always
ports:
- "1245:80"
在单纯的运行一个服务没问题!
[root@localhost web_statistics]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f4221a15a76f flask_web_stj:latest "gunicorn main:app -…" About a minute ago Up About a minute 0.0.0.0:1245->80/tcp flask_new01
[root@localhost web_statistics]#
停止和删除容器,方便下次的启动
[root@localhost web_statistics]# docker stop flask_new01
flask_new01
[root@localhost web_statistics]# docker rm flask_new01
flask_new01
[root@localhost web_statistics]#
核对格式问题
修正格式后:
再次的启动
非后台启动:
[root@localhost web_statistics]# docker-compose up
Starting flask_new01 ... done
Starting flask_new02 ... done
Attaching to flask_new02, flask_new01
flask_new02 | [2020-11-05 06:41:08 +0000] [1] [INFO] Starting gunicorn 20.0.4
flask_new02 | [2020-11-05 06:41:08 +0000] [1] [INFO] Listening at: http://0.0.0.0:80 (1)
flask_new02 | [2020-11-05 06:41:08 +0000] [1] [INFO] Using worker: gevent
flask_new02 | [2020-11-05 06:41:08 +0000] [8] [INFO] Booting worker with pid: 8
flask_new01 | [2020-11-05 06:41:08 +0000] [1] [INFO] Starting gunicorn 20.0.4
flask_new01 | [2020-11-05 06:41:08 +0000] [1] [INFO] Listening at: http://0.0.0.0:80 (1)
flask_new01 | [2020-11-05 06:41:08 +0000] [1] [INFO] Using worker: gevent
flask_new01 | [2020-11-05 06:41:08 +0000] [8] [INFO] Booting worker with pid: 8
flask_new02 | [2020-11-05 06:41:08 +0000] [9] [INFO] Booting worker with pid: 9
flask_new02 | [2020-11-05 06:41:08 +0000] [10] [INFO] Booting worker with pid: 10
flask_new01 | [2020-11-05 06:41:08 +0000] [9] [INFO] Booting worker with pid: 9
flask_new01 | [2020-11-05 06:41:08 +0000] [10] [INFO] Booting worker with pid: 10
flask_new02 | [2020-11-05 06:41:08 +0000] [11] [INFO] Booting worker with pid: 11
flask_new02 | [2020-11-05 06:41:08 +0000] [12] [INFO] Booting worker with pid: 12
flask_new01 | [2020-11-05 06:41:08 +0000] [11] [INFO] Booting worker with pid: 11
[root@localhost web_statistics]# docker-compose up -d
Creating flask_new01 ... done
Creating flask_new02 ... done
[root@localhost web_statistics]#
[root@localhost web_statistics]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e64670bf0546 flask_web_stj:latest "gunicorn main:app -…" 19 seconds ago Up 17 seconds 0.0.0.0:1245->80/tcp flask_new01
792a8edb7c20 flask_web_stj:latest "gunicorn main:app -…" 19 seconds ago Up 18 seconds 0.0.0.0:1246->80/tcp flask_new02
[root@localhost web_statistics]#
和多次执行
[root@localhost web_statistics]# docker-compose up -d
Starting flask_new01 ... done
Starting flask_new02 ... done
[root@localhost web_statistics]# docker-compose up -d
flask_new01 is up-to-date
flask_new02 is up-to-date
[root@localhost web_statistics]#
docker-compose 的命令
[root@localhost web_statistics]# docker-compose
Define and run multi-container applications with Docker.
Usage:
docker-compose [-f ...] [options] [COMMAND] [ARGS...]
docker-compose -h|--help
Options:
-f, --file FILE Specify an alternate compose file
(default: docker-compose.yml)
-p, --project-name NAME Specify an alternate project name
(default: directory name)
--verbose Show more output
--log-level LEVEL Set log level (DEBUG, INFO, WARNING, ERROR, CRITICAL)
--no-ansi Do not print ANSI control characters
-v, --version Print version and exit
-H, --host HOST Daemon socket to connect to
--tls Use TLS; implied by --tlsverify
--tlscacert CA_PATH Trust certs signed only by this CA
--tlscert CLIENT_CERT_PATH Path to TLS certificate file
--tlskey TLS_KEY_PATH Path to TLS key file
--tlsverify Use TLS and verify the remote
--skip-hostname-check Don't check the daemon's hostname against the
name specified in the client certificate
--project-directory PATH Specify an alternate working directory
(default: the path of the Compose file)
--compatibility If set, Compose will attempt to convert keys
in v3 files to their non-Swarm equivalent
Commands:
build Build or rebuild services
bundle Generate a Docker bundle from the Compose file
config Validate and view the Compose file
create Create services
down Stop and remove containers, networks, images, and volumes
events Receive real time events from containers
exec Execute a command in a running container
help Get help on a command
images List images
kill Kill containers
logs View output from containers
pause Pause services
port Print the public port for a port binding
ps List containers
pull Pull service images
push Push service images
restart Restart services
rm Remove stopped containers
run Run a one-off command
scale Set number of containers for a service
start Start services
stop Stop services
top Display the running processes
unpause Unpause services
up Create and start containers
version Show the Docker-Compose version information
[root@localhost web_statistics]#
扩展学习
## 启动服务
```
docker-compose -f docker-compose.yml up -d
```
## 停止服务
```
docker-compose -f docker-compose.yml stop
```
## 停止并删除服务
```
docker-compose -f docker-compose.yml down
```
PS:容器的日志的查看
[root@localhost web_statistics]# docker logs -f flask_new01 (持续的观察日志的请求)
或
[root@localhost web_statistics]# docker logs flask_new01
[2020-11-05 06:28:17 +0000] [1] [INFO] Starting gunicorn 20.0.4
[2020-11-05 06:28:17 +0000] [1] [INFO] Listening at: http://0.0.0.0:80 (1)
[2020-11-05 06:28:17 +0000] [1] [INFO] Using worker: gevent
[2020-11-05 06:28:17 +0000] [8] [INFO] Booting worker with pid: 8
[2020-11-05 06:28:17 +0000] [9] [INFO] Booting worker with pid: 9
[2020-11-05 06:28:17 +0000] [10] [INFO] Booting worker with pid: 10
[2020-11-05 06:28:17 +0000] [11] [INFO] Booting worker with pid: 11
[2020-11-05 06:28:18 +0000] [12] [INFO] Booting worker with pid: 12
[2020-11-05 06:40:52 +0000] [1] [INFO] Handling signal: term
[2020-11-05 06:40:52 +0000] [10] [INFO] Worker exiting (pid: 10)
[2020-11-05 06:40:52 +0000] [8] [INFO] Worker exiting (pid: 8)
[2020-11-05 06:40:52 +0000] [9] [INFO] Worker exiting (pid: 9)
[2020-11-05 06:40:52 +0000] [11] [INFO] Worker exiting (pid: 11)
[2020-11-05 06:40:52 +0000] [12] [INFO] Worker exiting (pid: 12)
[2020-11-05 06:40:53 +0000] [1] [INFO] Shutting down: Master
[2020-11-05 06:41:08 +0000] [1] [INFO] Starting gunicorn 20.0.4
[2020-11-05 06:41:08 +0000] [1] [INFO] Listening at: http://0.0.0.0:80 (1)
[2020-11-05 06:41:08 +0000] [1] [INFO] Using worker: gevent
[2020-11-05 06:41:08 +0000] [8] [INFO] Booting worker with pid: 8
[2020-11-05 06:41:08 +0000] [9] [INFO] Booting worker with pid: 9
[2020-11-05 06:41:08 +0000] [10] [INFO] Booting worker with pid: 10
[2020-11-05 06:41:08 +0000] [11] [INFO] Booting worker with pid: 11
[2020-11-05 06:41:08 +0000] [12] [INFO] Booting worker with pid: 12
[2020-11-05 06:41:11 +0000] [1] [INFO] Handling signal: term
[2020-11-05 06:41:12 +0000] [8] [INFO] Worker exiting (pid: 8)
[2020-11-05 06:41:12 +0000] [10] [INFO] Worker exiting (pid: 10)
[2020-11-05 06:41:12 +0000] [9] [INFO] Worker exiting (pid: 9)
[2020-11-05 06:41:12 +0000] [11] [INFO] Worker exiting (pid: 11)
[2020-11-05 06:41:12 +0000] [12] [INFO] Worker exiting (pid: 12)
[2020-11-05 06:41:13 +0000] [1] [INFO] Shutting down: Master
[2020-11-05 06:41:19 +0000] [1] [INFO] Starting gunicorn 20.0.4
[2020-11-05 06:41:19 +0000] [1] [INFO] Listening at: http://0.0.0.0:80 (1)
[2020-11-05 06:41:19 +0000] [1] [INFO] Using worker: gevent
[2020-11-05 06:41:19 +0000] [8] [INFO] Booting worker with pid: 8
[2020-11-05 06:41:19 +0000] [9] [INFO] Booting worker with pid: 9
[2020-11-05 06:41:19 +0000] [10] [INFO] Booting worker with pid: 10
[2020-11-05 06:41:19 +0000] [11] [INFO] Booting worker with pid: 11
[2020-11-05 06:41:19 +0000] [12] [INFO] Booting worker with pid: 12
[2020-11-05 07:11:04 +0000] [1] [INFO] Handling signal: term
[2020-11-05 07:11:05 +0000] [8] [INFO] Worker exiting (pid: 8)
[2020-11-05 07:11:05 +0000] [9] [INFO] Worker exiting (pid: 9)
[2020-11-05 07:11:05 +0000] [10] [INFO] Worker exiting (pid: 10)
[2020-11-05 07:11:05 +0000] [11] [INFO] Worker exiting (pid: 11)
[2020-11-05 07:11:05 +0000] [12] [INFO] Worker exiting (pid: 12)
[2020-11-05 07:11:06 +0000] [1] [INFO] Shutting down: Master
[2020-11-05 07:11:28 +0000] [1] [INFO] Starting gunicorn 20.0.4
[2020-11-05 07:11:28 +0000] [1] [INFO] Listening at: http://0.0.0.0:80 (1)
[2020-11-05 07:11:28 +0000] [1] [INFO] Using worker: gevent
[2020-11-05 07:11:28 +0000] [8] [INFO] Booting worker with pid: 8
[2020-11-05 07:11:28 +0000] [9] [INFO] Booting worker with pid: 9
[2020-11-05 07:11:28 +0000] [11] [INFO] Booting worker with pid: 11
[2020-11-05 07:11:28 +0000] [10] [INFO] Booting worker with pid: 10
[2020-11-05 07:11:28 +0000] [12] [INFO] Booting worker with pid: 12
[root@localhost web_statistics]#
实践2:
完整的搭载nginx+redis+postgersql+flaskapp实践(分开容器)
番外
安装完成docker,宿主主机会分配一个docker的网卡,然后docker的网卡通过nat方式转发到不同的容器上。
不同的容器通信的方式都是基本通过IP+端口之间的相互访问。
要实现相关的容器之间的通信,不需要知悉容器内部的分配的IP信息。
通过查看Host查看容器的IP信息
[root@localhost web_statistics]# docker run -id nginx
d61ace7a9ee90a7d47200fe0d079e16bbe3e66a08d5e68ea49cef9a843884431
[root@localhost web_statistics]# docker exec -it nginx bash
Error: No such container: nginx
[root@localhost web_statistics]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d61ace7a9ee9 nginx "/docker-entrypoint.…" 48 seconds ago Up 47 seconds 80/tcp nervous_goldwasser
e64670bf0546 flask_web_stj:latest "gunicorn main:app -…" About an hour ago Up 46 minutes 0.0.0.0:1245->80/tcp flask_new01
792a8edb7c20 flask_web_stj:latest "gunicorn main:app -…" About an hour ago Up 46 minutes 0.0.0.0:1246->80/tcp flask_new02
[root@localhost web_statistics]# docker exec -it
"docker exec" requires at least 2 arguments.
See 'docker exec --help'.
Usage: docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
Run a command in a running container
[root@localhost web_statistics]# docker exec -it d61ace7a9ee9
"docker exec" requires at least 2 arguments.
See 'docker exec --help'.
Usage: docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
Run a command in a running container
[root@localhost web_statistics]# docker exec -it d61ace7a9ee9 bash
root@d61ace7a9ee9:/# cat /etc/hosts
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.2 d61ace7a9ee9
root@d61ace7a9ee9:/#
关键信息:
172.17.0.2 d61ace7a9ee9
root@d61ace7a9ee9:/#
通信基础之 -- link
容器和容器的之间的访问,通过再对于的容器HOST内建立对于的映射来进行通信。
实验
通过一个centos容器访问一个nginx容器。
-
- 测试运行一个nginx 容器
[root@localhost ~]# docker run -d -p 80:80 --name ceshinginx nginx
e3661843c89c1943c33c8fb35f34392b4b3984ca12de4b74f0da77af354f2d5f
[root@localhost ~]#
测试访问:
[root@localhost ~]# curl 127.0.0.1
Welcome to nginx!
Welcome to nginx!
If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.
For online documentation and support please refer to
nginx.org.
Commercial support is available at
nginx.com.
Thank you for using nginx.
[root@localhost ~]#
-
- 运行一个centos 容器
[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
flask_web_stj latest 1410b6bc6d9d 23 hours ago 913MB
newmynginx latest cd903b9a327f 26 hours ago 146MB
python 3.6 1297140c6dd2 35 hours ago 874MB
tomcat latest 891fcd9c5b3a 3 weeks ago 647MB
nginx latest f35646e83998 3 weeks ago 133MB
redis 4.0.14 191c4017dcdd 6 months ago 89.3MB
hell02 latest bf756fb1ae65 10 months ago 13.3kB
centos centos7.1.1503 e1430271e2f9 20 months ago 212MB
ubuntu 15.10 9b9cb95443b5 4 years ago 137MB
[root@localhost ~]# docker run -dit --link ceshinginx:ceshinginx e1430271e2f9
41197fed636aee9f729e7217298257c906b0a50658db36dd262693648b98b0a5
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
41197fed636a e1430271e2f9 "/bin/bash" 28 seconds ago Up 27 seconds sharp_lichterman
e3661843c89c nginx "/docker-entrypoint.…" 2 minutes ago Up 2 minutes 0.0.0.0:80->80/tcp ceshinginx
e64670bf0546 flask_web_stj:latest "gunicorn main:app -…" 2 hours ago Up About an hour 0.0.0.0:1245->80/tcp flask_new01
792a8edb7c20 flask_web_stj:latest "gunicorn main:app -…" 2 hours ago Up About an hour 0.0.0.0:1246->80/tcp flask_new02
[root@localhost ~]# docker exec -it 41197fed636a /bin/bash
使用ceshinginx连接的对应的容器
[root@41197fed636a /]# curl http://ceshinginx
Welcome to nginx!
Welcome to nginx!
If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.
For online documentation and support please refer to
nginx.org.
Commercial support is available at
nginx.com.
Thank you for using nginx.
[root@41197fed636a /]#
-
- 查看 ceshinginx 对于映射的域名(-- link )
[root@41197fed636a /]# cat /etc/hosts
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.2 ceshinginx e3661843c89c
172.17.0.3 41197fed636a
[root@41197fed636a /]#
nginx+redis+postgersql+flaskapp实践(分开容器)实践
说明:
- 1)拉取 postgersql镜像
-
- 拉取 nginx镜像
-
- 拉取redis镜像
- 4) 制作flaskapp镜像
分解步骤篇
一、postgersql 容器的启动
1:拉取镜像
[root@localhost ~]# docker pull postgres:9.4
2:启动镜像并挂载数据
[root@localhost ~]# docker run --name web_statistics_pg -v /data/web_statistics/pgdata:/var/lib/postgresql/data -e POSTGRES_PASSWORD=123456 -p 5432:5432 -d postgres:9.4
993156e83c4bf545b2a1d091903256ec955da33bfce9fe5ae9029e0b9aa77817
[root@localhost ~]#
PS补充
对与已经使用docker run --name web_statistics_pg 启动的若的容器的重新启动问题
The container name "/web_statistics_pg" is already in use by containe
解决方案:
(1)更改启动容器名字, 比如说 docker run -it --name=mycentos2 centos:7 /bin/bash, 可以解决问题.
(2)将原来的容器删除
(3)docker restart 容器ID 或 容器名称 重启该container容器。
3: 测试数据库的链接情况
4: (可选性)查看外部挂载的数据
[root@localhost ~]# cd /data/web_statistics/pgdata
[root@localhost pgdata]# ls
base pg_clog pg_hba.conf pg_logical pg_notify pg_serial pg_stat pg_subtrans pg_twophase pg_xlog postgresql.conf postmaster.pid
global pg_dynshmem pg_ident.conf pg_multixact pg_replslot pg_snapshots pg_stat_tmp pg_tblspc PG_VERSION postgresql.auto.conf postmaster.opts
[root@localhost pgdata]#
5: 使用nacicat创建数据库
6: 进入容器内查看具体信息(通过简写的ID 993)
[root@localhost pgdata]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
993156e83c4b postgres:9.4 "docker-entrypoint.s…" 6 days ago Up 9 minutes 0.0.0.0:5432->5432/tcp web_statistics_pg
e64670bf0546 flask_web_stj:latest "gunicorn main:app -…" 6 days ago Up 31 minutes 0.0.0.0:1245->80/tcp flask_new01
792a8edb7c20 flask_web_stj:latest "gunicorn main:app -…" 6 days ago Up 31 minutes 0.0.0.0:1246->80/tcp flask_new02
[root@localhost pgdata]# docker exec -it 993 bash
root@993156e83c4b:/#
7: 链接数据库查看表数据信息
root@993156e83c4b:/# psql -U postgres
psql (9.4.26)
Type "help" for help.
postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
------------+----------+----------+------------+------------+-----------------------
postgres | postgres | UTF8 | en_US.utf8 | en_US.utf8 |
template0 | postgres | UTF8 | en_US.utf8 | en_US.utf8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | en_US.utf8 | en_US.utf8 | =c/postgres +
| | | | | postgres=CTc/postgres
测试数据库 | postgres | UTF8 | en_US.utf8 | en_US.utf8 |
(4 rows)
postgres=#
补充PostgreSQL场景的进入命令之后的一些操作
---------------
接入PostgreSQL数据库: psql -h IP地址 -p 端口 -U 数据库名
之后会要求输入数据库密码
二、访问数据库
1、列举数据库:\l
2、选择数据库:\c 数据库名
3、查看该某个库中的所有表:\dt
4、切换数据库:\c interface
5、查看某个库中的某个表结构:\d 表名
6、查看某个库中某个表的记录:select * from apps limit 1;
7、显示字符集:\encoding
8、退出psgl:\q
二、redis 容器的启动
1:拉取镜像
[root@localhost ~]# docker pull redis:4.0.14
[root@localhost pgdata]# docker images
\REPOSITORY TAG IMAGE ID CREATED SIZE
flask_web_stj latest 1410b6bc6d9d 7 days ago 913MB
newmynginx latest cd903b9a327f 7 days ago 146MB
python 3.6 1297140c6dd2 8 days ago 874MB
tomcat latest 891fcd9c5b3a 4 weeks ago 647MB
nginx latest f35646e83998 4 weeks ago 133MB
redis 4.0.14 191c4017dcdd 6 months ago 89.3MB
postgres 9.4 ed5a45034282 9 months ago 251MB
2:配置对于的redis.conf文件用于挂载同步到容器内部
[root@localhost web_statistics]# ll
total 32
-rw-r--r--. 1 root root 372 Nov 5 14:27 docker-compose.yaml
-rw-r--r--. 1 root root 253 Nov 4 16:29 Dockerfile
-rw-r--r--. 1 root root 235 Nov 4 16:47 gunicorn.conf.py
-rw-r--r--. 1 root root 10502 Nov 4 16:29 main.py
drwxr-xr-x. 3 root root 169 Nov 4 16:29 models
drwx------. 18 systemd-bus-proxy root 4096 Nov 12 10:27 pgdata
-rw-r--r--. 1 root root 274 Nov 4 16:29 requirements.txt
[root@localhost web_statistics]# cd pgdata/
[root@localhost pgdata]# cd ..
[root@localhost web_statistics]# touch redis.conf
[root@localhost web_statistics]# ls
docker-compose.yaml Dockerfile gunicorn.conf.py main.py models pgdata redis.conf requirements.txt
[root@localhost web_statistics]#
3:修改redis配置选项主要修改的地方有:
参考之前的安装:https://www.jianshu.com/p/c450612c6295
配置远程访问(生产环境请勿开启)
bind 127.0.0.1 ---->bind 0.0.0.0
protected-mode no #默认yes,开启保护模式,限制为本地访问
daemonize no#默认no,改为yes意为以守护进程方式启动,可后台运行,除非kill进程,改为yes会使配置文件方式启动redis失败
databases 16 #数据库个数(可选)
appendonly yes #redis持久化(可选)
requirepass 密码 #配置redis访问密码
4:创建redis可持久化数据的存放外部目录,用于数据持久化存储:
[root@localhost web_statistics]# mkdir redisdata
[root@localhost web_statistics]# ll
total 32
-rw-r--r--. 1 root root 372 Nov 5 14:27 docker-compose.yaml
-rw-r--r--. 1 root root 253 Nov 4 16:29 Dockerfile
-rw-r--r--. 1 root root 235 Nov 4 16:47 gunicorn.conf.py
-rw-r--r--. 1 root root 10502 Nov 4 16:29 main.py
drwxr-xr-x. 3 root root 169 Nov 4 16:29 models
drwx------. 18 systemd-bus-proxy root 4096 Nov 12 10:27 pgdata
-rw-r--r--. 1 root root 0 Nov 12 10:58 redis.conf
drwxr-xr-x. 2 root root 6 Nov 12 10:59 redisdata
-rw-r--r--. 1 root root 274 Nov 4 16:29 requirements.txt
[root@localhost web_statistics]#
5:启动redis 容器
docker run -p 6379:6379 --name web_staredis_redis -v /data/web_statistics/redis.conf:/etc/redis/redis.conf -v /data/web_statistics/redisdata:/data -d redis:4.0.14 redis-server /etc/redis/redis.conf --appendonly yes --requirepass 123456
[root@localhost web_statistics]# docker run -p 6379:6379 --name web_staredis_redis -v /data/web_statistics/redis.conf:/etc/redis/redis.conf -v /data/web_statistics/redisdata:/data -d redis:4.0.14 redis-server /etc/redis/redis.conf --appendonly yes --requirepass 123456
不指定密码的话默认不需要密码--requirepass 123456
错误异常:
[root@localhost web_statistics]# docker run -p 6379:6379 --name web_staredis_redis -v /data/web_statistics/redis.conf:/etc/redis/redis.conf -v /data/web_statistics/redisdata:/data -d redis:4.0.14 redis-server /etc/redis/redis.conf --appendonly yes
docker: Error response from daemon: Conflict. The container name "/web_staredis_redis" is already in use by container "a6575e591f13d718be79dc860ab61cb1476a06a7bc3a64b5f8bf34e63b3ca844". You have to remove (or rename) that container to be able to reuse that name.
See 'docker run --help'.
[root@localhost web_statistics]# docker restart web_staredis_redis
web_staredis_redis
[root@localhost web_statistics]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
993156e83c4b postgres:9.4 "docker-entrypoint.s…" 6 days ago Up 53 minutes 0.0.0.0:5432->5432/tcp web_statistics_pg
e64670bf0546 flask_web_stj:latest "gunicorn main:app -…" 6 days ago Up About an hour 0.0.0.0:1245->80/tcp flask_new01
792a8edb7c20 flask_web_stj:latest "gunicorn main:app -…" 6 days ago Up About an hour 0.0.0.0:1246->80/tcp flask_new02
[root@localhost web_statistics]#
发现容器没有启动,需要查看容器的启动的日志信息
[root@localhost ~]# docker logs -f -t --tail 100 web_staredis_redis
2020-11-12T03:17:34.061055854Z 1:C 12 Nov 03:17:34.052 # Can't chdir to '/usr/local/redis/data': No such file or directory
2020-11-12T03:20:33.072935555Z 1:C 12 Nov 03:20:33.067 # Can't chdir to '/usr/local/redis/data': No such file or directory
[root@localhost ~]#
提示错误的异常:检测redis.conf的配置文件信息
[root@localhost web_statistics]# docker logs -ft web_staredis_redis
2020-11-12T04:48:30.753000868Z 1:C 12 Nov 04:48:30.749 # Can't chdir to '/usr/local/redis/data': No such file or directory
[root@localhost web_statistics]# ^C
[root@localhost web_statistics]#
修改为:
dir ./ #输入本地redis数据库存放文件夹(可选)
重新的启动再查看日志:
[root@localhost web_statistics]# docker run -p 6379:6379 --name web_staredis_redis -v /data/web_statistics/redis.conf:/etc/redis/redis.conf -v /data/web_statistics/redisdata:/data -d redis:4.0.14 redis-server /etc/redis/redis.conf --appendonly yes
895b8d1ed815cf86a49502770fcc64ae19ad4983bab76cea5b6468ff65923b56
[root@localhost web_statistics]#
日志内容:
[root@localhost web_statistics]# docker logs -ft web_staredis_redis
2020-11-12T04:55:23.202270517Z 1:C 12 Nov 04:55:23.199 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
2020-11-12T04:55:23.202290927Z 1:C 12 Nov 04:55:23.199 # Redis version=4.0.14, bits=64, commit=00000000, modified=0, pid=1, just started
2020-11-12T04:55:23.202294330Z 1:C 12 Nov 04:55:23.199 # Configuration loaded
2020-11-12T04:55:23.202296738Z 1:M 12 Nov 04:55:23.201 * Running mode=standalone, port=6379.
2020-11-12T04:55:23.202298865Z 1:M 12 Nov 04:55:23.201 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
2020-11-12T04:55:23.202306490Z 1:M 12 Nov 04:55:23.201 # Server initialized
2020-11-12T04:55:23.202308781Z 1:M 12 Nov 04:55:23.201 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
2020-11-12T04:55:23.202311189Z 1:M 12 Nov 04:55:23.201 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
2020-11-12T04:55:23.202314377Z 1:M 12 Nov 04:55:23.201 * Ready to accept connections
再查看镜像信息
[root@localhost web_statistics]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
895b8d1ed815 redis:4.0.14 "docker-entrypoint.s…" About a minute ago Up About a minute 0.0.0.0:6379->6379/tcp web_staredis_redis
993156e83c4b postgres:9.4 "docker-entrypoint.s…" 6 days ago Up 2 hours 0.0.0.0:5432->5432/tcp web_statistics_pg
e64670bf0546 flask_web_stj:latest "gunicorn main:app -…" 6 days ago Up 3 hours 0.0.0.0:1245->80/tcp flask_new01
792a8edb7c20 flask_web_stj:latest "gunicorn main:app -…" 6 days ago Up 3 hours 0.0.0.0:1246->80/tcp flask_new02
[root@localhost web_statistics]#
三、nginx 容器的启动
1: 查看已存在的镜像没有的话 则从新拉取
[root@localhost web_statistics]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
flask_web_stj latest 1410b6bc6d9d 7 days ago 913MB
newmynginx latest cd903b9a327f 7 days ago 146MB
python 3.6 1297140c6dd2 8 days ago 874MB
tomcat latest 891fcd9c5b3a 4 weeks ago 647MB
nginx latest f35646e83998 4 weeks ago 133MB
redis 4.0.14 191c4017dcdd 6 months ago 89.3MB
postgres 9.4 ed5a45034282 9 months ago 251MB
hell02 latest bf756fb1ae65 10 months ago 13.3kB
centos centos7.1.1503 e1430271e2f9 20 months ago 212MB
ubuntu 15.10 9b9cb95443b5 4 years ago 137MB
[root@localhost web_statistics]#
2:nginx.conf(nginx_web_statistics_api.conf) 配置文件的编写,用于反向到flask
upstream web_statistics_api {
server 127.0.0.1:1245 weight=1 max_fails=0 fail_timeout=12s;
server 127.0.0.1:1246 weight=1 max_fails=0 fail_timeout=12s;
}
server {
listen 80;
server_name 192.168.219.130; # 这是HOST机器的外部域名,用地址也行
location / {
proxy_pass http://web_statistics_api ; # 这里是指向 gunicorn host 的服务地址
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
3:挂载配置文件到容器内部,并启动容器
docker run --name nginx_flask_api -v /data/web_statistics/nginx_web_statistics_api.conf:/etc/nginx/conf.d/default.conf -d nginx
查看容器没正常启动,查看日志:
[root@localhost web_statistics]# docker logs -ft nginx_flask_api
2020-11-12T05:13:04.143913388Z /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
2020-11-12T05:13:04.143964086Z /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
2020-11-12T05:13:04.153475026Z /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
2020-11-12T05:13:04.162648668Z 10-listen-on-ipv6-by-default.sh: Getting the checksum of /etc/nginx/conf.d/default.conf
2020-11-12T05:13:04.174473655Z 10-listen-on-ipv6-by-default.sh: error: /etc/nginx/conf.d/default.conf differs from the packaged version
2020-11-12T05:13:04.174807502Z /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
2020-11-12T05:13:04.179840033Z /docker-entrypoint.sh: Configuration complete; ready for start up
2020-11-12T05:13:04.204432876Z 2020/11/12 05:13:04 [emerg] 1#1: unexpected end of file, expecting ";" or "}" in /etc/nginx/conf.d/default.conf:1058
2020-11-12T05:13:04.204451325Z nginx: [emerg] unexpected end of file, expecting ";" or "}" in /etc/nginx/conf.d/default.conf:1058
[root@localhost web_statistics]#
修改配置文件后
[root@localhost web_statistics]# docker run --name nginx_flask_api -v /data/web_statistics/nginx_web_statistics_api.conf:/etc/nginx/conf.d/default.conf -d nginx
5e823f5e6f1486c3f39f8c5c7a540210b142ce13959e13d65a3354effb5f6d7c
[root@localhost web_statistics]#
再看日志
[root@localhost web_statistics]# docker logs -ft nginx_flask_api
2020-11-12T05:17:33.097731784Z /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
2020-11-12T05:17:33.097755572Z /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
2020-11-12T05:17:33.104457769Z /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
2020-11-12T05:17:33.110354247Z 10-listen-on-ipv6-by-default.sh: Getting the checksum of /etc/nginx/conf.d/default.conf
2020-11-12T05:17:33.113142158Z 10-listen-on-ipv6-by-default.sh: error: /etc/nginx/conf.d/default.conf differs from the packaged version
2020-11-12T05:17:33.114474992Z /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
2020-11-12T05:17:33.124894602Z /docker-entrypoint.sh: Configuration complete; ready for start up
查看运行中的容器:
[root@localhost web_statistics]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5e823f5e6f14 nginx "/docker-entrypoint.…" 40 seconds ago Up 39 seconds 80/tcp nginx_flask_api
895b8d1ed815 redis:4.0.14 "docker-entrypoint.s…" 22 minutes ago Up 22 minutes 0.0.0.0:6379->6379/tcp web_staredis_redis
993156e83c4b postgres:9.4 "docker-entrypoint.s…" 6 days ago Up 3 hours 0.0.0.0:5432->5432/tcp web_statistics_pg
e64670bf0546 flask_web_stj:latest "gunicorn main:app -…" 6 days ago Up 3 hours 0.0.0.0:1245->80/tcp flask_new01
792a8edb7c20 flask_web_stj:latest "gunicorn main:app -…" 6 days ago Up 3 hours 0.0.0.0:1246->80/tcp flask_new02
[root@localhost web_statistics]#
指定启动的nginx端口映射
[root@localhost web_statistics]# docker run --name nginx_flask_api -p 80:80 -v /data/web_statistics/nginx_web_statistics_api.conf:/etc/nginx/conf.d/default.conf -d nginx
c4672ebaf6ad0be4522cc4654f93c9d4a51b64850a8b1182236c47a9966f9633
[root@localhost web_statistics]# docker rm nginx_flask_api^C
[root@localhost web_statistics]# docker ps
四、测试验证流程
1:访问nginx
502错误。错误无法访问的上游服务。
2:查看nginx配置文件:
upstream web_statistics_api {
server 127.0.0.1:1245 weight=1 max_fails=0 fail_timeout=12s;
server 127.0.0.1:1246 weight=1 max_fails=0 fail_timeout=12s;
}
server {
listen 80;
server_name 192.168.219.130; # 这是HOST机器的外部域名,用地址也行
location / {
proxy_pass http://web_statistics_api ; # 这里是指向 gunicorn host 的服务地址
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
更新配置无果:
root@c4672ebaf6ad:/# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
root@c4672ebaf6ad:/# nginx -s reload
2020/11/12 05:31:07 [notice] 34#34: signal process started
root@c4672ebaf6ad:/#
PS:修改外部挂载的nginx文件,如果配置文件有更新的话,内部的nginx需要生效的话 ,需要进行
nginx -s reload
问题定位。nginx,不能访问内部的flask端口,需要访问宿主的端口:
nginx容器内部访问应该 也是flask容器内部的地址,直接访问flask外部的地址是无法访问的,需要用link进行串联
进入nginx容器内部,尝试访问外部的flask接口
[root@localhost web_statistics]# docker exec -it nginx_flask_api /bin/bash
root@a2a95e5d943b:/# nginx -s reload
2020/11/12 05:37:11 [notice] 41#41: signal process started
root@a2a95e5d943b:/# curl
curl: try 'curl --help' or 'curl --manual' for more information
root@a2a95e5d943b:/# curl 127.0.0.1
curl: (7) Failed to connect to 127.0.0.1 port 80: Connection refused
root@a2a95e5d943b:/# curl 127.0.0.1:8081
502 Bad Gateway
502 Bad Gateway
nginx/1.19.3
访问nginx配置的端口
root@a2a95e5d943b:/# curl 127.0.0.1:1245
curl: (7) Failed to connect to 127.0.0.1 port 1245: Connection refused
访问外部的宿主的端口
root@a2a95e5d943b:/# curl 192.168.219.130:1245
400 Bad Request
Bad Request
The browser (or proxy) sent a request that this server could not understand.
3:修改nginx配置文件,且重载:
upstream web_statistics_api {
server 192.168.219.130:1245 weight=1 max_fails=0 fail_timeout=12s;
server 192.168.219.130:1246 weight=1 max_fails=0 fail_timeout=12s;
}
server {
listen 80;
server_name 192.168.219.130; # 这是HOST机器的外部域名,用地址也行
location / {
proxy_pass http://web_statistics_api ; # 这里是指向 gunicorn host 的服务地址
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
重载nginx
root@a2a95e5d943b:/# nginx -s reload
2020/11/12 08:31:01 [notice] 53#53: signal process started
root@a2a95e5d943b:/#
4:再次访问
5:结果已经说明从nginx层访问到了flask层
docker compose 编排整合
fpnr_docker-compose.yaml 文件编写
version: "3.3"
services:
flask_web_stj_nginx:
container_name: nginx_flask_api_yaml
restart: always
image: nginx
volumes:
- /data/web_statistics/nginx_web_statistics_api.conf:/etc/nginx/conf.d/default.conf
ports:
- "80:80"
flask_web_stj_pgdb:
container_name: web_statistics_pg_yaml
restart: always
image: postgres:9.4
volumes:
- /data/web_statistics/pgdata:/var/lib/postgresql/data
ports:
- "5432:5432"
flask_web_stj_redis:
container_name: web_staredis_redis_yaml
restart: always
image: redis:4.0.14
volumes:
- /data/web_statistics/redis.conf:/etc/redis/redis.conf
- /data/web_statistics/redisdata:/data
command: redis-server /etc/redis/redis.conf --appendonly yes --requirepass 123456
ports:
- "6379:6379"
flask_web_stj_01:
image: flask_web_stj:latest
build: .
container_name: flask_new01_yaml
restart: always
ports:
- "1255:80"
flask_web_stj_02:
image: flask_web_stj:latest
build: .
container_name: flask_new02_yaml
restart: always
ports:
- "1256:80"
指定文件启动服务
docker-compose -f fpnr_docker-compose.yaml up -d
扩展学习
## 启动服务
```
docker-compose -f docker-compose.yml up -d
```
## 停止服务
```
docker-compose -f docker-compose.yml stop
```
## 停止并删除服务
```
docker-compose -f docker-compose.yml down
```
异常排查
[root@localhost web_statistics]# docker-compose -f fpnr_docker-compose.yaml up -d
Recreating flask_new01 ...
Recreating flask_new02 ...
Creating web_statistics_pg_yaml ...
Creating web_staredis_redis_yaml ...
Creating nginx_flask_api_yaml ... error
Creating web_staredis_redis_yaml ... error
dcf96703431f0f9108f932eb8eb1b568df0568a866779ba2a0d08ead0a322): Bind for 0.0.0.0:80 failed: port is already allocated
Creating web_statistics_pg_yaml ... error
Recreating flask_new01 ... done
Recreating flask_new02 ... done
ERROR: for web_statistics_pg_yaml Cannot start service flask_web_stj_pgdb: driver failed programming external connectivity on endpoint web_statistics_pg_yaml (86f30ff09f92619081f41dd140f326e7208abfc8d35007943a8fcfaee2d65022): Bind for 0.0.0.0:5432 failed: port is already allocated
ERROR: for flask_web_stj_nginx Cannot start service flask_web_stj_nginx: driver failed programming external connectivity on endpoint nginx_flask_api_yaml (c47dcf96703431f0f9108f932eb8eb1b568df0568a866779ba2a0d08ead0a322): Bind for 0.0.0.0:80 failed: port is already allocated
ERROR: for flask_web_stj_redis Cannot start service flask_web_stj_redis: driver failed programming external connectivity on endpoint web_staredis_redis_yaml (5301b336f4ac40ba428c068bd06d2c706ed4cab6e19ed3c36b8194cb9f8f4f09): Bind for 0.0.0.0:6379 failed: port is already allocated
ERROR: for flask_web_stj_pgdb Cannot start service flask_web_stj_pgdb: driver failed programming external connectivity on endpoint web_statistics_pg_yaml (86f30ff09f92619081f41dd140f326e7208abfc8d35007943a8fcfaee2d65022): Bind for 0.0.0.0:5432 failed: port is already allocated
ERROR: Encountered errors while bringing up the project.
[root@localhost web_statistics]#
暂停以前一些容器的运行和端口的占用
[root@localhost web_statistics]# docker stop nginx_flask_api
nginx_flask_api
[root@localhost web_statistics]# docker stop web_staredis_redis
web_staredis_redis
[root@localhost web_statistics]# docker stop web_statistics_pg
web_statistics_pg
[root@localhost web_statistics]# docker stop flask_new02_yaml
flask_new02_yaml
[root@localhost web_statistics]# docker stop flask_new01_yaml
flask_new01_yaml
[root@localhost web_statistics]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
[root@localhost web_statistics]#
重新再跑一次
[root@localhost web_statistics]# docker-compose -f fpnr_docker-compose.yaml up -d
Starting web_statistics_pg_yaml ... done
Starting flask_new01_yaml ... done
Starting web_staredis_redis_yaml ... done
Starting flask_new02_yaml ... done
Starting nginx_flask_api_yaml ... done
[root@localhost web_statistics]#
[root@localhost web_statistics]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
049838d17129 flask_web_stj:latest "gunicorn main:app -…" 3 minutes ago Up 24 seconds 0.0.0.0:1256->80/tcp flask_new02_yaml
89e76e82744c flask_web_stj:latest "gunicorn main:app -…" 3 minutes ago Up 24 seconds 0.0.0.0:1255->80/tcp flask_new01_yaml
d2b810aa864e redis:4.0.14 "docker-entrypoint.s…" 3 minutes ago Up 24 seconds 0.0.0.0:6379->6379/tcp web_staredis_redis_yaml
1ea8940f57f9 postgres:9.4 "docker-entrypoint.s…" 3 minutes ago Up 24 seconds 0.0.0.0:5432->5432/tcp web_statistics_pg_yaml
f55811e5e3f2 nginx "/docker-entrypoint.…" 3 minutes ago Up 25 seconds 0.0.0.0:80->80/tcp nginx_flask_api_yaml
[root@localhost web_statistics]#
停止所有的服务:
[root@localhost web_statistics]# docker-compose -f fpnr_docker-compose.yaml stop
Stopping flask_new02_yaml ... done
Stopping flask_new01_yaml ... done
Stopping web_staredis_redis_yaml ... done
Stopping web_statistics_pg_yaml ... done
Stopping nginx_flask_api_yaml ... done
[root@localhost web_statistics]#
再次访问接口: