SlideShare a Scribd company logo
DevOPS – Day 2
Thursday, June 2th 2016
Questions since the previous workshop?
What is a container?
• Program executed in a limited kernel resources
• Cgroups: CPU, Memory, Network, …
• Namespace: Process, User, Filesystem, ...
• Union file system: Filesystem
Virtual machine vs Container
Server
OS
Hypervisor
Guest OS
Libraries
App 1
Server
OS
Docker Engine
Guest OS Guest OS
Libraries Libraries
App 2 App 3
Libraries
App 1
Libraries Libraries
App 2 App 3
Containers for developers
• Micro service paradigm
• Environment variables for application configuration
User
Contract
Support
Notification
Analytics
Invoice
Storage
Task
Containers for system engineers
• Blue/Green deployment
• High level management
• Security issues
• Change all tools…
User
Load balancer
A version
B version
A version
B version
A version
B version
Web server Application Database
Docker
Very active project…
You must read release notes and check technical details!
Docker softwares
Docker
Engine
Docker
Machine
Docker
Registry
Docker
Compose
Kitematic
Docker Toolbox
Automate Docker
provisioning
Desktop GUI for
Docker
Multi-container
orchestration tool
Run Docker
container
Docker containers
hosted registry
Helper to install Docker
components on desktop
Docker
Cloud
Hosted service for
building and
deployingcontainers
Docker
Swarm
Host clustering
and container
scheduling
Dockerfile
• Dockerfile is a document composed of various commands to assemble
an image
• Inherit from on a base image
Dockerfile
FROM ubuntu
MAINTAINER SvenDowideit@docker.com
RUN apt-key adv --keyserverhkp://p80.pool.sks-keyservers.net:80--recv-keys
B97B0AFCAA1A47F044F244A07FCC7D46ACCC4CF8
RUN echo "deb http://apt.postgresql.org/pub/repos/apt/ precise-pgdgmain">/etc/apt/sources.list.d/pgdg.list
RUN apt-get update && apt-get install -y python-software-properties software-properties-common postgresql-9.3 postgresql-client-9.3
postgresql-contrib-9.3
USER postgres
RUN /etc/init.d/postgresqlstart &&
psql --command "CREATE USER dockerWITH SUPERUSER PASSWORD 'docker';"&&
createdb -O dockerdocker
RUN echo "host all all 0.0.0.0/0 md5">> /etc/postgresql/9.3/main/pg_hba.conf
RUN echo "listen_addresses='*'">> /etc/postgresql/9.3/main/postgresql.conf
EXPOSE 5432
VOLUME ["/etc/postgresql","/var/log/postgresql","/var/lib/postgresql"]
CMD ["/usr/lib/postgresql/9.3/bin/postgres", "-D", "/var/lib/postgresql/9.3/main", "-c", "config_file=/etc/postgresql/9.3/main/postgresql.conf"]
Base image
Maintainer information
Install requirements
Set user to use when
running the image
Application configuration
Export port on network
Export volumes
Start application
Docker compose
• Compose is a tool for defining and running multi-container Docker
applications
• /! 2 versions of docker-compose format
docker-compose.yml (version 1)
postgres :
image: postgres
cron:
build: .
dockerfile: Dockerfile-cron
links:
- postgres
web:
build: .
volumes:
- .:/code
ports:
- "8000:8000"
links:
- postgres
PostgreSQLdatabase
CRON container
Load volume from
current directory
Expose ports
Link containers
docker-compose.yml (version 2)
version: '2’
services:
web:
build: .
ports:
- ”8000:8000"
volumes:
- .:/code
- logvolume01:/var/log
depends_on:
- postgresql
postgres:
image: postgres
volumes:
logvolume01: {}
Version
Application
Postgresql
Volumes
Running containers with Docker composer
1. Define application container
Create Dockerfile
2. Define container relations
Create docker-compose.yml
3. Start containers
docker-compose up
4. Execute commands
docker-compose run <container> <cmd>
Let’s try Docker…
Install Docker Toolbox
• Docker Toolbox’ll install all tools
• https://www.docker.com/products/docker-toolbox
Virtualbox
Virtual machine
Docker Engine
Docker Machine
Docker CLI client
Docker Composer
Container
Create your first container
• Start Docker server
docker-machine start default
• Load Docker connection parameters
eval $(docker-machine env default)
• Create Dockerfile
FROM php
WORKDIR /var/www
ADD index.php .
EXPOSE 8080
ENTRYPOINT ["php", "-S", "0.0.0.0:8080"]
Start your first container
• Create index.php
echo '<?php echo ”Good morning!n”; ?>' > index.php
• Build container
docker build -t app1 .
• Check images
docker images | head
• Run the container
docker run -t app1 -p 8080:8080 app1
• Connect to the website
curl -v $(docker-machine ip):8080
Update the container
• Update application content
echo '<?php echo "Welcome!n”;?>' > index.php
• Connect to the website
curl -v $(docker-machine ip):8080
• Still Good morning… What’s wrong?
• Restart your container
Share your container
• Create an account on https://hub.docker.com
• Tag your container
docker tag app1 <username>/app1
• Connect on Dockerhub
docker login
• Push container to Docker Hub
docker push <username>/app1
• Connect on DockerHub website
https://hub.docker.com/<username>/app1/
cAdvisor
• Created by Google to monitor their own containers (lmctfy)
• Analyzes resource usage and performance characteristics of running
containers.
Start cAdvisor
• Run cAdvisor
docker run 
--name=cadvisor 
--restart always 
--detach=true
--volume=/:/rootfs:ro
--volume=/var/run:/var/run:rw 
--volume=/sys:/sys:ro
--volume=/var/lib/docker/:/var/lib/docker:ro 
--publish=8001:8080 
google/cadvisor:latest
• Connect to the web interface
http://192.168.99.100:8001
Unit tests
Continuous integration/deployment
• Continuous integration
• Run tests for each commit
• Detect bugs
• Continuous deployment
• Deploy application if tests success
GIT repository
CI CD
ProductionStaging
Developer
Gitlab
• Open source GIT repository management solution
• Community and Enterprise editions
• GitHub alternative
• https://about.gitlab.com
Start GitLab
• Run GitLab
docker run --detach
--hostname gitlab.example.com
--publish8000:80 
--name gitlab
--restart always 
--volume /srv/gitlab/config:/etc/gitlab
--volume /srv/gitlab/logs:/var/log/gitlab
--volume /srv/gitlab/data:/var/opt/gitlab
gitlab/gitlab-ce:latest
• Connect to the web interface(Default password: root/5iveL!fe)
http://$(docker-machine ip):8000
Configure GitLab
• Connect on GitLab interface
• Create a project
• Create a user for Jenkins
Clone the repository
git clone http://192.168.99.100:8000/root/app1.git
cd app1/
echo 'Welcome' > README.md
git add README.md
git commit README.md -m 'Add README.md'
git push
Jenkins
• The most popular tool to build and deploy projects
• Unittest
• Continuous Integration
• Continuous Delivery
• Plugins for everything!
• Distribute work across multiple machines
• https://jenkins.io
Start Jenkins
• Run Jenkins
docker run --detach 
--publish 10000:8080 
--name jenkins 
--restart always 
jenkins:latest
• Connect to the web interface
http://$(docker-machine ip):10000
Configure Jenkins
• Install “Git plugin”
• Restart Jenkins
• Create a new project called “App1 - master”
• Add GIT credentials
• Poll SCM
* * * * *
• Execute
phpunit -c app
Lunch break!
Evaluation
Create a web hit counter with containers
Docker Engine
Redis
PHP application
Technical details
• 2 Docker containers:
• Redis
• Application (PHP application)
• Use PHP composer to install requirements
• Use Docker Compose to start containers
• Bonus:
• 3 tiers architecture (Nginx/PHP/Redis)
• SSL
• Unit tests
• …
Solution
Application files
<?php
require __DIR__. '/vendor/autoload.php';
PredisAutoloader::register();
// Connect to redis
try {
$redis = new PredisClient('tcp://redis:6379');
}
catch (Exception $error){
die($error->getMessage());
}
// Get visitors
$visitors = ($redis->exists('visitors')) ? $redis->get('visitors'): 0;
// Increment visitors
$visitors++;
$redis->set('visitors',$visitors);
// Display visitors
if ($visitors > 1) {
echo "<h1>There are$visitors visitors!</h1>n";
} else {
echo "<h1>There is $visitors visitor!</h1>n";
}
?>
{
"require": {
"predis/predis": "^1.0.3"
}
}
index.php composer.json
Docker files
FROM php:5-apache
# Install GIT
RUN apt-get update
&& apt-getinstall -ygit 
&& apt-getclean
&& rm -rf/var/lib/apt/lists/*
# Install PHP composer
RUN curl -sS https://getcomposer.org/installer | php ----install-dir=/usr/local/bin --
filename=composer
WORKDIR /var/www/html
# Add application
ADD . /var/www/html/
# Install applicationrequirements with PHP composer
RUN composer install
version: '2'
services:
application:
build: .
ports:
- "80:80"
depends_on:
- redis
redis:
image: redis:latest
Dockerfile docker-compose.yml
Q&A

More Related Content

DevOPS training - Day 2/2

  • 1. DevOPS – Day 2 Thursday, June 2th 2016
  • 2. Questions since the previous workshop?
  • 3. What is a container? • Program executed in a limited kernel resources • Cgroups: CPU, Memory, Network, … • Namespace: Process, User, Filesystem, ... • Union file system: Filesystem
  • 4. Virtual machine vs Container Server OS Hypervisor Guest OS Libraries App 1 Server OS Docker Engine Guest OS Guest OS Libraries Libraries App 2 App 3 Libraries App 1 Libraries Libraries App 2 App 3
  • 5. Containers for developers • Micro service paradigm • Environment variables for application configuration User Contract Support Notification Analytics Invoice Storage Task
  • 6. Containers for system engineers • Blue/Green deployment • High level management • Security issues • Change all tools… User Load balancer A version B version A version B version A version B version Web server Application Database
  • 8. Very active project… You must read release notes and check technical details!
  • 9. Docker softwares Docker Engine Docker Machine Docker Registry Docker Compose Kitematic Docker Toolbox Automate Docker provisioning Desktop GUI for Docker Multi-container orchestration tool Run Docker container Docker containers hosted registry Helper to install Docker components on desktop Docker Cloud Hosted service for building and deployingcontainers Docker Swarm Host clustering and container scheduling
  • 10. Dockerfile • Dockerfile is a document composed of various commands to assemble an image • Inherit from on a base image
  • 11. Dockerfile FROM ubuntu MAINTAINER SvenDowideit@docker.com RUN apt-key adv --keyserverhkp://p80.pool.sks-keyservers.net:80--recv-keys B97B0AFCAA1A47F044F244A07FCC7D46ACCC4CF8 RUN echo "deb http://apt.postgresql.org/pub/repos/apt/ precise-pgdgmain">/etc/apt/sources.list.d/pgdg.list RUN apt-get update && apt-get install -y python-software-properties software-properties-common postgresql-9.3 postgresql-client-9.3 postgresql-contrib-9.3 USER postgres RUN /etc/init.d/postgresqlstart && psql --command "CREATE USER dockerWITH SUPERUSER PASSWORD 'docker';"&& createdb -O dockerdocker RUN echo "host all all 0.0.0.0/0 md5">> /etc/postgresql/9.3/main/pg_hba.conf RUN echo "listen_addresses='*'">> /etc/postgresql/9.3/main/postgresql.conf EXPOSE 5432 VOLUME ["/etc/postgresql","/var/log/postgresql","/var/lib/postgresql"] CMD ["/usr/lib/postgresql/9.3/bin/postgres", "-D", "/var/lib/postgresql/9.3/main", "-c", "config_file=/etc/postgresql/9.3/main/postgresql.conf"] Base image Maintainer information Install requirements Set user to use when running the image Application configuration Export port on network Export volumes Start application
  • 12. Docker compose • Compose is a tool for defining and running multi-container Docker applications • /! 2 versions of docker-compose format
  • 13. docker-compose.yml (version 1) postgres : image: postgres cron: build: . dockerfile: Dockerfile-cron links: - postgres web: build: . volumes: - .:/code ports: - "8000:8000" links: - postgres PostgreSQLdatabase CRON container Load volume from current directory Expose ports Link containers
  • 14. docker-compose.yml (version 2) version: '2’ services: web: build: . ports: - ”8000:8000" volumes: - .:/code - logvolume01:/var/log depends_on: - postgresql postgres: image: postgres volumes: logvolume01: {} Version Application Postgresql Volumes
  • 15. Running containers with Docker composer 1. Define application container Create Dockerfile 2. Define container relations Create docker-compose.yml 3. Start containers docker-compose up 4. Execute commands docker-compose run <container> <cmd>
  • 17. Install Docker Toolbox • Docker Toolbox’ll install all tools • https://www.docker.com/products/docker-toolbox Virtualbox Virtual machine Docker Engine Docker Machine Docker CLI client Docker Composer Container
  • 18. Create your first container • Start Docker server docker-machine start default • Load Docker connection parameters eval $(docker-machine env default) • Create Dockerfile FROM php WORKDIR /var/www ADD index.php . EXPOSE 8080 ENTRYPOINT ["php", "-S", "0.0.0.0:8080"]
  • 19. Start your first container • Create index.php echo '<?php echo ”Good morning!n”; ?>' > index.php • Build container docker build -t app1 . • Check images docker images | head • Run the container docker run -t app1 -p 8080:8080 app1 • Connect to the website curl -v $(docker-machine ip):8080
  • 20. Update the container • Update application content echo '<?php echo "Welcome!n”;?>' > index.php • Connect to the website curl -v $(docker-machine ip):8080 • Still Good morning… What’s wrong? • Restart your container
  • 21. Share your container • Create an account on https://hub.docker.com • Tag your container docker tag app1 <username>/app1 • Connect on Dockerhub docker login • Push container to Docker Hub docker push <username>/app1 • Connect on DockerHub website https://hub.docker.com/<username>/app1/
  • 22. cAdvisor • Created by Google to monitor their own containers (lmctfy) • Analyzes resource usage and performance characteristics of running containers.
  • 23. Start cAdvisor • Run cAdvisor docker run --name=cadvisor --restart always --detach=true --volume=/:/rootfs:ro --volume=/var/run:/var/run:rw --volume=/sys:/sys:ro --volume=/var/lib/docker/:/var/lib/docker:ro --publish=8001:8080 google/cadvisor:latest • Connect to the web interface http://192.168.99.100:8001
  • 25. Continuous integration/deployment • Continuous integration • Run tests for each commit • Detect bugs • Continuous deployment • Deploy application if tests success GIT repository CI CD ProductionStaging Developer
  • 26. Gitlab • Open source GIT repository management solution • Community and Enterprise editions • GitHub alternative • https://about.gitlab.com
  • 27. Start GitLab • Run GitLab docker run --detach --hostname gitlab.example.com --publish8000:80 --name gitlab --restart always --volume /srv/gitlab/config:/etc/gitlab --volume /srv/gitlab/logs:/var/log/gitlab --volume /srv/gitlab/data:/var/opt/gitlab gitlab/gitlab-ce:latest • Connect to the web interface(Default password: root/5iveL!fe) http://$(docker-machine ip):8000
  • 28. Configure GitLab • Connect on GitLab interface • Create a project • Create a user for Jenkins
  • 29. Clone the repository git clone http://192.168.99.100:8000/root/app1.git cd app1/ echo 'Welcome' > README.md git add README.md git commit README.md -m 'Add README.md' git push
  • 30. Jenkins • The most popular tool to build and deploy projects • Unittest • Continuous Integration • Continuous Delivery • Plugins for everything! • Distribute work across multiple machines • https://jenkins.io
  • 31. Start Jenkins • Run Jenkins docker run --detach --publish 10000:8080 --name jenkins --restart always jenkins:latest • Connect to the web interface http://$(docker-machine ip):10000
  • 32. Configure Jenkins • Install “Git plugin” • Restart Jenkins • Create a new project called “App1 - master” • Add GIT credentials • Poll SCM * * * * * • Execute phpunit -c app
  • 34. Evaluation Create a web hit counter with containers
  • 36. Technical details • 2 Docker containers: • Redis • Application (PHP application) • Use PHP composer to install requirements • Use Docker Compose to start containers • Bonus: • 3 tiers architecture (Nginx/PHP/Redis) • SSL • Unit tests • …
  • 38. Application files <?php require __DIR__. '/vendor/autoload.php'; PredisAutoloader::register(); // Connect to redis try { $redis = new PredisClient('tcp://redis:6379'); } catch (Exception $error){ die($error->getMessage()); } // Get visitors $visitors = ($redis->exists('visitors')) ? $redis->get('visitors'): 0; // Increment visitors $visitors++; $redis->set('visitors',$visitors); // Display visitors if ($visitors > 1) { echo "<h1>There are$visitors visitors!</h1>n"; } else { echo "<h1>There is $visitors visitor!</h1>n"; } ?> { "require": { "predis/predis": "^1.0.3" } } index.php composer.json
  • 39. Docker files FROM php:5-apache # Install GIT RUN apt-get update && apt-getinstall -ygit && apt-getclean && rm -rf/var/lib/apt/lists/* # Install PHP composer RUN curl -sS https://getcomposer.org/installer | php ----install-dir=/usr/local/bin -- filename=composer WORKDIR /var/www/html # Add application ADD . /var/www/html/ # Install applicationrequirements with PHP composer RUN composer install version: '2' services: application: build: . ports: - "80:80" depends_on: - redis redis: image: redis:latest Dockerfile docker-compose.yml
  • 40. Q&A