我有一个简单的Docker
映像,其中包含一些python测试用例。
这是我的Dockerfile
ARG PYTHON_VERSION=3.8.3-alpine
# Use the python image as the base image
FROM python:${PYTHON_VERSION}
# upgrade pip
RUN pip install --upgrade pip
# set CUSTOMER_NAME as environment variables
ENV CUSTOMER_NAME=${CUSTOMER_NAME:-A}
# Set the working directory
WORKDIR /app
# Create a non-root user and add the permissions
RUN adduser -D "${USERNAME:-jananath}"
# set the username - HERE WE ARE USING A DEFAULT VALUE FOR THE USERNAME, WHICH IS "jananath"
USER "${USERNAME:-jananath}"
# copy the requirements file and install the dependencies
COPY --chown="${USERNAME:-jananath}":"${USERNAME:-jananath}" requirements.txt requirements.txt
RUN pip install --no-cache-dir --upgrade --user -r requirements.txt
ENV PATH="/home/${USERNAME:-jananath}/.local/bin:${PATH}"
# copy the app code
COPY --chown=${USERNAME:-jananath}:${USERNAME:-jananath} . .
# expose the default Flask port
EXPOSE 80
# set the entrypoint to run the app
CMD ["uvicorn", "main:app", "--proxy-headers", "--host", "0.0.0.0", "--port", "80"]
然后我构建图像:
docker build -t hello:v1 .
然后我运行应用程序
docker run --rm -itd -p 80:80 -e CUSTOMER_NAME=A hello:v1
然后执行pytest
docker exec -it 570e3153ab90 pytest
它成功地给出了如下pytest
输出:
================================================= test session starts =================================================
platform linux -- Python 3.8.3, pytest-7.2.0, pluggy-1.0.0
rootdir: /app, configfile: pytest.ini
plugins: anyio-3.6.2
collected 2 items
test_main.py .. [100%]
================================================== 2 passed in 0.24s ==================================================
一切正常,除了我在GitHub操作中运行了相同的图像。
. . .
container-test-job:
runs-on: ubuntu-latest
container:
image: ghcr.io/<USERNAME>/<REPO>/hello:v2
credentials:
username: ${{ github.actor }}
password: ${{ secrets.TOKEN_REPOSITORY }}
env:
CUSTOMER_NAME: A
steps:
- name: Test
shell: python
run: |
pytest
. . .
但我得到以下错误:
追溯(最近调用最后调用):文件"/_w/ temp/www.example.com ",第1行,pytest名称错误:598eea6e-7acf-4d7c-964f-a69440ea38c2.py进程已完成,退出代码为1。 name 'pytest' is not defined Error: Process completed with exit code 1.
有人能帮助我了解这里的问题以及如何解决它吗?
谢谢大家!
1条答案
按热度按时间uinbv5nw1#
试试这段代码,然后你就可以建立镜像并把它推到你的注册表中。