我需要在docker命令中传递env值,并在角应用程序中获得该值。但根据我目前的码头结构,它不像预期的那样工作。我在下面解释我的代码。
文档:
FROM node:12-alpine as angular-build
#FROM nginx:1.17.1-alpine
RUN echo "NODE Version:" && node --version
RUN echo "NPM Version:" && npm --version
COPY ./app /app
WORKDIR /app/mean-stack/angular-js
RUN npm install
#COPY . /app/mean-stack/angular-js
RUN npm run build --prod
FROM node:10.22.1-alpine3.9
ENV PYTHONUNBUFFERED=1
RUN apk add --update --no-cache python3 && ln -sf python3 /usr/bin/python
RUN python3 -m ensurepip
RUN pip3 install --no-cache --upgrade pip setuptools
RUN apk update && apk add --no-cache openssl-dev libffi-dev gcc musl-dev python3-dev make
RUN pip install flask requests rstr robotframework natsort
RUN pip install paramiko dpath zipfile36 simplejson
RUN pip install jsonpickle
RUN echo "NODE Version:" && node --version
RUN echo "NPM Version:" && npm --version
COPY install-nginx-alpine.sh /
RUN sh /install-nginx-alpine.sh
EXPOSE 90
# # Expose 443, in case of LTS / HTTPS
EXPOSE 443
# Install uWSGI
RUN apk add --no-cache uwsgi-python3
# Copy the base uWSGI ini file to enable default dynamic uwsgi process number
COPY uwsgi.ini /etc/uwsgi/
# Install Supervisord
RUN apk add --no-cache supervisor
# Custom Supervisord config
COPY supervisord-alpine.ini /etc/supervisor.d/supervisord.ini
# uWSGI Python plugin
# As an env var to re-use the config file
ENV UWSGI_PLUGIN python3
# Which uWSGI .ini file should be used, to make it customizable
ENV UWSGI_INI /app/uwsgi.ini
# By default, run 2 processes
ENV UWSGI_CHEAPER 2
# By default, when on demand, run up to 16 processes
ENV UWSGI_PROCESSES 16
# By default, allow unlimited file sizes, modify it to limit the file sizes
# To have a maximum of 1 MB (Nginx's default) change the line to:
# ENV NGINX_MAX_UPLOAD 1m
ENV NGINX_MAX_UPLOAD 0
# By default, Nginx will run a single worker process, setting it to auto
# will create a worker for each CPU core
ENV NGINX_WORKER_PROCESSES 1
# By default, Nginx listens on port 80.
# To modify this, change LISTEN_PORT environment variable.
# (in a Dockerfile or with an option for `docker run`)
ENV LISTEN_PORT 90
# Used by the entrypoint to explicitly add installed Python packages
# and uWSGI Python packages to PYTHONPATH otherwise uWSGI can't import Flask
ENV ALPINEPYTHON python3.8
# Copy start.sh script that will check for a /app/prestart.sh script and run it before starting the app
COPY start.sh /start.sh
RUN chmod +x /start.sh
RUN echo "uwsgi_read_timeout 7200s;" > /etc/nginx/conf.d/custom_timeout.conf
# URL under which static (not modified by Python) files will be requested
# They will be served by Nginx directly, without being handled by uWSGI
ENV STATIC_URL /site
# Absolute path in where the static files wil be
ENV STATIC_PATH /app/static/angular
# If STATIC_INDEX is 1, serve / with /static/index.html directly (or the static URL configured)
# ENV STATIC_INDEX 1
ENV STATIC_INDEX 0
# Add demo app
COPY ./app /app
WORKDIR /app/mean-stack/node-js
RUN echo "$WORKDIR"
RUN pwd
RUN ls -lt
RUN npm install
EXPOSE 8081
# Angular resources
#WORKDIR /app/mean-stack/angular-js
#RUN npm install
#COPY . /app/mean-stack/angular-js
#RUN npm run build --prod
RUN mkdir /app/static/angular
COPY --from=angular-build /app/mean-stack/angular-js/dist/ubot /app/static/angular
# When the container starts, replace the env.js with values from environment variables
#CMD ["/bin/sh", "-c", "envsubst < /app/static/angular/assets/env.template.js > /app/static/angular/assets/env.js && exec nginx -g 'daemon off;'"]
## Remove default nginx index page
#RUN rm -rf /usr/share/nginx/html/*
#
## Copy from the stahg 1
#COPY --from=angular-build /app/mean-stack/angular-js/dist/ubot /usr/share/nginx/html
#EXPOSE 4200 90
#WORKDIR /user/share/n
WORKDIR /app
# Make /app/* available to be imported by Python globally to better support several use cases like Alembic migrations.
ENV PYTHONPATH=/app
ENV FLASK_DEBUG=1
# Move the base entrypoint to reuse it
COPY uwsgi-nginx-entrypoint.sh /uwsgi-nginx-entrypoint.sh
RUN chmod +x /uwsgi-nginx-entrypoint.sh
# Copy the entrypoint that will generate Nginx additional configs
COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh
#Install mongodb
RUN cp /etc/apk/repositories /etc/apk/repositories_bak
RUN echo 'http://dl-cdn.alpinelinux.org/alpine/v3.8/main' > /etc/apk/repositories
RUN echo 'http://dl-cdn.alpinelinux.org/alpine/v3.8/community' >> /etc/apk/repositories
RUN apk update && apk --no-cache --update add mongodb
RUN mongo --version
RUN mkdir -p /data/db
EXPOSE 27017
ENTRYPOINT ["/entrypoint.sh"]
# Run the start script provided by the parent image tiangolo/uwsgi-nginx.
# It will check for an /app/prestart.sh script (e.g. for migrations)
# And then will start Supervisor, which in turn will start Nginx and uWSGI
CMD ["/start.sh"]start.sh:
#! /usr/bin/env sh
set -e
# If there's a prestart.sh script in the /app directory, run it before starting
PRE_START_PATH=/app/prestart.sh
echo "Checking for script in $PRE_START_PATH"
if [ -f $PRE_START_PATH ] ; then
echo "Running script $PRE_START_PATH"
. $PRE_START_PATH
else
echo "There is no script $PRE_START_PATH"
fi
# Start Supervisor, with Nginx and uWSGI
#exec /usr/bin/supervisord
/usr/sbin/nginx &
/usr/sbin/uwsgi --ini /etc/uwsgi/uwsgi.ini &
node mean-stack/node-js/app.js &
mongod &
/bin/ash
tail -f /dev/null
echo "NODE_PORT = ${NODE_PORT}"
envsubst < "/app/static/angular/assets/env.template.js" > "/app/static/angular/assets/env.js"
nginx -g 'daemon off;'在这个start.sh文件中,我设置了NODE_PORT并用env.template.js替换了env.js文件。我还将在下面解释。
assets/env.js
(function(window) {
window["env"] = window["env"] || {};
// Environment variables
window["env"]["nodePort"] = 1900;
console.log('env port', window["env"]["nodePort"]);
})(this);assets/env.template.js
(function(window) {
window.env = window.env || {};
console.log('template', "${NODE_PORT}");
// Environment variables
window["env"]["nodePort"] = "${NODE_PORT}";
})(this);我已经把env.js纳入了index.html。
sudo docker run -d -it -p 8999:90 -p 1905:8081 --env NODE_PORT=1905 --name test-container-ubot ubot-container:lates因此,我需要当上面的命令运行时,我应该在角文件中得到那个NODE_PORT=1905。请帮我解决这个问题。
发布于 2020-11-29 07:52:59
我认为唯一可能使用env的代码是这段代码,它可能会更改$NODE_PORT (如果定义的话)。如果-f $PRE_START_PATH,请尝试对此进行注释;然后回显“正在运行的脚本$PRE_START_PATH”
试着注释这一行并检查
#。$PRE_START_PATH
否则回音“没有脚本$PRE_START_PATH”fi
检查一下。
https://stackoverflow.com/questions/65057928
复制相似问题