gpt4 book ai didi

docker 容器在入口点退出

转载 作者:行者123 更新时间:2023-12-01 05:50:22 24 4
gpt4 key购买 nike

我正在尝试使用 docker run 命令通过 Dockerfile 运行 docker 容器。以下是我的 Dockerfile。

FROM python:3.6
# for imaging stuff
RUN apt-get update
RUN apt install libmagickwand-dev
# Create app directory
RUN mkdir -p /home/test/app
# Install Libre Office and ghostscript for pdf conversion and repairing
RUN apt-get update -qq \
&& apt-get install -y -q libreoffice \
&& apt-get remove -q -y libreoffice-gnome \
&& apt-get update \
&& apt-get -y install ghostscript \
&& apt-get -y install nano \
&& apt-get -y install poppler-utils \
&& apt-get install -y nginx
# Cleanup after apt-get commands
RUN apt-get clean \
&& rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* \
/var/cache/apt/archives/*.deb /var/cache/apt/*cache.bin
# Activate WORKING DIR
WORKDIR /home/test/app
# Initiating devnull directory
RUN mkdir -p dev_null
# Copying requirements
COPY requirements/local.txt /tmp/requirements.txt
# Install the app dependencies
RUN pip install -r /tmp/requirements.txt
COPY id_rsa /root/.ssh/id_rsa
COPY requirements/private.txt /tmp/private.txt
RUN ssh-keyscan -T 60 bitbucket.org >> /root/.ssh/known_hosts \
&& chmod 600 /root/.ssh/id_rsa \
&& pip install -r /tmp/private.txt
# Envs
ENV DJANGO_SETTINGS_MODULE app.settings.local
ENV ENVIORNMENT local
# ADD the source code and entry point into the container
ADD . /home/test/app
ADD docker-entrypoint.sh /home/test/app/docker-entrypoint.sh
# Making entry point executable
RUN apt-get update && apt-get install -y supervisor \
&& apt-get install -y nginx
#RUN mkdir -p /var/log/test
#COPY supervisor.conf /etc/supervisor/conf.d/supervisor.conf
RUN chmod +x docker-entrypoint.sh
RUN mkdir -p /var/log/test
COPY supervisor.conf /etc/supervisor/conf.d/supervisor.conf
# Exposing port


# Copy entrypoint script into the image
COPY ./docker-entrypoint.sh /
COPY ./django_nginx.conf /etc/nginx/
RUN chmod +x start.sh

#RUN chmod +x /docker-entrypoint.sh
EXPOSE 8000
ENTRYPOINT ["bash", "/docker-entrypoint.sh"]
CMD ["bash", "/home/test/app/start.sh"]

以下是我的切入点。
#!/usr/bin/env bash
set -e

# ToDo Need to enable this
#until psql $DATABASE_URL -c '\l'; do
# >&2 echo "Postgres is unavailable - sleeping"
# sleep 1
#done
#
#>&2 echo "Postgres is up - continuing"
cd app
mkdir -p app/keys
if [[ ! -e /var/log/gunicorn-access.log ]]; then
touch /var/log/gunicorn-access.log
fi
if [[ ! -e /var/log/gunicorn-error.log ]]; then
touch /var/log/gunicorn-error.log
fi

if [ "x$DJANGO_MANAGEPY_MIGRATE" = 'xon' ]; then
echo "Django starting to migrate un-applied migrations"
python manage.py migrate --noinput
fi

if [ "x$DJANGO_MANAGEPY_COLLECTSTATIC" = 'xon' ]; then
echo "Django starting to collect static data"
python manage.py collectstatic --noinput
fi

if [ "x$DJANGO_LOADDATA" = 'xon' ]; then
python manage.py loaddata taxing/fixtures/province-taxing-table-initial-data.json
fi

if [ "x$LOAD_TEMPLATE_FROM_S3" = 'xtrue' ]; then
echo "loading s3"
python manage.py loadindex

fi


# Start Gunicorn processes
echo Starting Gunicorn.
exec gunicorn test.wsgi:application \
--name test \
--workers 3 \
--log-level=info \
--log-file=/srv/logs/gunicorn.log \
--access-logfile=/srv/logs/access.log &

exec service nginx start

以下是我的 start.sh
#!/bin/bash

echo "I am running"
echo "Belive me"
/usr/bin/supervisord -n

我正在用 docker build -t test . 构建容器它正在 build 中。但是当我尝试使用 docker run --name test --env-file ./env test 运行这个容器时我的容器在 ["bash", "/docker-entrypoint.sh"] 上退出命令 Dockerfile 没有任何错误/消息,但如果我从 Dockerfile 中删除入口点命令,它就可以正常工作。我无法找出错误。任何帮助表示赞赏。

最佳答案

这是我在这个序列中看到的:

ENTRYPOINT ["bash", "/docker-entrypoint.sh"]
CMD ["bash", "/home/test/app/start.sh"]

当你启动容器时,Docker 运行 bash /docker-entrypoint.sh bash /home/test/app/start.sh .然而,入口点脚本根本不查看它的命令行参数,所以任何 CMD您在此处指定或在 docker run 末尾给出的任何命令命令行被完全忽略。

当该入口点脚本运行时:

exec gunicorn ... &
exec service nginx start
# end of file

它开始 gunicorn作为后台进程并继续到下一行;然后它用 service 替换自己命令。那个 service command 成为主容器进程,进程 ID 为 1。它启动 nginx,并立即返回。现在主容器进程已经返回,容器退出。

对于您编写的这段代码,您应该删除 exec在入口点脚本末尾添加几行并将它们替换为

exec "$@"

这将导致 shell 运行其命令行参数(即 Dockerfile CMD)。

但是,有一个现成的 nginx Docker image .通常,如果您需要多个进程,在同一个 Docker 网络上将它们作为两个独立的容器运行会更容易也更好。这避免了尝试在同一个容器中运行多个事物的复杂性。

关于docker 容器在入口点退出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55057650/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com