[英]Why is my docker image not running when using docker run (image), but i can run containers generated by docker-compose up?
My docker-compose creates 3 containers - django, celery and rabbitmq. When i run the following commands -> docker-compose build and docker-compose up, the containers run successfully.我的 docker-compose 创建了 3 个容器 - django、celery 和 rabbitmq。当我运行以下命令时 -> docker-compose build 和 docker-compose up,容器成功运行。
However I am having issues with deploying the image.但是我在部署图像时遇到问题。 The image generated has an image ID of 24d7638e2aff.生成的图像的图像 ID 为 24d7638e2aff。 For whatever reason however, if I just run the command below, nothing happens with an exit 0. Both the django and celery applications have the same image id.但是,无论出于何种原因,如果我只运行下面的命令,出口 0 不会发生任何事情。django 和 celery 应用程序都具有相同的图像 ID。
docker run 24d7638e2aff
This is not good, as I am unable to deploy this image on kube.netes.这不好,因为我无法在 kube.netes 上部署此映像。 My only thought is that the dockerfile has been configured wrongly, but i cannot figure out what is the cause我唯一的想法是 dockerfile 配置错误,但我无法弄清楚是什么原因
docker-compose.yaml docker-compose.yaml
version: "3.9"
services:
django:
container_name: testapp_django
build:
context: .
args:
build_env: production
ports:
- "8000:8000"
command: >
sh -c "python manage.py migrate &&
python manage.py runserver 0.0.0.0:8000"
volumes:
- .:/code
links:
- rabbitmq
- celery
rabbitmq:
container_name: testapp_rabbitmq
restart: always
image: rabbitmq:3.10-management
ports:
- "5672:5672" # specifies port of queue
- "15672:15672" # specifies port of management plugin
celery:
container_name: testapp_celery
restart: always
build:
context: .
args:
build_env: production
command: celery -A testapp worker -l INFO -c 4
depends_on:
- rabbitmq
Dockerfile Dockerfile
ARG PYTHON_VERSION=3.9-slim-buster
# define an alias for the specfic python version used in this file.
FROM python:${PYTHON_VERSION} as python
# Python build stage
FROM python as python-build-stage
ARG build_env
# Install apt packages
RUN apt-get update && apt-get install --no-install-recommends -y \
# dependencies for building Python packages
build-essential \
# psycopg2 dependencies
libpq-dev
# Requirements are installed here to ensure they will be cached.
COPY ./requirements .
# Create Python Dependency and Sub-Dependency Wheels.
RUN pip wheel --wheel-dir /usr/src/app/wheels \
-r ${build_env}.txt
# Python 'run' stage
FROM python as python-run-stage
ARG build_env
ARG APP_HOME=/app
ENV PYTHONUNBUFFERED 1
ENV PYTHONDONTWRITEBYTECODE 1
ENV BUILD_ENV ${build_env}
WORKDIR ${APP_HOME}
RUN addgroup --system appuser \
&& adduser --system --ingroup appuser appuser
# Install required system dependencies
RUN apt-get update && apt-get install --no-install-recommends -y \
# psycopg2 dependencies
libpq-dev \
# Translations dependencies
gettext \
# git for GitPython commands
git-all \
# cleaning up unused files
&& apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false \
&& rm -rf /var/lib/apt/lists/*
# All absolute dir copies ignore workdir instruction. All relative dir copies are wrt to the workdir instruction
# copy python dependency wheels from python-build-stage
COPY --from=python-build-stage /usr/src/app/wheels /wheels/
# use wheels to install python dependencies
RUN pip install --no-cache-dir --no-index --find-links=/wheels/ /wheels/* \
&& rm -rf /wheels/
COPY --chown=appuser:appuser ./docker_scripts/entrypoint /entrypoint
RUN sed -i 's/\r$//g' /entrypoint
RUN chmod +x /entrypoint
# copy application code to WORKDIR
COPY --chown=appuser:appuser . ${APP_HOME}
# make appuser owner of the WORKDIR directory as well.
RUN chown appuser:appuser ${APP_HOME}
USER appuser
EXPOSE 8000
ENTRYPOINT ["/entrypoint"]
entrypoint入口点
#!/bin/bash
set -o errexit
set -o pipefail
set -o nounset
exec "$@"
How do I build images of these containers so that I can deploy them to k8s?我如何构建这些容器的镜像以便我可以将它们部署到 k8s?
The Compose command:
overrides the Dockerfile CMD
. Compose command:
覆盖 Dockerfile CMD
。 docker run
doesn't look at the docker-compose.yml
file at all, and docker run
with no particular command runs the image's CMD
. docker run
根本不查看docker-compose.yml
文件,而docker run
没有特定命令运行图像的CMD
。 You haven't declared anything for that, which is why the container exits immediately.您还没有为此声明任何内容,这就是容器立即退出的原因。
Leave the entrypoint script unchanged (or even delete it entirely, since it doesn't really do anything).保持入口点脚本不变(或者甚至完全删除它,因为它实际上并没有做任何事情)。 Add a CMD
line to the Dockerfile在Dockerfile中增加一条CMD
线路
CMD python manage.py migrate && python manage.py runserver 0.0.0.0:8000
Now plain docker run
as you've shown it will attempt to start the Django server.现在docker run
,如您所示,它将尝试启动 Django 服务器。 For the Celery container, you can still pass a command override对于 Celery 容器,你仍然可以通过命令覆盖
docker run -d --net ... your-image \
celery -A testapp worker -l INFO -c 4
If you do deploy to Kube.netes, and you keep the entrypoint script, then you need to use args:
in your pod spec to provide the alternate command, not command:
.如果您确实部署到 Kube.netes,并且保留了入口点脚本,那么您需要在 pod 规范中使用args:
来提供备用命令,而不是command:
。
I think that is because the commands to run the django server are in the docker-compose.yml
.我认为这是因为运行 django 服务器的命令在docker-compose.yml
中。
You should move these commands inside the entrypoint.您应该将这些命令移到入口点内。
set -o errexit
set -o pipefail
set -o nounset
python manage.py migrate && python manage.py runserver 0.0.0.0:8000
exec "$@"
Pay attention that this command python manage.py runserver 0.0.0.0:8000
will start the application with a development server that cannot be used for production purposes.请注意,此命令python manage.py runserver 0.0.0.0:8000
将使用不能用于生产目的的开发服务器启动应用程序。
You should look for gunicorn or similar.你应该寻找gunicorn或类似的东西。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.