Cookiecutter-django: Management commands when using docker

Created on 23 May 2018  路  4Comments  路  Source: pydanny/cookiecutter-django

Note: for support questions, please use the cookiecutter-django tag on stackoverflow. This repository's issues are reserved for feature requests and bug reports.

  • *I'm submitting a ... *

    • [ ] bug report
    • [x] feature request
    • [ ] support request => Please do not submit support request here, see note at the top of this template.
  • Do you want to request a feature or report a bug?

Feature

  • What is the current behavior?

As far as I know, using this project template with docker enabled makes it quite annoying to execute management commands from the command line (perhaps I am doing it wrong). What I saw me doing instead of ./manage.py <whatever> was docker-compose -f local.yml run django python manage.py <whatever>

  • What is the expected behavior?

Independent of shell completion, aliases etc., I would like to be able to just do single command management actions when using docker, so I created a really simple bash script called manage.sh which is located in my project root next to the manage.py file with just the following content:

#!/bin/bash

docker-compose -f local.yml run django python manage.py $@
  • What is the motivation / use case for changing the behavior?

Now you can call ./manage.sh makemigrations inside your docker-compose setup. Is that something that would be benefical to others? If yes, I could have a look at how to integrate a file like this depending on the project setup.

  • Please tell us about your environment:

Python3.6, Docker

Most helpful comment

Not sure if we need that extra layer of abstraction...

All 4 comments

Some of us prefer to use the command-line, so I like the original submission

Not sure if we need that extra layer of abstraction...

The migration step happens every time you bring up docker. If you really want shell access, just run docker exec -it <Name of the running container> sh and you have a shell into your running docker container. You can do all the things you want from the command line. No need for a script,python manage.py dumpdata -a or whatever you want to do is possible again.
For example: docker exec -it polls_django_1 sh will give you the shell. Logout when you're done.

Docker has many tools, I too think getting a shell like this is a far better option over adding another layer of abstraction like @webyneter said.


Update:

Maybe I wasn't clear in my explanation?
@saschalalala @webyneter I believe this issue should be closed, because using docker in the manner described above: docker-compose -f local.yml run django python manage.py makemigrations or running a one line shell script to reduce the length of a command; is not the preferred method for working with docker. I understand where the issue comes from, because most documentation show examples using the docker run command. I personally only ever use it to troubleshoot a container that fails to build on a specific build step.

You need to have two things running simultaneously, you have 3 options. This is assuming your project is named polls, because of the first django tutorial on their website. If you have your container running (first thing) you can get a shell in your container (second thing).

  1. Run your command docker-compose -f local.yml up, create a new tab, run this command in the new tab docker exec -it polls_django_1 sh.

This gives you a shell inside the running container. I added the hello world to give you the look and feel of the command line, to emphasize the difference between the two shells.

$ echo "Hello World!"
Hello World

$ docker exec -it polls_django_1 sh

# /app echo "Hello World!"
Hello World!

# /app python manage.py makemigrations
  1. Add the -d flag and run the commands in the same window. CookieCutter Django is built on Alpine, which doesn't have bash natively. Use sh instead, or download bash with apk add bash
docker-compose -f local.yml up -d
docker exec -it polls_django_1 sh

or alternatively, if you install bash...

docker-compose -f local.yml up -d
docker exec -it polls_django_1 bash
  1. Put the running container in the background and keep working.
    docker-compose -f local.yml up press the hotkey ctrl + z, once the process is stopped, type bg for background. Run the same command to get a shell. docker exec -it polls_django_1 sh.

@global2alex Yeah, I understand what you are saying. Nevertheless, I quite often do things in a running docker container during development, even migrations etc. and I prefer doing so from my local shell, e.g. for command history purposes. But I understand why this seems to be a not so common use case so I'll stick with adding this file in my projects ;)

And adding it with cli option seems to be quite overkill and just an unnecessary question more to ask during setup. Thanks for your feedback everyone, I'll close this one then.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

grll picture grll  路  3Comments

adammsteele picture adammsteele  路  3Comments

webyneter picture webyneter  路  3Comments

pygabo picture pygabo  路  3Comments

japrogramer picture japrogramer  路  4Comments