testdriven.io django tdd - got Error: '$' is not a valid port number when running docker run
testdriven.io django tdd - got Error: '$' is not a valid port number when running docker run
每次我尝试运行命令
docker run --name django-tdd -e "PORT=8765" -p 8008:8765 registry.heroku.com/lit-sierra-68791/web:latest
我收到 错误:“$”不是有效的端口号
Dockerfile.prod
# pull official base image
FROM python:3.9.5-slim-buster
# set working directory
WORKDIR /usr/src/app
# set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
ENV DEBUG 0
ENV SECRET_KEY fgerg345y4y56u5u5757jk5k56kuykykyk
ENV DJANGO_ALLOWED_HOSTS localhost 127.0.0.1 [::1]
ENV PORT 8765
# install system dependencies
RUN apt-get update \
&& apt-get -y install gcc postgresql \
&& apt-get clean
# add and install requirements
RUN pip install --upgrade pip
COPY ./requirements.txt .
RUN pip install -r requirements.txt
# add app
COPY . .
# add and run as non-root user
RUN adduser --disabled-password myuser
USER myuser
# run gunicorn
CMD gunicorn drf_project.wsgi:application --bind 0.0.0.0:$PORT
我什至在上面的文件中添加了 ENV PORT 8765,但它也没有用。
不能在CMD中引用ENV变量。
您可以通过创建读取变量并将其传递给 CMD 的脚本来解决此问题。
创建文件bin/start.sh
#!/bin/sh
gunicorn drf_project.wsgi:application --bind 0.0.0.0:$PORT
更新Dockerfile
...
CMD ["bin/start.sh"]
每次我尝试运行命令
docker run --name django-tdd -e "PORT=8765" -p 8008:8765 registry.heroku.com/lit-sierra-68791/web:latest
我收到 错误:“$”不是有效的端口号
Dockerfile.prod
# pull official base image
FROM python:3.9.5-slim-buster
# set working directory
WORKDIR /usr/src/app
# set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
ENV DEBUG 0
ENV SECRET_KEY fgerg345y4y56u5u5757jk5k56kuykykyk
ENV DJANGO_ALLOWED_HOSTS localhost 127.0.0.1 [::1]
ENV PORT 8765
# install system dependencies
RUN apt-get update \
&& apt-get -y install gcc postgresql \
&& apt-get clean
# add and install requirements
RUN pip install --upgrade pip
COPY ./requirements.txt .
RUN pip install -r requirements.txt
# add app
COPY . .
# add and run as non-root user
RUN adduser --disabled-password myuser
USER myuser
# run gunicorn
CMD gunicorn drf_project.wsgi:application --bind 0.0.0.0:$PORT
我什至在上面的文件中添加了 ENV PORT 8765,但它也没有用。
不能在CMD中引用ENV变量。
您可以通过创建读取变量并将其传递给 CMD 的脚本来解决此问题。
创建文件bin/start.sh
#!/bin/sh
gunicorn drf_project.wsgi:application --bind 0.0.0.0:$PORT
更新Dockerfile
...
CMD ["bin/start.sh"]