Skip to content

realpython/django-docker-tests

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 

Repository files navigation

Testing with Docker

Let's look at a nice workflow for testing with Docker.

Tools we'll be using:

  1. Python/Django
  2. Docker
  3. Fig
  4. Github
  5. Jenkins
  6. Docker Hub

Docker Explained

"Docker is an open-source engine that automates the deployment of any application as a lightweight, portable, self-sufficient container that will run virtually anywhere."

Docker provides the isolation of a virtual machine, wrapping the filesystem, processes, evironment variables, etc. into a nice container - but without all the overhead since it uses the host machine's kernal. Your app, processes, and code run in containers, while images save the state of the containers for easy recreation on other machines. Using the popular .git analagy, images are akin to repositores while containers are akin to a local clone.

Why use Docker?

  1. Containers are isolated like a true VM. Forget about messing with virtualenv.
  2. Docker images are shareable and handle version control at the system-level. Easily distrubute your working environment amongst your enture team.

Finally, you can truly mimic your production environment at the local level, without any perfromance loss.

You can add Docker into your development workflow to test your deployments locally in an isolated, lightweight VM-like environment, all without touching virtualenv. Pretty cool.

Fig Explained

Fig automates the (re)creation of a Docker environment.

Quick Start

Setup Docker for Mac

  1. Follow the instructions here to install Docker and Fig.

  2. Sanity check

    $ docker-osx shell
    $ fig --version
    fig 0.5.2
    
  3. docker-osx commands: run docker-osx to view the available commands

Create a Dockerfile

A Dockerfile is a configuration file that automates the creation of your container from an image.

# start with a base image
FROM ubuntu:13.10
MAINTAINER Real Python <info@realpython.com>

# install dependencies
RUN apt-get -qq update
RUN apt-get install -y python python-pip

# grab contents of source directory
ADD ./src /src/

# specify working directory
WORKDIR /src

# build app
RUN pip install -r requirements.txt
RUN python manage.py syncdb --noinput

# expose port 8000 for us to use
EXPOSE 8000

CMD python manage.py runserver 0.0.0.0:8000

Django Setup

Clone the repo to get started quickly. Alternatively, use your own Django Project. Just be sure to re-organize the project to match mine and add the fig.yml file. If you don't have any tests, be sure to add a few.

Signup for Docker Hub

Already have an account? Skip this section.

  1. Signup here.
  2. Enter the docker-osx shell: docker-osx shell
  3. Login: docker login

Create your Image

  1. Build it:
$ docker build -t web .
  1. Tag it:
docker tag web <your_docker_login>/web
  1. Push it:
$ docker push <your_docker_login>/web

Deployment Workflow

  1. Code locally
  2. PUSH to Github
  3. Jenkins runs tests
  4. If tests fail, process ends
  5. If tests pass:
    1. Docker Hub builds new image
    2. Jenkis Deploys to Heroku

Code Locally

In the normal flow, you would ideally be a point where you're ready to deploy code to production. Perhaps you fixed a major bug or implemented a new feature. Create a commit and PUSH

Resources

https://github.com/wsargent/docker-cheat-sheet

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published