我有这个dockerfile:
FROM node:16
ADD . /src
WORKDIR /src
# Install OpenJDK-11
RUN echo 'deb http://ftp.debian.org/debian stretch-backports main' | tee /etc/apt/sources.list.d/stretch-backports.list
RUN apt-get update && \
apt-get install -y openjdk-11-jre-headless && \
apt-get clean;
RUN npm i -g firebase-tools
RUN firebase --version
EXPOSE 9099 4000
我有这个 firebase.json:
{
"emulators": {
"auth": {
"port": 9099
},
"ui": {
"enabled": true,
"port": 4000
}
}
}
我有这个 docker-compose 文件:
version: "3.0"
services:
firebase:
build:
context: .
dockerfile: Dockerfile.firebase-emulator
volumes:
- ./fb-data:/src
ports:
- "9099:9099"
- "4000:4000"
stdin_open: true
tty: true
然后我运行
docker exec -it <container-id> sh
并开始在里面运行这些命令:
firebase login --no-localhost
firebase emulators:start --project demo-test
结果是这样的:
当我在浏览器中访问 http://localhost:4000/auth 结果是:
暴露的docker端口有问题吗?
谢谢您的帮助!
经过更多挖掘,为了使其工作,firebase.json 需要具有“host”属性:
{
"emulators": {
"auth": {
"port": 9099,
"host": "0.0.0.0"
},
"ui": {
"enabled": true,
"host": "0.0.0.0",
"port": 4000
}
}
}
我最近遇到了同样的问题,我的日志中充满了如下警告:
ui:尝试检查 ::1 上的端口 4000 时出错:错误:侦听 EADDRNOTAVAIL:地址不可用 ::1:4000
事实证明,这个问题与 Docker 处理 IPv6 和 IPv4 的方式有关。您可以在此处更深入地了解详细信息。
虽然我无法直接解决问题,但按照这个 guide & repo,我确实偶然发现了一个似乎有效的 Docker 配置。
注意:当您运行此命令时,这不会立即起作用。 您应该在容器 shell 中运行
firebase login --no-localhost
和 firebase init
。这会将您连接到现有/新的 Firebase 项目,并根据您的选择设置 Firebase 配置文件。
Dockerfile 看起来像:
# Start with a Node.js base image
FROM node:20-alpine
# Install necessary packages
RUN apk --no-cache add openjdk11-jre bash curl openssl gettext nano nginx sudo \
&& npm cache clean --force \
&& npm install -g firebase-tools@$FIREBASE_VERSION
# Copy the nginx configuration file from your project directory to the container
COPY nginx.conf /etc/nginx/nginx.conf
# Copy the serve.sh script into the container and ensure it is executable
COPY serve.sh /usr/local/bin/serve.sh
RUN chmod +x /usr/local/bin/serve.sh
# Set the working directory
WORKDIR /srv/firebase
# Copy the rest of your application (firebase directory contents)
COPY . /srv/firebase
# Expose the ports used by your services
EXPOSE 4000 4400 4600 5001 8080 8085 9000 9099 9199 6000
# Define the command to run your application
CMD ["/usr/local/bin/serve.sh"]
docker-compose.yaml:
version: '3.8'
services:
emulator:
build:
context: ./firebase
dockerfile: Dockerfile
args:
- FIREBASE_VERSION=13.6.0
stop_grace_period: 1m
environment:
FIREBASE_AUTH_EMULATOR_HOST: "localhost:9099"
FIRESTORE_EMULATOR_HOST: "localhost:8080"
PUBSUB_EMULATOR_HOST: "localhost:8085"
FUNCTIONS_EMULATOR_HOST: "localhost:5001"
FIREBASE_PROJECT: "test-project"
GCLOUD_PROJECT: "test-project"
FORCE_COLOR: 'true'
DATA_DIRECTORY: "data"
CHOKIDAR_USEPOLLING: 'true'
ports:
- "4000:4001" # ui
- "4400:4401" # hub
- "4600:4601" # logging
- "5001:5002" # functions
- "8080:8081" # firestore
- "8085:8086" # pubsub
- "9000:9001" # database
- "9099:9100" # auth
- "9199:9200" # Storage
- "6000:6001" # Hosting
volumes:
- ./firebase:/srv/firebase:rw
- ./firebase/cache:/root/.cache/:rw
- ~/.config/:/root/.config
- ./firebase/data:/srv/firebase/data:rw
的东西
此外,如果功能一开始无法工作。进入函数目录并运行
npm run build
。即使出现错误,也请返回 /srv/firebase 并重新启动模拟器。