Init django app with docker-compose

I am new to docker-compose and I have docker with my django instance and mysql database. I would like to create my own autoconfigured container that runs the command only when the docker is first launched. In this command, I would like to perform the following tasks:

  • perform initial database migrations
  • create superuser admin
  • import mysql backup to database

After that, the system should continue to run the django test web server.

Is there a way to tell the docker-compose command to run the command only when it first starts up, or is there any alternative in django to control it if the system is already configured and updated?

To clarify here, my dock file and docker-compose.yml:

FROM python:3.4
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
ADD requirements.txt /code/
RUN pip install -r requirements.txt
ADD . /code/

####################

version: '2'
services:
db:
   image: "mysql:5.6"
   ports:
     - 3306:3306
   environment:
     MYSQL_ROOT_PASSWORD: xxxxxx
     MYSQL_DATABASE: xxxxxx
     MYSQL_USER: xxxxx
     MYSQL_PASSWORD: xxxxxxx
 web:
   build: .
   command: python manage.py runserver 0.0.0.0:8000
   volumes:
     - .:/code
   ports:
     - "8000:8000"
   depends_on:
     - db

Thank.

+4
1

@cricket_007, , , . sh script -. , , web_local.version web_server.version.

Web_local.version .gitignore, .

Start_web.sh script - script, , web_local.version. , script , ​​ . , , -, , , -.

web_start.sh script :

#!/bin/bash

FILE="web_local.version"

if [ -f "$FILE" ];
then
   echo "File $FILE exist."
   if diff ./web_server.version ./web_local.version > /dev/null;
   then
      echo "model version up to date :)"
   else
      echo "model updated!!"
      python manage.py migrate
      cp ./web_server.version ./$FILE
  fi
else
  echo "File $FILE does not exist"
  sleep 10 #added because the first time db take a long time to init and the script doesn't wait until db is finished
  cp ./web_server.version ./$FILE
  python manage.py migrate
fi
python manage.py runserver 0.0.0.0:8000

, , , , git, , .

+5

All Articles