061 Docker

22 Aug 2014

Docker is a platform for distributed apps. We will learn how to create isolated environment for apps based on docker. We will create 2 simple apps with nginx and nodejs with various docker commands and see how we can replicate that exact environment.

Download video: mp4

Sample code: Github

Version: Client version 1.1.2, API version 1.13

Similar episodes: 025 VirtualBox, 039 Vagrant

##Background on Docker

  1. Main website
  2. Get yourself familiar with common commands
  3. docs
  4. Vagrant or Docker ?
  5. Virtual machines vs Docker image search
  6. LXC
  7. common commands

##Things to learn with Docker

###1. install on mac osx

  1. install instructions
  2. get some info on docker from the command line

    $ docker $ docker version $ echo $DOCKER_HOST

  3. start docker

    $ boot2docker start $ export DOCKER_HOST=tcp://$(boot2docker ip 2>/dev/null):2375 $ echo $DOCKER_HOST

  4. check running containers and images

    $ docker ps $ docker ps -a $ docker images

  5. delete all containers and images

    docker rm $(docker ps -a -q) docker rmi $(docker images -q)

###2. make a static html page with nginx

  1. in an empty folder create index.html


    Hello world with Docker!


  2. create Dockerfile

    ``` FROM ubuntu:14.04

    RUN apt-get update ```

  3. build from Dockerfile with options - don’t forget the ending dot to mean current folder

    $ docker build -t html .

  4. enter the docker image

    $ docker ps -a $ docker images $ docker run -i -t html /bin/bash

  5. once you enter, check which Linux version is installed and commonly used programs like git, nano and curl

    ``` $ lsb_release -a

    No LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 14.04 LTS Release: 14.04

    $ git –version $ nano $ curl ```

  6. install git, nano and curl by appending to file Dockerfile

    ... RUN apt-get install -y git curl nano

  7. try out whether it has installed everything!

    ``` $ docker build -t html . $ docker run -i -t html /bin/bash

    /# git –version /# curl build-podcast.com /# nano version ```

  8. add on the installation of nginx and a directory with the entire project. Amend the Dockerfile

    ... RUN apt-get install -y git curl nano nginx RUN mkdir /apps ADD . /apps/html

  9. try out that the app folder in there as well as nginx

    ``` $ docker build -t html . $ docker run -i -t html /bin/bash

    /# cd /apps/html /# cat index.html /# cat /etc/nginx/nginx.conf ```

  10. access url $DOCKER_HOST:8888 in the browser e.g. - Webpage is not available
  11. create nginx config file nginx.conf

    ``` user www-data; worker_processes 4; pid /run/nginx.pid;

    events { worker_connections 768; }

    http { sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 2048; include /etc/nginx/mime.types; default_type application/octet-stream; access_log /var/log/nginx/access.log; error_log /var/log/nginx/error.log; gzip on; gzip_disable “msie6”; include /etc/nginx/conf.d/.conf; include /etc/nginx/sites-enabled/;

    server { listen 8888; server_name localhost; charset utf-8; add_header “X-UA-Compatible” “IE=Edge,chrome=1”; index index.html; location / { expires -1; add_header Pragma “no-cache”; add_header Cache-Control “no-store, no-cache, must-revalidate, post-check=0, pre-check=0”; root /apps/html; try_files $uri $uri/ /index.html =404; } } }

    daemon off; ```

  12. add to Dockerfile to copy the nginx.conf file to the correct location, expose the port number to host and start nginx

    ``` … RUN mv /etc/nginx/nginx.conf /etc/nginx/nginx.conf.original RUN mv /apps/html/nginx.conf /etc/nginx/nginx.conf

    WORKDIR /apps/html

    EXPOSE 8888 CMD [“nginx”]


  13. build the docker image and try out in the browser!

    $ docker build -t html .

  14. start the server by running the command

    $ docker images $ docker ps -a $ docker ps $ docker run -d -p 8888:8888 html

  15. visit the browser url $DOCKER_HOST:8888 e.g.

  16. initiate a git repo

    $ git init $ git add . $ git commit -m "initial commit"

  17. build the image and access the command line. .git folder is available even though it is not used in running the add.

    ``` $ docker build -t html . $ docker run -i -t html /bin/bash

    /# ls -al ```

  18. create .dockerignore


  19. build the image again and access the command line to find there’s no more .git folder

    ``` $ docker build -t html . $ docker run -i -t html /bin/bash

    /# ls -al ```

  20. add a bash profile by editing Dockerfile

    ... RUN curl https://raw.githubusercontent.com/sayanee/dotfiles/master/.bashrc > /.bashrc ...

###2. make another simple node app

  1. in another empty folder
  2. in a file app.js

    var http = require('http'); http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World\n'); }).listen(8000); console.log('Server running at port 8000/');

  3. create the Dockerfile

    ``` FROM ubuntu:14.04

    RUN apt-get update RUN apt-get install -y git curl nano nodejs nodejs-dev npm RUN curl https://raw.githubusercontent.com/sayanee/dotfiles/master/.bashrc > /.bashrc RUN ln -s /usr/bin/nodejs /usr/bin/node

    RUN mkdir /apps ADD . /apps/nodeapp

    WORKDIR /apps/nodeapp

    EXPOSE 8000 CMD [“node”, “/apps/nodeapp/app.js”] ```

  4. build the image

    $ docker build -t nodeapp . $ docker run -i -t nodeapp /bin/bash

  5. check the browser with port 8000
  6. docker stop container name

    $ docker ps $ docker stop <container_name>

  7. check the browser with port 8000 - website not avaiable!
  8. debug with the port mapping

    ``` $ docker run -i -p 8000:8000 -t nodeapp /bin/bash

    /#: node app.js & ```

  9. check the browser with port 8000

##More Resources on Docker 1. Docker run videos 1. Docker hub

##Build Link of this episode

Free programming books