Supercharge your development environment

(using Docker)

Deni Bertovic / @denibertovic

About me

Deni Bertovic

https://denibertovic.com

https://github.com/denibertovic


http://goodcode.io

What is Docker?

What is Docker?

  • "Open platform for developers and sysadmins to build, ship, and run distributed applications. "
  • Light weight containers
  • Available on most linux distros

What's a container?

Traditional VM vs. Docker

Images vs. Containers?

Images vs. Containers

Why should you care?

  • It's fast
  • Minimal overhead/resource usage
  • Run thousands of containers
  • Easy to run your whole production stack locally

Yeah but I have a Mac!

Use Boot2Docker!

Architecture

How do I use it?

What we've learned so far?

  • Container - Light weight virtualization
  • Image - Immutable snapshot of a container
  • Hub/Index- Central hub for sharing images

Docker CLI example


    user@host:/$ docker login
    
user@host:/$ docker pull denibertovic/postgres
user@host:/$ docker run -d -t denibertovic/postgres

How to run containers?

user@host:/$ docker run -i -t debian /bin/bash
user@host:/$ docker run -d -t postgres
user@host:/$ docker run -d -t postgres:9.3

Manipulating Containers

user@host:/$ docker ps

CONTAINER ID  IMAGE      COMMAND   CREATED        STATUS
d03ba4afb47c  debian:7.5 /bin/bash 10 seconds ago Up 9 seconds
user@host:/$ docker logs d03ba4afb47c
user@host:/$ docker stop d03ba4afb47c
user@host:/$ docker kill d03ba4afb47c
user@host:/$ docker start d03ba4afb47c

Images - Manual creation


user@host:/$ docker run -i -t debian /bin/bash
                            

root@container:/$ apt-get install postgresql-9.3
                            
user@host:/$ docker ps
user@host:/$ docker commit <CID> username/postgresql

Hub


user@host:/$ docker login
                        

user@host:/$ docker push username/postgresql
                        

co-worker@workstation2:/$ docker pull username/postgresql
                        

Images - Dockerfiles


FROM      debian:wheezy
ENV DEBIAN_FRONTEND noninteractive

...

RUN apt-get -qq update
RUN apt-get -qq -y install postgresql-9.3 \
    postgresql-client-9.3 postgresql-contrib-9.3
ADD postgresql.conf /etc/postgresql/9.3/main/postgresql.conf
...

CMD /usr/local/bin/start_postgres.sh
                    

Dockerfiles

user@host:/$ docker build -t postgres .

Recap

  • Run containers: docker run
  • Build images manually: docker commit
  • Use Dockerfiles for building images

How does docker fit into your dev environment?

Dev flow


user@host:/$ docker run -d -t postgresql

user@host:/$ psql -Upostgres -h localhost \
    -c"CREATE DATABASE mydb WITH OWNER myuser;"

user@host:/$ python manage.py syncdb
                        

What about ports?


user@host:/$ docker run -d -p 5432:5432 -t postgresql
                    
"-p host_port:container_port"

What about data?


user@host:/$ docker run -d -p 5432:5432 \
    -v `pwd`/data:/var/lib/postgresql \
    -t postgresql
                    
"-v host_dir:container_dir"

Everyone on the team runs the same database, cache, amqp, openssl, C libraries...etc

Running your web app in a container

  • Simplify runtime
  • Make sure everyone runs the same version of deps
  • What about virtualenv? --> C libraries!

Container links


user@host:/$ docker run -d --name pg -t postgresql

user@host:/$ docker run -d -p 8000:8000 \
    --link pg:database -t django_app
                    
"--link pg:database"

root@f9e4e78c60c6:/$ cat /etc/hosts

172.17.0.9  database
                    

                    root@f9e4e78c60c6:/# export
DATABASE_PORT="tcp://172.17.0.9:5432"
DATABASE_PORT_5432_TCP="tcp://172.17.0.9:5432"
DATABASE_PORT_5432_TCP_ADDR="172.17.0.9"
DATABASE_PORT_5432_TCP_PORT="5432"
DATABASE_PORT_5432_TCP_PROTO="tcp"
                    

Fig

https://github.com/docker/fig


web:
  image: django-webapp
  command: python app.py
  links:
   - db
   - redis
   - memcached
  ports:
   - "8000:8000"
db:
  image: denibertovic/postgres
redis:
  image: username/redis
memcached:
  image: otheruser/memcahced

# THEN RUN
fig up
                    

Build once and run anywhere

Thanks!