Docker-Compose +命令 [英] Docker-Compose + Command
问题描述
看来我的docker-compose命令只执行最后一个命令.在这种情况下,运行服务器.
It seems my docker-compose commands only execute the last command. In this case runserver.
command: python3 manage.py collectstatic --noinput
command: python3 manage.py migrate --noinput
command: python3 manage.py runserver 0.0.0.0:8000
我试图将这些命令移到entrypoint.sh文件中.但是,我不知道如何在我的dockerfile&中实现它.docker-compose.
I tried to move these commands into an entrypoint.sh file. However, I can't figure out how to implement this into my dockerfile & docker-compose.
以下是我的dockerfile:
The following is my dockerfile:
# Pull base image
FROM python:3
# Set environment varibles
ENV PYTHONUNBUFFERED 1
# Set work directory
RUN mkdir /code
WORKDIR /code
# Install dependencies
RUN pip install --upgrade pip
RUN pip install pipenv
COPY ./Pipfile /code/Pipfile
RUN pipenv install --deploy --system --skip-lock --dev
# Copy project
COPY . /code/
我的docker-compose:
My docker-compose:
version: '3'
services:
db:
image: postgres
ports:
- "5432:5432"
web:
build: .
command: python3 manage.py collectstatic --noinput
command: python3 manage.py migrate --noinput
command: python3 manage.py runserver 0.0.0.0:8000
env_file: .env
volumes:
- .:/code
ports:
- "8000:8000"
depends_on:
- db
entrypoint.sh
entrypoint.sh
#!/bin/bash
# Collect static files
echo "Collect static files"
python manage.py collectstatic --noinput
# Apply database migrations
echo "Apply database migrations"
python manage.py migrate
# Start server
echo "Starting server"
python manage.py runserver 0.0.0.0:8000
推荐答案
这是因为您只能有一个 command
.
That is because you can only have one command
.
您可以像这样组合多个命令:
You can combine multiple commands like this:
command: sh -c "python3 manage.py collectstatic --noinput && python3 manage.py migrate --noinput && python3 manage.py runserver 0.0.0.0:8000"
或者,正如您提到的入口点一样,您可以在 Dockerfile 或在 docker-compose.yml 中.请确保删除命令,因为您将不再需要它们.
Alternatively as you mentioned the entrypoint, you can specify the entrypoint in the Dockerfile or in docker-compose.yml. Make sure to remove the commands as you won't need them anymore.
关于入口点与cmd与运行,这是一篇不错的文章: http://goinbigdata.com/docker-run-vs-cmd-vs-entrypoint/
Here's a good article on entrypoint vs cmd vs run: http://goinbigdata.com/docker-run-vs-cmd-vs-entrypoint/
这篇关于Docker-Compose +命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!