An Introduction to Docker using Examples
Gerard Keating
Docker
Docker is an open-source project that automates the deployment of Linux applications inside software containers.
-Wikipedia
Docker is an open-source project that automates the deployment of Linux applications inside software containers.
Software containers are Operating-system-level virtualization which is a computer virtualization method in which the kernel of an operating system allows the existence of multiple isolated user-space instances.
This presentation:
- Where I use docker?
- A Simple example
- Docker Compose
- An Example Project
- Tips
- To Research Further
Where I use docker?
- CCPE's graphite system and grafana board
- Experimenting with new technologies, currently looking into Sonar Qube
- Rapid prototyping new systems like "An Example Project"
- Learning a new programming language
- This presentation (before being moved to slides.com)
A Simple example: Overview
A Simple example
A Python Flask "Hello World!" application
app.py
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return "Docker presentation"
if __name__ == "__main__":
app.run('0.0.0.0', 80, debug=True)
Folder Structure
├── Dockerfile └── src └── app.py
A Simple example: Dockerfile
FROM python:2.7
MAINTAINER Gerard Keating <gerard_keating@symantec.com>
COPY src/ /app
WORKDIR /app
RUN pip install flask
CMD python app.py
docker build -t docker_example1:latest .
docker images
$ docker build -t docker_example1:latest .
Sending build context to Docker daemon 4.608 kB
Step 1 : FROM python:2.7
---> 6b494b5f019c
Step 2 : MAINTAINER Gerard Keating <gerard_keating@symantec.com>
---> Using cache
---> 8c96ad4ecd7c
Step 3 : COPY src/ /app
---> Using cache
---> 1068fdb21dd8
Step 4 : WORKDIR /app
---> Using cache
---> c482c92f1d29
Step 5 : RUN pip install flask
---> Using cache
---> 8014d50a37a4
Step 6 : CMD python app.py
---> Using cache
---> 44b55fc47d09
Successfully built 44b55fc47d09
$ docker images
REPOSITORY IMAGE ID CREATED SIZE TAG
docker_example1 44b55fc47d09 About an hour ago 682.4 MB latest
A Simple example: running the instance
docker run -p 3000:80 --name docker_example1_inst docker_example1
docker ps
Docker Compose
Compose is a tool for defining and running multi-container Docker applications
Basically a wrapper for docker
Note: Not recommended for production nor for managing 10+ containers
Also useful for running just one container
- Deals with names of containers
- Deals with deleting, keeping and building containers and images
- Don't need to remember all the docker arguments or write a bash script
Docker Compose
For docker compose to work you need a yaml(docker-compose.yml) file
Here's an example
version: '2'
services:
example1:
build: .
ports:
- 3000:80
Then similiar to before
docker-compose build
docker-compose up
An Example Project: Requirements
- Get information from a sql server
- Run something on that data and store data in mongo db for later analysis
New Compose File
version: '2'
services:
db:
image: mongo
volumes:
- ./mongodb_data:/data/db
ports:
- "27017:27017"
monitor:
build: .
volumes:
- ./src:/app
env_file:
- secure.env
Dockerfile
FROM ubuntu:xenial
# Update the sources list
RUN apt-get update
# Install Python and Basic Python Tools
RUN apt-get install -y python python-dev python-distribute python-pip
RUN pip install --upgrade pip
# mssql connect stuff
RUN apt-get install -y freetds-bin freetds-common freetds-dev unixodbc unixodbc-dev tdsodbc
#
RUN apt-get install -y libcurl3
ADD requirements.txt /
RUN pip install -r /requirements.txt
ADD odbcinst.ini /etc/odbcinst.ini
# Copy the application folder inside the container
ADD src /app
WORKDIR "/app"
CMD python monitor.py
docker creates it's own network with dns so to connect to the mongo db from python is just
client = pymongo.MongoClient("db")
$ docker-compose build
db uses an image, skipping
Building monitor
Step 1/12 : FROM ubuntu:xenial
---> 0ef2e08ed3fa
Step 2/12 : RUN apt-get update
---> Using cache
---> 8d6511968ecd
..........
$ docker-compose up
Creating sixsmonitor_db_1
Creating sixsmonitor_monitor_1
Attaching to sixsmonitor_db_1, sixsmonitor_monitor_1
db_1 | 2017-05-31T09:24:59.086+0000 I CONTROL [initandlisten] MongoDB starting : pid=1 port=27017 dbpath=/data/db 64-bit host=d191ae6b54cf
db_1 | 2017-05-31T09:24:59.086+0000 I CONTROL [initandlisten] db version v3.4.3
db_1 | 2017-05-31T09:24:59.086+0000 I CONTROL [initandlisten] git version: f07437fb5a6cca07c10bafa78365456eb1d6d5e1
db_1 | 2017-05-31T09:24:59.086+0000 I CONTROL [initandlisten] OpenSSL version: OpenSSL 1.0.1t 3 May 2016
db_1 | 2017-05-31T09:24:59.086+0000 I CONTROL [initandlisten] allocator: tcmalloc
db_1 | 2017-05-31T09:24:59.086+0000 I CONTROL [initandlisten] modules: none
db_1 | 2017-05-31T09:24:59.086+0000 I CONTROL [initandlisten] build environment:
db_1 | 2017-05-31T09:24:59.086+0000 I CONTROL [initandlisten] distmod: debian81
db_1 | 2017-05-31T09:24:59.086+0000 I CONTROL [initandlisten] distarch: x86_64
db_1 | 2017-05-31T09:24:59.086+0000 I CONTROL [initandlisten] target_arch: x86_64
db_1 | 2017-05-31T09:24:59.086+0000 I CONTROL [initandlisten] options: {}
db_1 | 2017-05-31T09:24:59.088+0000 W - [initandlisten] Detected unclean shutdown - /data/db/mongod.lock is not empty.
db_1 | 2017-05-31T09:24:59.095+0000 I - [initandlisten] Detected data files in /data/db created by the 'wiredTiger' storage engine, so setting the active storage engine to 'wiredTiger'.
db_1 | 2017-05-31T09:24:59.098+0000 W STORAGE [initandlisten] Recovering data from the last clean checkpoint.
db_1 | 2017-05-31T09:24:59.099+0000 I STORAGE [initandlisten] wiredtiger_open config: create,cache_size=487M,session_max=20000,eviction=(threads_min=4,threads_max=4),config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),checkpoint=(wait=60,log_size=2GB),statistics_log=(wait=0),
monitor_1 | 2017-05-31 09:24:59,336 monitor:213 INFO:Script started
monitor_1 | 2017-05-31 09:24:59,337 monitor:25 DEBUG:Connecting to cdm
monitor_1 | 2017-05-31 09:24:59,618 monitor:27 DEBUG:Connected to cdm
Tips
- Try and keep each docker container doing only one thing
- Keep your build times short by putting frequently changed things near the end of your docker file
- docker technology is still moving fast, good news bugs fixed fast, bad news things are constantly changing
To Research Further
- Windows running in a docker container: looked into it before but found alot of new licenses needed
- Container orchestration: i.e. how to manage 100s of containers with technologies like kubernetes and docker swarm
- docker security: how easy is it to escape a docker container? docker diff and docker readonly filesystems, Scanning docker images for CVES (docker supply a service for this called Docker Security Scanning)
?QUESTIONS?
THANK YOU
Anymore questions please e-mail me at dockerslides@mail.corepipeline.com
Docker Lightning talk
By gerardk
Docker Lightning talk
An quick introduction to docker using a flask python example
- 2,051