NGINX 不会与 Docker 容器中的 Google Cloud Storage FUSE 一起运行

问题描述 投票:0回答:1

我在

docker-compose.yml
文件中定义了两项服务。

version: "3.9"
services:
  rtmp:
    build: ./live-stream
    ports:
      - "1935:1935"
      - "8080:8080"
    container_name: "live_server"
    privileged: true
    environment:
      GOOGLE_APPLICATION_CREDENTIALS: "/service-account-key.json"
    volumes:
      - ./data:/tmp/hls
  auth:
    build: ./auth
    container_name: auth_server

里面

live-stream
我有以下
Dockerfile

FROM tiangolo/nginx-rtmp

RUN apt-get update \
&& apt-get install -y sudo

COPY index.html /www/
COPY service-account-key.json .

ENV GCSFUSE_REPO gcsfuse-buster
RUN echo $GCSFUSE_REPO
RUN echo "deb https://packages.cloud.google.com/apt $GCSFUSE_REPO main" | sudo tee /etc/apt/sources.list.d/gcsfuse.list
RUN curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
RUN sudo apt-get update -y
RUN sudo apt-get install fuse gcsfuse -y

RUN echo "deb [signed-by=/usr/share/keyrings/cloud.google.gpg] 
https://packages.cloud.google.com/apt cloud-sdk main" | sudo tee -a 
/etc/apt/sources.list.d/google-cloud-sdk.list && curl 
https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo gpg --dearmor -o 
/usr/share/keyrings/cloud.google.gpg && sudo apt-get update -y && sudo apt-get 
install google-cloud-sdk -y

RUN sudo gcloud auth activate-service-account --key-file=/service-account-key.json
ENV GOOGLE_APPLICATION_CREDENTIALS "/service-account-key.json"
RUN mkdir /ls_media
CMD ["sudo", "gcsfuse", "--foreground", "--key-file", "/service-account-key.json", "****", "/ls_media"]

我正在使用

tiangolo/nginx-rtmp
图像来创建 nginx rtmp 服务器。 当 gcsfuse 未运行时,RTMP 服务器可以正常工作,但是一旦启动,NGINX 服务器就无法再访问。

容器中没有错误日志。我已经测试了每一行,NGINX 服务器运行良好,直到

CMD ["sudo", "gcsfuse", "--foreground", "--key-file", "/service-account-key.json", "****", "/ls_media"]

这是我的 NGINX 配置。

events {}

rtmp {
    server {
        listen 1935;
        application live {
            live on;
            hls on;
            hls_path /tmp/hls;
            hls_fragment 2s;
            hls_playlist_length 40s;

            on_publish http://auth_server:8080/auth;
        }
    }
}

http {
    server {
        listen 8080;
        location / {
            root /www;
        }

        location /hls {
            # Types this root accepts
            types { 
                application/vnd.apple.mpegurl m3u8;
                application/octet-stream ts;
            }

            root /tmp;
            add_header Cache-Control no-cache;
            add_header Access-Control-Allow-Origin *;
        }
    }
}
docker nginx docker-compose google-cloud-storage nginx-rtmp
1个回答
0
投票

问题是

CMD
中的
Dockerfile
覆盖了基础图像中的
CMD
。当你的 CMD
没有
有这个
Dockerfile
时,NGINX就会运行。当你do拥有它时,它将运行而不是NGINX。您希望它们两者都运行。

假设您的项目看起来(隐约!)像这样:

├── docker-compose.yml
└── live-stream
    ├── Dockerfile
    ├── entrypoint.sh
    ├── index.html
    └── service-account-key.json

🗎

docker-compose.yml
(我删除了一些与实际问题无关的内容。)

version: "3.9"
services:
  rtmp:
    build: ./live-stream
    ports:
      - "1935:1935"
      - "8080:8080"
    container_name: live_server
    privileged: true
    environment:
      GOOGLE_APPLICATION_CREDENTIALS: "/service-account-key.json"

🗎

Dockerfile

FROM tiangolo/nginx-rtmp

COPY index.html /www/
COPY service-account-key.json .

ENV GCSFUSE_REPO gcsfuse-buster
RUN echo $GCSFUSE_REPO
RUN echo "deb https://packages.cloud.google.com/apt $GCSFUSE_REPO main" | tee /etc/apt/sources.list.d/gcsfuse.list
RUN curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add -
RUN apt-get update -y && \
    apt-get install fuse gcsfuse -y

RUN echo "deb [signed-by=/usr/share/keyrings/cloud.google.gpg] https://packages.cloud.google.com/apt cloud-sdk main" | tee -a /etc/apt/sources.list.d/google-cloud-sdk.list && \
    curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | gpg --dearmor -o /usr/share/keyrings/cloud.google.gpg && \
    apt-get update -y && \
    apt-get install -y google-cloud-sdk

RUN gcloud auth activate-service-account --key-file=/service-account-key.json
ENV GOOGLE_APPLICATION_CREDENTIALS "/service-account-key.json"
RUN mkdir /ls_media

COPY entrypoint.sh .
CMD /bin/bash entrypoint.sh

🗎

entrypoint.sh

#! /bin/bash

gcsfuse --foreground --key-file /service-account-key.json **** /ls_media &
nginx -g "daemon off;"

🚨我没有有效的

service-account-key.json
,所以无法测试以上所有内容。但是,关键是容器启动时您没有运行
entrypoint.sh
脚本。该脚本将在后台运行
gcsfuse
,然后启动 NGINX。然后,您将在容器中有效地运行两个进程。这可能会很好地工作。然而,一般来说,您希望每个容器只有一个进程,因此更好的设计可能是拥有一个包含两个服务的 Docker Compose 堆栈,一个用于 NGINX,另一个用于
gcsfuse

此外,您不需要在

sudo
中使用
Dockerfile

© www.soinside.com 2019 - 2024. All rights reserved.