Try Docker for yourself by checking out this repository!
- Download Docker
- Download Docker Compose (If not on a Mac)
- Fork this repo into your own GitHub space
- Clone this repo onto your own machine
docker-compose up
Notice how two services are created: db_1
and web_1
. See that migrations run and the server starts.
In a new tab, run:
docker-compose run web --rm ./manage.py createsuperuser
Log into the admin at http://localhost:8000/admin/
and add some content.
Visit http://localhost:8000/spells/
to see a list of your data, and http://localhost:8000/spells/{pk}/
to see a detail view of your data.
Add new features to the Spell
model, or add a new model or app.
Run
docker-compose run --rm web ./manage.py makemigrations
And then run
docker-compose run --rm web ./manage.py migrate
Hop into the shell with
docker-compose run --rm web ./manage.py shell
And experiment with poking around by importing a model and running queries on it.
Update your requirements file with something like requests
or another library you love. Stop Docker, then restart with:
docker-compose up --build
docker images
docker container ls
or
docker ps
It's helpful in development (and pretty necessary in production) to have your data persist across containers and across coding sessions. Read more about volumes in the docs.
To add a data volume to your project, make these changes to docker-compose.yml
:
services:
db:
...
volumes:
- postgres_data:/var/lib/postgresql/data/
web:
...
volumes:
postgres_data:
First add a volume
element under your db
service and name it postgres_data
. Include the path to where your data will be stored in your container.
Then, add another element at the same level as services
called volumes
, and refer to your postgres_data
volumes.
Now you won't need to recreate your superuser and test data every time you shut down your containers.
docker-compose down
- Build your image from scratch and give it a name
- "Exec" into the Docker container with
docker exec --it
- Stop and restart just the
web
container - Add a new service to
docker-compose.yml
; maybe Redis? - Add tests and run them using
docker-compose run
commands
- An Intro to Docker for Djangonauts
- Docker: Useful Command Line Stuff
- Docker tutorial
- Docker Compose tutorial
- Compose and Django tutorial
- Best Practices for Writing Dockerfiles
- 10 Things to Avoid in Docker Containers
- Video: 5 Things About Docker
- Docker CheatSheet
- Dockerizing Django, UWISGI, and Postgres the Serious Way
- Managing Sensitive Data with Docker Secrets
This repo uses the Git Commit Message StyleGuide.