首先,在一个容器中运行redis、postgres 和"main container" 是不是最佳做法。
通常,您应该有 3 个独立的容器(每个容器一个应用程序)通过网络进行通信。有时我们想在同一个容器中运行两个或多个轻量级服务,但 redis 和 postgres 不是这样的服务。
我推荐阅读:best practices for building containers。
但是,使用supervisord 进程管理系统可以在同一个 docker 容器中拥有多个服务。
我将在一个 docker 容器中同时运行 redis 和 postgres 服务(这与您的问题类似)来说明它是如何工作的。仅用于演示目的。
这是一个目录结构,我们只需要Dockerfile和supervisor.conf(supervisord配置文件):
$ tree example_container/
example_container/
├── Dockerfile
└── supervisor.conf
首先,我创建了一个supervisord 配置文件,其中定义了redis 和postgres 服务:
$ cat example_container/supervisor.conf
[supervisord]
nodaemon=true
[program:redis]
command=redis-server # command to run redis service
autorestart=true
stderr_logfile=/dev/stdout
stderr_logfile_maxbytes = 0
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes = 0
[program:postgres]
command=/usr/lib/postgresql/12/bin/postgres -D /var/lib/postgresql/12/main/ -c config_file=/etc/postgresql/12/main/postgresql.conf # command to run postgres service
autostart=true
autorestart=true
stderr_logfile=/dev/stdout
stderr_logfile_maxbytes = 0
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes = 0
user=postgres
environment=HOME="/var/lib/postgresql",USER="postgres"
接下来我创建了一个简单的Dockerfile:
$ cat example_container/Dockerfile
FROM ubuntu:latest
ARG DEBIAN_FRONTEND=noninteractive
# Installing redis and postgres
RUN apt-get update && apt-get install -y supervisor redis-server postgresql-12
# Copying supervisor configuration file to container
ADD supervisor.conf /etc/supervisor.conf
# Initializing redis and postgres services using supervisord
CMD ["supervisord","-c","/etc/supervisor.conf"]
然后我构建了 docker 镜像:
$ docker build -t example_container:v1 .
最后我使用上图运行并测试了 docker 容器:
$ docker run --name multi_services -dit example_container:v1
472c7b2eac7441360126f8fcd0cc80e0e63ac3039f8195715a3a400f6288a236
$ docker exec -it multi_services bash
root@472c7b2eac74:/# ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.7 0.1 27828 23372 pts/0 Ss+ 10:04 0:00 /usr/bin/python3 /usr/bin/supervisord -c /etc/supervisor.conf
postgres 8 0.1 0.1 212968 28972 pts/0 S 10:04 0:00 /usr/lib/postgresql/12/bin/postgres -D /var/lib/postgresql/12/main/ -c config_file=/etc/postgresql/12/main/postgresql.conf
root 9 0.1 0.0 47224 6216 pts/0 Sl 10:04 0:00 redis-server *:6379
...
root@472c7b2eac74:/# netstat -tulpn
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:6379 0.0.0.0:* LISTEN 9/redis-server *:6
tcp 0 0 127.0.0.1:5432 0.0.0.0:* LISTEN 8/postgres
tcp6 0 0 :::6379 :::* LISTEN 9/redis-server *:6
如您所见,可以在一个容器中拥有多个服务,但这是一种不推荐的方法,应该仅用于测试。