简体   繁体   中英

Starting Django's runserver in a Docker container through docker-compose

I would like to have Django's runserver command running when I call docker-compose up

Here is what I tried, firstly, my image is starting from a Python image customized following this dockerfile :

# Dockerfile

FROM python:3.8
MAINTAINER geoffroy

# Set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

# Ports exposure
EXPOSE 8000
VOLUME /data

# Install dependancies
RUN apt-get update && apt-get install -y \
    vim \
    git \
    && rm -rf /var/lib/apt/lists/*

# Setup python dependancies
RUN git clone https://github.com/blondelg/auto.git
WORKDIR /auto
RUN cd /auto
RUN pip install --no-cache-dir -r requirements.txt

# Build the secret key generator
RUN echo "import random" > generate_key.py
RUN echo "print(''.join(random.SystemRandom().choice('abcdefghijklmnopqrstuvwxyz0123456789!@$^&*(-_=+)') for i in range(50)))" >> generate_key.py

# Setup environment configuration
RUN cp auto/config_sample.ini auto/config.ini
RUN sed -i "s/SECRET_KEY_PATTERN/$(python generate_key.py)/gI" auto/config.ini
RUN sed -i "s/django.db.backends.sqlite3/django.db.backends.mysql/gI" auto/config.ini
RUN sed -i 's|{BASE_DIR}/db.sqlite3|autodb|gI' auto/config.ini
RUN sed -i "s/USER_PATTERN/root/gI" auto/config.ini
RUN sed -i "s/PASSWORD_PATTERN/root/gI" auto/config.ini
RUN sed -i "s/HOST_PATTERN/database/gI" auto/config.ini
RUN sed -i "s/PORT_PATTERN/3306/gI" auto/config.ini

Then, I have my docker-compose.yml designed as follow:

# docker-compose.yml
version: "3"
services:
    database:
        image: mariadb
        container_name: database
        ports: 
            - "3306:3306"
        environment:
            - MYSQL_ROOT_PASSWORD=root
            - MYSQL_DATABASE=autodb
        hostname: 'database'
    runserver:
        build: .
        command: python /auto/manage.py runserver 0.0.0.0:8000 
        container_name: runserver
        ports:
            - "8000:8000"
        depends_on:
            - database

Then I run

docker-compose up --build -d

My two containers (runserver + database) are up but going to http://127.0.0.1:8000 returns an error page whereas I should have the Django start page .

Also, when I go into the container ( docker exec -ti runserver bash ) and when I run python manage.py runserver 0.0.0.0:8000 , I can access to the Django start page through http://127.0.0.1:8000 .

What could be wrong here?

In your Dockerfile, there is this line WORKDIR /auto . It means that you are already in the /auto folder. So, in your docker-compose file, you should say

python manage.py runserver 0.0.0.0:8000 

Instead of /auto/manage.py .

Following this documentation https://docs.docker.com/compose/django/ , Ifixed the way I was declaring volume in my docker-compose.yaml

        volumes:
            - .:/auto

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM