SlideShare a Scribd company logo
1 of 126
Download to read offline
Testing fácil con
Docker:
Gestiona dependencias
y unifica entornos
Micael Gallego (CodeURJC)
@micael_gallego
MAD · NOV 24-25 · 2017
Testing fácil con Docker · @micael_gallego
@micael_gallego
micael.gallego@urjc.es
@micaelgallego
About me
developer
University professor
Trainer & Consultor
Testing fácil con Docker · @micael_gallego
Consultancy / Training
Cloud Computing
WebTechnologies
Extreme Programming
Testing / Git / Jenkins
Software Architecture
Concurrent Programming
Open source elastic platform
for end to end testing
http://codeurjc.es http://elastest.io
Advanced log management
Test orchestration
Test execution comparison
Web and Mobile testing
Security testing
IoT testing
Performance testing
Testing fácil con Docker · @micael_gallego
Virtualizacion and Containers
● Developers want to reduce the
differences between local,
continuous integration and
production environments
● Avoiding “It works in my machine”
type of problems
Virtualization Containers
Testing fácil con Docker · @micael_gallego
Virtualizacion and Containers
● Virtualization
– Full fledged Virtual Machine (VirtualBox)
– Developer friendly managed VM
(Vagrant)
● Containers
– Docker
Testing fácil con Docker · @micael_gallego
Testing fácil con Docker · @micael_gallego
VirtualBox
● Developed by Oracle (was owned by Sun
Microsystems)
● Mostly open source, several free (but closed
source) modules
● Windows, Linux and Mac versions
● Advanced Desktop Virtualization
– Shared folders host / guest
– Keyboard and mouse advanced integration
– Graphic 3D acceleration
– Webcam
https://www.virtualbox.org/
Testing fácil con Docker · @micael_gallego
VirtualBox
Testing fácil con Docker · @micael_gallego
VirtualBox
● Manual
– Create an empty virtual machine
– Connect to a ISO (simulating real CD
devide)
– Install a full fledged Operating System
– It is time consuming and it is not easy
to share VM between developers
Testing fácil con Docker · @micael_gallego
VirtualBox
Testing fácil con Docker · @micael_gallego
VirtualBox
Testing fácil con Docker · @micael_gallego
Testing fácil con Docker · @micael_gallego
Vagrant
● It is a command line utility to manage
VMs
● It makes very easy to download and start
a new VM (only with a command)
● Allows to provisioning the new VM with
command provisioning tools (script, chef,
puppet, ansible…)
● VM configuration is specified in a text file,
allowing to share it in the git repository
https://www.vagrantup.com/
Testing fácil con Docker · @micael_gallego
Vagrant
● How to create a new VM with ubuntu Xenial
● Vagrant manages certificates and networking
to make easy to connect to the new VM
● By default, working dir is shared
with VM
$ vagrant init ubuntu/xenial64
$ vagrant up
$ vagrant ssh
Testing fácil con Docker · @micael_gallego
containers
https://www.docker.com/
Testing fácil con Docker · @micael_gallego
Docker
● With VMs you can have the production
environment in your laptop
● But…
– VMs takes minutes to start up
– VMs use (waste?) a lot of resources
(memory and disk space)
Testing fácil con Docker · @micael_gallego
Docker
● In a first look, containers can be
considered as “lightweight VMs”
– They contain an isolated environment to run
apps
– Start in milliseconds
– They use only the resources it needs
– A container doesn't have a full fledged
operating system, only the minimal
software to execute apps
Testing fácil con Docker · @micael_gallego
Docker
Testing fácil con Docker · @micael_gallego
Docker
●
Containers and VMs are very different
Virtual Machines Containers
Heavier Lighter
Execute several processes per
Virtual Machine
Usually execute only one process
per container
Ssh connection Direct execution in the container
(rarely needed)
More isolated using hypervisor Less isolated because are
executed using kernel features
Can virtualizeWindows over
Linux
Linux containers must be
executed in linux hosts*
* More on that later
Testing fácil con Docker · @micael_gallego
Docker
● To install an application in a linux system you
need all dependencies installed
● Can be incompatibilities between applications
that need different version of the same
dependency
● Docker include in a container all needed
software isolated to the rest of the system
Testing fácil con Docker · @micael_gallego
Docker
Testing fácil con Docker · @micael_gallego
Docker
● Docker containers SO support
– Linux containers
● Very mature technology
● It can be used in any* linux distribution
– Windows containers
● Preliminary technology
● It only can be used in a very recent**
Windows Server version
* Kernel version 3.10 or greather. Published in June 2013
**Windows Server 2016 (Core and with Desktop Experience), Nano Server, andWindows 10
Professional and Enterprise (Anniversary Edition).
Testing fácil con Docker · @micael_gallego
Docker
● You can execute linux containers in
any operating system
● It uses virtualization (under the
covers) in Mac and Windows
Testing fácil con Docker · @micael_gallego
Docker
● Docker Toolbox for Mac and Windows
– It uses VirtualBox as virtualization
– It is not the same development experience
than in linux
● Docker for Mac and Windows
– Uses native virtualization technology in each
operating system
– Only available in new versions of that SOs
Testing fácil con Docker · @micael_gallego
Docker
Docker concepts
Testing fácil con Docker · @micael_gallego
•Docker Image
– Basic template for a container (hard disk of
VM)
– It contains SO (ubuntu), libs (Java) and app
(webapp.jar)
– A container always is started from an image
– If you want to start a new container from an
image that is not in your system, it is
automatically downloaded from Internet
Docker concepts
Testing fácil con Docker · @micael_gallego
•Docker Registry
●
Remote service used to store and retrive docker
images
●
It can hold several versions of the same image
●
All versions of the same image are located in the
same repository (like in git)
●
Docker Hub is a public registry managed by Docker
Inc.
●
You can buy private repositories in Docker Hub
●
You can also operate your own private registry
Docker concepts
Testing fácil con Docker · @micael_gallego
•Popular repositories in Docker Hub
Docker concepts
Testing fácil con Docker · @micael_gallego
•Docker Container
– It is the “equivalent” of a Virtual Machine
– A container is created from a docker image
– When a file is wrote, the image it is not
modified, the container is modified
– It can be started, paused or
stopped
Docker concepts
Testing fácil con Docker · @micael_gallego
•Docker Engine
– Local service used to control docker
– Manages images (download, create, pull,
push…)
– Manages containers (start, stop, commit...)
– It can be used with the docker client or
using its REST API
Docker concepts
Testing fácil con Docker · @micael_gallego
•Docker client
– Command line interface (CLI) tool to
control docker engine
– It is available when docker is installed in a
system to connect to their local docker
engine
Docker concepts
Testing fácil con Docker · @micael_gallego
•Official documentation
– https://docs.docker.com/
•Advanced tutorials
– https://docs.docker.com/engine/tutorials/dockerizing/
– https://docs.docker.com/engine/tutorials/usingdocker/
– https://docs.docker.com/engine/tutorials/dockerimages/
– https://docs.docker.com/engine/tutorials/networkingcontainers/
– https://docs.docker.com/engine/tutorials/dockervolumes/
– https://docs.docker.com/engine/tutorials/dockerrepos/
●
Cheat Sheet
– https://github.com/wsargent/docker-cheat-sheet/blob/master/README.md
Docker documentation
Testing fácil con Docker · @micael_gallego
First steps with docker
Install Docker
– Windows:
●
Microsoft Windows 10 Professional or Enterprise 64-bit:
https://store.docker.com/editions/community/docker-ce-desktop-windows
●
Other Windows versions: https://www.docker.com/products/docker-toolbox
– Linux:
●
Ubuntu: https://store.docker.com/editions/community/docker-ce-server-ubuntu
●
Fedora: https://store.docker.com/editions/community/docker-ce-server-fedora
●
Debian: https://store.docker.com/editions/community/docker-ce-server-debian
●
CentOS: https://store.docker.com/editions/community/docker-ce-server-centos
– Mac:
●
Apple Mac OS Yosemite 10.10.3 or above:
https://store.docker.com/editions/community/docker-ce-desktop-mac
●
Older Mac: https://www.docker.com/products/docker-toolbox
Testing fácil con Docker · @micael_gallego
First steps with docker
Hands on…
https://github.com/docker/labs/tree/master/beginner
Testing fácil con Docker · @micael_gallego
First steps with docker
Testing if docker is correctly installed
$ docker run hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
03f4658f8b78: Pull complete
a3ed95caeb02: Pull complete
Digest:
sha256:8be990ef2aeb16dbcb9271ddfe2610fa6658d13f6dfb8bc72074cc1ca369
66a7
Status: Downloaded newer image for hello-world:latest
Hello from Docker.
This message shows that your installation appears to be working
correctly.
...
Testing fácil con Docker · @micael_gallego
First steps with docker
Running your first container
$ docker run alpine ls -l
total 48
drwxr-xr-x 2 root root 4096 Mar 2 16:20 bin
drwxr-xr-x 5 root root 360 Mar 18 09:47 dev
drwxr-xr-x 13 root root 4096 Mar 18 09:47 etc
drwxr-xr-x 2 root root 4096 Mar 2 16:20
home
drwxr-xr-x 5 root root 4096 Mar 2 16:20 lib
......
......
Testing fácil con Docker · @micael_gallego
First steps with docker
Running your first container
$ docker run alpine ls -l
Command “run”
Creates a new
container and start it
Testing fácil con Docker · @micael_gallego
First steps with docker
Running your first container
$ docker run alpine ls -l
Image name
alpine is a minimal linux system
(4.8Mb).The image is downloaded if
not stored in local machine
Testing fácil con Docker · @micael_gallego
First steps with docker
Running your first container
$ docker run alpine ls -l
Command “ls -l”
This command will be
executed inside the
running container
Testing fácil con Docker · @micael_gallego
First steps with docker
Inspecting the downloaded images
$ docker images
REPOSITORY TAG IMAGE ID
CREATED VIRTUAL SIZE
alpine latest c51f86c28340
4 weeks ago 1.109 MB
hello-world latest 690ed74de00f
5 months ago 960 B
List all images stored in the system
Testing fácil con Docker · @micael_gallego
First steps with docker
Executing a container
$ docker run alpine echo "hello from alpine"
hello from alpine
Execute the command “echo” inside the container
Testing fácil con Docker · @micael_gallego
First steps with docker
Inspecting containers (executing)
$ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED
STATUS PORTS NAMES
a6a9d46d0b2f alpine "echo 'hello from alp" 6 minutes
ago Exited (0) 6 minutes ago lonely_kilby
ff0a5c3750b9 alpine "ls -l" 8 minutes
ago Exited (0) 8 minutes ago elated_ramanujan
c317d0a9e3d2 hello-world "/hello" 34 seconds
ago Exited (0) 12 minutes ago stupefied_mcclintock
It shows containers in the system.
All of them has STATUS Exited.These containers are
not currently executing (but using disk space)
Testing fácil con Docker · @micael_gallego
First steps with docker
Interactive commands in containers
$ docker run -it alpine /bin/sh
/ # ls
bin dev etc home lib linuxrc media mnt proc
root run sbin sys tmp usr var
/ # uname -a
Linux 97916e8cb5dc 4.4.27-moby #1 SMP Wed Oct 26 14:01:48 UTC 2016 x86_64
Linux
/ # exit
$
To execute an interactive command it is necessary to
use the option “-it” to connect the console to the
container command
Testing fácil con Docker · @micael_gallego
First steps with docker
● Interactive commands in containers
– When you execute a /bin/sh command in
a container it offers a “similar” experience
than a ssh connection
– Buy there are no ssh server neither ssh
client
– It is executing a shell inside the
container
Testing fácil con Docker · @micael_gallego
First steps with docker
● Managing containers lifecycle
$ docker run -d seqvence/static-site
Option “-d”
Executes the container
in background
Testing fácil con Docker · @micael_gallego
First steps with docker
● Managing containers lifecycle
$ docker ps
CONTAINER ID IMAGE COMMAND
CREATED STATUS PORTS NAMES
a7a0e504ca3e seqvence/static-site "/bin/sh -c 'cd /usr/"
28 seconds ago Up 26 seconds
Container id is
a7a0e504ca3e
This id is used to refer to
this container
STATUS is UP
Testing fácil con Docker · @micael_gallego
First steps with docker
● Managing containers lifecycle
– Stop running container
– Delete files of the stopped container
$ docker stop a7a0e504ca3e
$ docker rm a7a0e504ca3e
Testing fácil con Docker · @micael_gallego
Net services with docker
● Start container exposing a port
docker run --name static-site 
-e AUTHOR="Your Name" -d 
-p 9000:80 seqvence/static-site
Testing fácil con Docker · @micael_gallego
Net services with docker
● Start container exposing a port
docker run --name static-site 
-e AUTHOR="Your Name" -d 
-p 9000:80 seqvence/static-site
--name static-site
Specifies a unique name
for the container
Testing fácil con Docker · @micael_gallego
Net services with docker
docker run --name static-site 
-e AUTHOR="Your Name" -d 
-p 9000:80 seqvence/static-site
-e AUTHOR="Your Name"
Set the environment variable
AUTHOR to value “Your Name”
● Start container exposing a port
Testing fácil con Docker · @micael_gallego
Net services with docker
docker run --name static-site 
-e AUTHOR="Your Name" -d 
-p 9000:80 seqvence/static-site
-d
Execute container as deamon
● Start container exposing a port
Testing fácil con Docker · @micael_gallego
Net services with docker
docker run --name static-site 
-e AUTHOR="Your Name" -d 
-p 9000:80 seqvence/static-site
-p 9000:80
Connects the host port 9000 to
the port 80 in the container
● Start container exposing a port
Testing fácil con Docker · @micael_gallego
Net services with docker
● Use the service
– Open http://127.0.0.1:9000 in a browser in
your host to access 80 port in container
Testing fácil con Docker · @micael_gallego
Net services with docker
● Use the service
– If you are using Docker Toolbox for Mac or
Windows you can’t use 127.0.0.1 IP
– Then you have to open
http://192.168.99.100:9000/ in the browser
$ docker-machine ip default
192.168.99.100
Testing fácil con Docker · @micael_gallego
Net services with docker
● Container management
– Stop and remove the container
– Stop and remove a running container
– Remove all running containers
$ docker rm -f static-site
$ docker stop static-site
$ docker rm static-site
$ docker rm -f $(docker ps -a -q)
Testing fácil con Docker · @micael_gallego
Managing docker images
● List images in host
Tag is like “version”. Latest is… the
latest ;)
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
seqvence/static-site latest 92a386b6e686 2 hours ago 190.5 MB
nginx latest af4b3d7d5401 3 hours ago 190.5 MB
python 2.7 1c32174fd534 14 hours ago 676.8 MB
postgres 9.4 88d845ac7a88 14 hours ago 263.6 MB
Containous/traefik latest 27b4e0c6b2fd 4 days ago 20.75 MB
...
Testing fácil con Docker · @micael_gallego
Managing docker images
● Managing versions
– Download a concrete version
– Download latest version
$ docker pull ubuntu:12.04
$ docker pull ubuntu
Testing fácil con Docker · @micael_gallego
Managing docker images
● Searching images in DockerHub
Testing fácil con Docker · @micael_gallego
Managing docker images
● Searching images in Docker Store
Testing fácil con Docker · @micael_gallego
Managing docker images
● Image types
– Base images
● Images without a parent image
● Examples: Ubuntu, debian, alpine…
● Used by Operating Systems
– Child images
● Base images plus some additional software
● Examples: Nginx, Apache, MySQL...
Testing fácil con Docker · @micael_gallego
Managing docker images
●
Official vs User images
– Official images
●
Images created by trusted
companies or communities
– User images
●
Any user can create an account and
upload her own images
Testing fácil con Docker · @micael_gallego
Managing docker images
● Create your first
docker image
– We will create a web
application for display
random cat pics using
Python
– Clone following repository
and go to project folder
$ git clone https://github.com/codeurjc/testing-docker
$ cd web-python
Testing fácil con Docker · @micael_gallego
Managing docker images
● Create your first image
– You have all source files for the web
application
– But you need Python and Flask to execute
the app
– To execute the web application, you will
create a new image with dependencies
(Python and Flask) and your application code
– Then you can create a new container to
execute your application
Testing fácil con Docker · @micael_gallego
Managing docker images
● Dockerfile
– File used to describe a new image
– Specifies
● Base image
● Commands to execute in the image
● Files to include in the image from the
project folder
● Open ports
● Command to execute when start the image
Testing fácil con Docker · @micael_gallego
# Select base image
FROM alpine:latest
# Install python and pip
RUN apk add ­­update py­pip
RUN pip install ­­upgrade pip
# copy app files
COPY app.py /usr/src/app/
COPY templates/index.html /usr/src/app/templates/
COPY requirements.txt /usr/src/app/
# install Python modules needed by the app
RUN pip install ­­no­cache­dir ­r  
   /usr/src/app/requirements.txt
# tell the port number the container should expose
EXPOSE 5000
# run the application
CMD ["python", "/usr/src/app/app.py"]
Testing fácil con Docker · @micael_gallego
Managing docker images
● Dockerfile
– FROM: Base image
– COPY: Copy files from Dockerfile folder
– RUN execute commands
– EXPOSE: Public ports
– CMD: Command to execute when
container is started
https://docs.docker.com/engine/userguide/eng-
image/dockerfile_best-practices/
Testing fácil con Docker · @micael_gallego
Managing docker images
● Build the image
– In the folder with a Dockerfile execute
– Executed actions
● Create a new container with base image
● Execute commands and copy app files
● Create a new container with the result
$ docker build -t webapp .
Testing fácil con Docker · @micael_gallego
Managing docker images
● Run the new image
– Open http://127.0.0.1:9000/ in the browser
– Windows and Mac users with Toolbox use the IP
$ docker run -p 9000:5000 webapp
* Running on http://0.0.0.0:5000/
(Press CTRL+C to quit)
Testing fácil con Docker · @micael_gallego
Managing docker images
Testing fácil con Docker · @micael_gallego
Managing docker images
● Build the image again
– Change some HTML in templatesindex.html
– Create the image again
– The Dockerfile steps without changes are not re-
executed (are reused from previous execution)
– The image is created very quickly because only
the files copy is perfomed
$ docker build -t webapp .
$ docker run -p 9000:5000 webapp
Testing fácil con Docker · @micael_gallego
Volumes
● Volumes
– Allow sharing files between host and container
– Execute a container to show an nonexistent file
– Create a text file
$ docker run alpine cat /data/file.txt
cat: can't open '/data/file.txt': No such
file or directory
$ echo "My file" >> file.txt
Testing fácil con Docker · @micael_gallego
Volumes
Volumes
● Mount a host folder inside a container folder
● Host contents replace container contents of
that folder
● Containers can write files in volumes to be
available in the host
$ sudo docker run -v "$PWD":/data alpine
cat /data/file.txt
My file
Testing fácil con Docker · @micael_gallego
Volumes
● Volumes
– Docker images use volumes to read files
from host
– Official NGINX container can serve host
files using http
● Serving current folder files ($PWD)
● Go to http://127.0.0.1:9000/file.txt
https://hub.docker.com/_/nginx/
$ docker run -p 9000:80 -v 
"$PWD":/usr/share/nginx/html:ro -d nginx
Testing fácil con Docker · @micael_gallego
Volumes
● Volumes
– Docker Toolbox for Win or
Mac only allow folders
inside user folder to be
used as volume
– You can use other folders
but have to configure
shared folders in
VirtualBox
https://hub.docker.com/_/nginx/
Testing fácil con Docker · @micael_gallego
Volumes
● Developing web app with
volumes
– If you mount your code folder in a
container, when you edit a file, it is
available to the container automatically
– It is not necessary to recreate the image
in each edition
$ docker run -v "$PWD":/usr/src/app/ 
-p 9000:5000 webapp
Testing fácil con Docker · @micael_gallego
•Containers main use cases
– Net service
●
Executed in background long time...
●
Used through network
●
Ex: Databases, web servers...
– Command
●
Execute a single command and stop
●
Read and write files from host with volumes
●
Ex: Java Compiler, jekyll, ffmpeg...
Docker container usage
Testing fácil con Docker · @micael_gallego
•Docker for building software
– A container can have all needed environment to
execute a developer tool
– For example, you can have the compiler and the
test dependencies in a container
– You can clone a git repository and execute the
(dockerized) compiler without install any
software in your host
Docker for software developers
Testing fácil con Docker · @micael_gallego
•Dockerized Java Maven
– Clone a maven repo
– Compile and exec tests
Docker for software developers
$ git clone https://github.com/codeurjc/testing-docker/
$ cd testing-docker/basic-java-tests
$ docker run --rm -v "$PWD":/data -w /data maven mvn package
Testing fácil con Docker · @micael_gallego
•Dockerized Java Maven
Docker for software developers
$ docker run --rm -v "$PWD":/data -w /data maven mvn package
https://hub.docker.com/_/maven/
--rm
Remove container when
execution finish
Testing fácil con Docker · @micael_gallego
•Dockerized Java Maven
Docker for software developers
$ docker run --rm -v "$PWD":/data -w /data maven mvn package
https://hub.docker.com/_/maven/
--w
Working dir for the command
Testing fácil con Docker · @micael_gallego
•Dockerized Java Maven
Docker for software developers
$ docker run --rm -v "$PWD":/data -w /data maven mvn package
https://hub.docker.com/_/maven/
maven
Official Maven image
Testing fácil con Docker · @micael_gallego
•Dockerized Java Maven
Docker for software developers
$ docker run --rm -v "$PWD":/data -w /data maven mvn package
https://hub.docker.com/_/maven/
mvn package
Maven command
Testing fácil con Docker · @micael_gallego
•Dockerized Java Maven
– Jar package is generated in /target folder in host
– As container command is executed as root user
(by default), generated files are owned by root.
– Change to your user
Docker for software developers
simple-maven-project-with-tests-1.0-SNAPSHOT.jar
https://hub.docker.com/_/maven/
sudo chown -R username:group target
Testing fácil con Docker · @micael_gallego
•Dockerized Node tests
– Clone a test repo
Compile and exec tests
Docker for software developers
$ git clone https://github.com/codeurjc/testing-docker/
$ cd testing-docker/basic-js-tests
$ docker run --rm -v "$PWD":/data -w /data node:9-alpine
npm install; npm test
Testing fácil con Docker · @micael_gallego
•Advantages of dockerized dev tools
– Avoid several developers having different
versions of such tools
– It is very easy to test the same code in different
versions (Java 7, Java 8...)
– Reduce tools configuration problems.You can
compile and execute a project easily
– The same tools can be executed in development
laptops and also in CI environment
Docker for software developers
Testing fácil con Docker · @micael_gallego
Docker for software developers
Testing fácil con Docker · @micael_gallego
•Docker in Continuous Integration
– If you execute dev tools in containers, it is very
easy to compile, test and package in CI
environment
– Only have to execute the same command in
laptop and CI environment
– If a tool changes, only have to change the
command, it is not necessary to install anything
Docker in CI servers
Testing fácil con Docker · @micael_gallego
https://jenkins.io/
•Docker in Continuous Integration
Docker in CI servers
– Download LTS version as .war
– We need Java 8 to execute it
$ java -jar jenkins.war
Testing fácil con Docker · @micael_gallego
•Docker in Continuous Integration
Docker in CI servers
Testing fácil con Docker · @micael_gallego
Jenkins installation
Testing fácil con Docker · @micael_gallego
Jenkins installation
Testing fácil con Docker · @micael_gallego
New admin
account
Jenkins installation
Testing fácil con Docker · @micael_gallego
Jenkins installation
Testing fácil con Docker · @micael_gallego
● Create new Jenkins job
– Create a job with pipeline
– Pipeline:
● Clone git repository
● Compile, test and package Java project
● Copy test results to Jenkins
Jenkins Job
Testing fácil con Docker · @micael_gallego
Creamos una nueva
tarea para descargar el
proyecto y ejecutar los
tests
Jenkins Job
Testing fácil con Docker · @micael_gallego
Jenkins Job
Testing fácil con Docker · @micael_gallego
Change command
to execute
dockerized maven
command
Testing fácil con Docker · @micael_gallego
node {
// Mark the code checkout 'stage'....
stage 'Checkout'
// Get some code from a GitHub repository
git url: 'https://github.com/codeurjc/testing-docker.git'
// Mark the code build 'stage'....
stage 'Build'
// Run the maven build
sh "cd basic-java-tests; docker run --rm -v "$PWD":/data -w /data maven mvn package"
step([$class: 'JUnitResultArchiver',
testResults: '**/target/surefire-reports/TEST-*.xml'])
}
Pipeline
Jenkins Job
Testing fácil con Docker · @micael_gallego
Execute the
new job
Jenkins Job
Testing fácil con Docker · @micael_gallego
“Checkout”
stage
Jenkins Job
Testing fácil con Docker · @micael_gallego
Compilation and
test stage
Jenkins Job
Testing fácil con Docker · @micael_gallego
Successful finished job
build (test passed)
Jenkins Job
Testing fácil con Docker · @micael_gallego
Clic on build to see
details
Jenkins Job
Testing fácil con Docker · @micael_gallego
See console output
Jenkins Job
Testing fácil con Docker · @micael_gallego
Repository clone
and dockerized
maven execution
Jenkins Job
Testing fácil con Docker · @micael_gallego
● Docker pipeline plugin
– Allows a more powerful way to control docker
container from pipelines
– It is not installed by default
Docker in CI servers
https://jenkins.io/doc/book/pipeline/docker/
node {
docker.image('node:7-alpine').inside {
stage('Test') {
sh 'node --version'
}
}
}
Testing fácil con Docker · @micael_gallego
Docker in CI servers
Testing fácil con Docker · @micael_gallego
Docker in CI servers
Testing fácil con Docker · @micael_gallego
Docker in CI servers
Testing fácil con Docker · @micael_gallego
Docker in CI servers
Testing fácil con Docker · @micael_gallego
● Docker pipeline plugin
Docker in CI servers
node {
stage ('Checkout') {
git url: 'https://github.com/codeurjc/testing-docker.git'
}
stage ('Build') {
docker.image('maven').inside {
sh 'cd basic-java-tests; mvn package'
}
step([$class: 'JUnitResultArchiver',
testResults: '**/target/surefire-reports/TEST-*.xml'])
}
}
sh "cd basic-java-tests; docker run --rm -v "$PWD":/data -w /data maven mvn package"
Testing fácil con Docker · @micael_gallego
● Docker pipeline plugin
– To cache Maven repository between executions,
it is possible to mount a volume
Docker in CI servers
node {
stage ('Checkout') {
git url: 'https://github.com/codeurjc/testing-docker.git'
}
stage ('Build') {
docker.image('maven').inside('-v $HOME/.m2:/root/.m2') {
sh 'cd basic-java-tests; mvn package'
}
step([$class: 'JUnitResultArchiver',
testResults: '**/target/surefire-reports/TEST-*.xml'])
}
}
Testing fácil con Docker · @micael_gallego
● Advantages of using docker in CI
– CI server just need docker installed, nothing
more
– All tools needed by devs are containerized
– Tools are downloaded (and cached)
automatically when needed
– Several languages/stacks/dependencies can be
used in the same CI server without conflicts
– Sysadmins do not need to give access to CI
server to developers (enforcing security)
Docker in CI servers
Testing fácil con Docker · @micael_gallego
● Testing different languages with
Docker
– Testing Node apps with mocha
● https://dzone.com/articles/testing-nodejs-application-
using-mocha-and-docker
– Testing C++ apps with Gtest
● https://github.com/yutakakinjyo/gtest-cmake-example
– Testing Angular apps
● https://jaxenter.com/build-and-test-angular-apps-using
-docker-132371.html
Docker in CI servers
Testing fácil con Docker · @micael_gallego
● Testing tools based on docker
– TestContainers
● Define testing dependencies in your JUnit test
● https://www.testcontainers.org/
– Dockunit
● Test your code in several environments
● https://www.npmjs.com/package/dockunit
– Many more...
Docker in CI servers
Testing fácil con Docker · @micael_gallego
Docker
Docker container as test dependencies
Docker for tests
Testing fácil con Docker · @micael_gallego
Docker
TestContainers
●
Library to write tests in Java with JUnit that allows
you to create and remove container in the context
of a test
●
It’s ideal for integration tests because external
dependencies are managed in the test, not in CI
server scripts
●
It is possible to start any container, but there are
wrappers for BBDD and Web browsers
https://github.com/testcontainers/testcontainers-java
Docker for tests
Testing fácil con Docker · @micael_gallego
Docker
TestContainers
●
How to start a generic container in the context of a test
public class RedisBackedCacheTest {
@Rule
public GenericContainer redis =
new GenericContainer("redis:3.0.6").withExposedPorts(6379);
@Before
public void setUp() throws Exception {
Jedis jedis = new Jedis(redis.getIpAddress(),redis.getMappedPort(6379));
cache = new RedisBackedCache(jedis, "test");
}
@Test
public void cacheTest(){
//Test using cache connected to a real Redis database
}
}
https://rnorth.org/junit-integration-testing-with-docker-and-testcontainers
Docker for tests
Testing fácil con Docker · @micael_gallego
Docker
TestContainers
●
How to start a Web Browser in the context of a test
public class SeleniumContainerTest {
@Rule
public BrowserWebDriverContainer chrome = new BrowserWebDriverContainer()
.withDesiredCapabilities(DesiredCapabilities.chrome())
.withRecordingMode(RECORD_ALL, new File("target"));
@Test
public void simplePlainSeleniumTest() {
RemoteWebDriver driver = chrome.getWebDriver();
System.out.println("Browser VNC URL: "+chrome.getVncAddress());
driver.get("https://wikipedia.org");
WebElement searchInput = driver.findElementByName("search");
//some asserts...
}
}
https://rnorth.org/better-junit-selenium-testing-with-docker-and-testcontainers
Docker for tests
Testing fácil con Docker · @micael_gallego
Docker
TestContainers
●
Execute sample code
$ git clone https://github.com/testcontainers/testcontainers-java-examples
$ cd cd testcontainers-java-examples
$ cd redis-backed-cache/
$ mvn test
//DDBB Tests...
$ cd ../selenium-container
$ mvn test
//Browser Tests...
Docker for tests
Video recorded in
/testcontainers-java-examples/selenium-container/target/recording*.flv
Testing fácil con Docker · @micael_gallego
Docker
TestContainers
●
You can execute “mvn test” inside a maven container
●
ButTestContainers lib needs create new containers
●
To allow a container to create new containers in the host,
the file /var/run/docker.sock have to be mounted as
volume
$ docker run --rm -v "$PWD":/data 
-v /var/run/docker.sock:/var/run/docker.sock 
-w /data maven mvn test
Docker for tests
Testing fácil con Docker · @micael_gallego
Docker
TestContainers
●
ExecutingTestContainers tests in Jenkins
Docker for tests
node {
stage ('Checkout') {
git url: 'https://github.com/testcontainers/testcontainers-java-examples'
}
stage ('Build') {
docker.image('maven').inside('-v $HOME/.m2:/root/.m2 '+
'-v /var/run/docker.sock:/var/run/docker.sock') {
sh 'cd redis-backed-cache; mvn test'
}
step([$class: 'JUnitResultArchiver',
testResults: '**/target/surefire-reports/TEST-*.xml'])
}
}
Testing fácil con Docker · @micael_gallego
Docker
Dockerized Jenkins
●
Can we execute do this?
●
CI Server only need Docker installed. All software is
managed using containers
Docker for tests
Jenkins
Maven
Tests
(TestContainers)
Web
Browser
Docker Containers
Testing fácil con Docker · @micael_gallego
Docker
Dockerized Jenkins
●
It is possible, but official Jenkins image doesn’t allow it
●
Jenkins container should have docker client installed
●
Image trion/jenkins-docker-client is based on official
jenkins image and install docker client
Docker for tests
docker run -it -e JENKINS_USER=$(id -u) 
--rm -p 8080:8080 
-v $HOME/.jenkins:/var/jenkins_home 
-v /var/run/docker.sock:/var/run/docker.sock 
--name jenkins trion/jenkins-docker-client
Testing fácil con Docker · @micael_gallego
● Conclusions
– Docker containers are changing the way we
develop, build, test and ship software
– Containers allow developers to use the same dev
tools and execute the project in the same
environment
– Containers ease the configuration and share of CI
servers
– If Continuous Integration is easier to use, more
projects will use it and more test will be executed
Docker in CI servers
Testing fácil con Docker · @micael_gallego
Thanks!
Do you have any
question?

More Related Content

What's hot

Eclipse DemoCamp Bucharest 2014 - Continuous Integration Jenkins/Hudson
Eclipse DemoCamp Bucharest 2014 - Continuous Integration Jenkins/HudsonEclipse DemoCamp Bucharest 2014 - Continuous Integration Jenkins/Hudson
Eclipse DemoCamp Bucharest 2014 - Continuous Integration Jenkins/Hudson
VladLica
 
Continuous Delivery: 5 years later (Incontro DevOps 2018)
Continuous Delivery: 5 years later (Incontro DevOps 2018)Continuous Delivery: 5 years later (Incontro DevOps 2018)
Continuous Delivery: 5 years later (Incontro DevOps 2018)
Giovanni Toraldo
 

What's hot (20)

Open Source tools overview
Open Source tools overviewOpen Source tools overview
Open Source tools overview
 
Performance Profiling Tools and Tricks
Performance Profiling Tools and TricksPerformance Profiling Tools and Tricks
Performance Profiling Tools and Tricks
 
Jenkins 101: Continuos Integration with Jenkins
Jenkins 101: Continuos Integration with JenkinsJenkins 101: Continuos Integration with Jenkins
Jenkins 101: Continuos Integration with Jenkins
 
Voxxed Luxembourd 2016 Jenkins 2.0 et Pipeline as code
Voxxed Luxembourd 2016 Jenkins 2.0 et Pipeline as codeVoxxed Luxembourd 2016 Jenkins 2.0 et Pipeline as code
Voxxed Luxembourd 2016 Jenkins 2.0 et Pipeline as code
 
NetBeans Support for EcmaScript 6
NetBeans Support for EcmaScript 6NetBeans Support for EcmaScript 6
NetBeans Support for EcmaScript 6
 
Bgoug 2019.11 test your pl sql - not your patience
Bgoug 2019.11   test your pl sql - not your patienceBgoug 2019.11   test your pl sql - not your patience
Bgoug 2019.11 test your pl sql - not your patience
 
Source Control with Domino Designer 8.5.3 and Git (DanNotes, November 28, 2012)
Source Control with Domino Designer 8.5.3 and Git (DanNotes, November 28, 2012)Source Control with Domino Designer 8.5.3 and Git (DanNotes, November 28, 2012)
Source Control with Domino Designer 8.5.3 and Git (DanNotes, November 28, 2012)
 
Jenkins Shared Libraries Workshop
Jenkins Shared Libraries WorkshopJenkins Shared Libraries Workshop
Jenkins Shared Libraries Workshop
 
OpenDaylight Developer Experience 2.0
 OpenDaylight Developer Experience 2.0 OpenDaylight Developer Experience 2.0
OpenDaylight Developer Experience 2.0
 
Eclipse DemoCamp Bucharest 2014 - Continuous Integration Jenkins/Hudson
Eclipse DemoCamp Bucharest 2014 - Continuous Integration Jenkins/HudsonEclipse DemoCamp Bucharest 2014 - Continuous Integration Jenkins/Hudson
Eclipse DemoCamp Bucharest 2014 - Continuous Integration Jenkins/Hudson
 
Using NuGet the way you should - TechDays NL 2014
Using NuGet the way you should - TechDays NL 2014Using NuGet the way you should - TechDays NL 2014
Using NuGet the way you should - TechDays NL 2014
 
Podman, Buildah, and Quarkus - The Latest in Linux Containers Technologies
Podman, Buildah, and Quarkus - The Latest in Linux Containers Technologies Podman, Buildah, and Quarkus - The Latest in Linux Containers Technologies
Podman, Buildah, and Quarkus - The Latest in Linux Containers Technologies
 
PIC your malware
PIC your malwarePIC your malware
PIC your malware
 
Apache Maven
Apache MavenApache Maven
Apache Maven
 
Building Good Containers for Python Applications
Building Good Containers for Python ApplicationsBuilding Good Containers for Python Applications
Building Good Containers for Python Applications
 
Maven
MavenMaven
Maven
 
An Introduction to Gradle for Java Developers
An Introduction to Gradle for Java DevelopersAn Introduction to Gradle for Java Developers
An Introduction to Gradle for Java Developers
 
Managing dependencies with gradle
Managing dependencies with gradleManaging dependencies with gradle
Managing dependencies with gradle
 
Continuous Delivery: 5 years later (Incontro DevOps 2018)
Continuous Delivery: 5 years later (Incontro DevOps 2018)Continuous Delivery: 5 years later (Incontro DevOps 2018)
Continuous Delivery: 5 years later (Incontro DevOps 2018)
 
Git for IBM Notes Designer
Git for IBM Notes DesignerGit for IBM Notes Designer
Git for IBM Notes Designer
 

Similar to Testing fácil con Docker: Gestiona dependencias y unifica entornos

Similar to Testing fácil con Docker: Gestiona dependencias y unifica entornos (20)

ExpoQA 2017 Using docker to build and test in your laptop and Jenkins
ExpoQA 2017 Using docker to build and test in your laptop and JenkinsExpoQA 2017 Using docker to build and test in your laptop and Jenkins
ExpoQA 2017 Using docker to build and test in your laptop and Jenkins
 
Magento Docker Setup.pdf
Magento Docker Setup.pdfMagento Docker Setup.pdf
Magento Docker Setup.pdf
 
Docker on Power Systems
Docker on Power SystemsDocker on Power Systems
Docker on Power Systems
 
Run automated tests in Docker
Run automated tests in DockerRun automated tests in Docker
Run automated tests in Docker
 
Docker for .NET Developers
Docker for .NET DevelopersDocker for .NET Developers
Docker for .NET Developers
 
Docker up and Running For Web Developers
Docker up and Running For Web DevelopersDocker up and Running For Web Developers
Docker up and Running For Web Developers
 
Docker Up and Running for Web Developers
Docker Up and Running for Web DevelopersDocker Up and Running for Web Developers
Docker Up and Running for Web Developers
 
Docker - A curtain raiser to the Container world
Docker - A curtain raiser to the Container worldDocker - A curtain raiser to the Container world
Docker - A curtain raiser to the Container world
 
Docker for developers
Docker for developersDocker for developers
Docker for developers
 
JOSA TechTalk: Introduction to docker
JOSA TechTalk: Introduction to dockerJOSA TechTalk: Introduction to docker
JOSA TechTalk: Introduction to docker
 
Introduction to Docker for NodeJs developers at Node DC 2/26/2014
Introduction to Docker for NodeJs developers at Node DC 2/26/2014Introduction to Docker for NodeJs developers at Node DC 2/26/2014
Introduction to Docker for NodeJs developers at Node DC 2/26/2014
 
Adventures in docker compose
Adventures in docker composeAdventures in docker compose
Adventures in docker compose
 
Introduction to Docker - Vellore Institute of Technology
Introduction to Docker - Vellore Institute of TechnologyIntroduction to Docker - Vellore Institute of Technology
Introduction to Docker - Vellore Institute of Technology
 
Introduction to Docker - VIT Campus
Introduction to Docker - VIT CampusIntroduction to Docker - VIT Campus
Introduction to Docker - VIT Campus
 
Puppet Camp Seattle 2014: Docker and Puppet: 1+1=3
Puppet Camp Seattle 2014: Docker and Puppet: 1+1=3 Puppet Camp Seattle 2014: Docker and Puppet: 1+1=3
Puppet Camp Seattle 2014: Docker and Puppet: 1+1=3
 
DOCKER-PIAIC-SLIDES
DOCKER-PIAIC-SLIDESDOCKER-PIAIC-SLIDES
DOCKER-PIAIC-SLIDES
 
Real-World Docker: 10 Things We've Learned
Real-World Docker: 10 Things We've Learned  Real-World Docker: 10 Things We've Learned
Real-World Docker: 10 Things We've Learned
 
[@NaukriEngineering] Docker 101
[@NaukriEngineering] Docker 101[@NaukriEngineering] Docker 101
[@NaukriEngineering] Docker 101
 
Introduction to Docker and Monitoring with InfluxData
Introduction to Docker and Monitoring with InfluxDataIntroduction to Docker and Monitoring with InfluxData
Introduction to Docker and Monitoring with InfluxData
 
Dockerizing a Symfony2 application
Dockerizing a Symfony2 applicationDockerizing a Symfony2 application
Dockerizing a Symfony2 application
 

More from Micael Gallego

More from Micael Gallego (20)

Software libre para videoconferencias
Software libre para videoconferenciasSoftware libre para videoconferencias
Software libre para videoconferencias
 
La evaluación con realimentación y posibilidad de recuperación para evitar el...
La evaluación con realimentación y posibilidad de recuperación para evitar el...La evaluación con realimentación y posibilidad de recuperación para evitar el...
La evaluación con realimentación y posibilidad de recuperación para evitar el...
 
WebRTC en tu web con OpenVidu
WebRTC en tu web con OpenViduWebRTC en tu web con OpenVidu
WebRTC en tu web con OpenVidu
 
¿Cómo poner software de calidad en manos del usuario de forma rápida?
¿Cómo poner software de calidad en manos del usuario de forma rápida?¿Cómo poner software de calidad en manos del usuario de forma rápida?
¿Cómo poner software de calidad en manos del usuario de forma rápida?
 
Curso Angular 9 - CodeURJC - Marzo 2020
Curso Angular 9 - CodeURJC - Marzo 2020Curso Angular 9 - CodeURJC - Marzo 2020
Curso Angular 9 - CodeURJC - Marzo 2020
 
Concurrencia y asincronía: Lenguajes, modelos y rendimiento: GDG Toledo Enero...
Concurrencia y asincronía: Lenguajes, modelos y rendimiento: GDG Toledo Enero...Concurrencia y asincronía: Lenguajes, modelos y rendimiento: GDG Toledo Enero...
Concurrencia y asincronía: Lenguajes, modelos y rendimiento: GDG Toledo Enero...
 
Herramientas y plugins para el desarrollo de aplicaciones cloud native para K...
Herramientas y plugins para el desarrollo de aplicaciones cloud native para K...Herramientas y plugins para el desarrollo de aplicaciones cloud native para K...
Herramientas y plugins para el desarrollo de aplicaciones cloud native para K...
 
Dev Tools para Kubernetes - Codemotion 2019
Dev Tools para Kubernetes - Codemotion 2019Dev Tools para Kubernetes - Codemotion 2019
Dev Tools para Kubernetes - Codemotion 2019
 
Curso Kubernetes CodeURJC
Curso Kubernetes CodeURJCCurso Kubernetes CodeURJC
Curso Kubernetes CodeURJC
 
Testeando aplicaciones Kubernetes: escalabilidad y tolerancia a fallos
Testeando aplicaciones Kubernetes: escalabilidad y tolerancia a fallosTesteando aplicaciones Kubernetes: escalabilidad y tolerancia a fallos
Testeando aplicaciones Kubernetes: escalabilidad y tolerancia a fallos
 
OpenVidu Commitconf 2018
OpenVidu Commitconf 2018 OpenVidu Commitconf 2018
OpenVidu Commitconf 2018
 
Introducción a las Pruebas Software
Introducción a las Pruebas SoftwareIntroducción a las Pruebas Software
Introducción a las Pruebas Software
 
Node para Javeros: Conoce a tu enemigo
Node para Javeros: Conoce a tu enemigoNode para Javeros: Conoce a tu enemigo
Node para Javeros: Conoce a tu enemigo
 
Desarrollo centrado en tareas en Eclipse con Mylyn 2009
Desarrollo centrado en tareas en Eclipse con Mylyn 2009Desarrollo centrado en tareas en Eclipse con Mylyn 2009
Desarrollo centrado en tareas en Eclipse con Mylyn 2009
 
Como ser mas productivo en el desarrollo de aplicaciones
Como ser mas productivo en el desarrollo de aplicacionesComo ser mas productivo en el desarrollo de aplicaciones
Como ser mas productivo en el desarrollo de aplicaciones
 
TypeScript: Un lenguaje aburrido para programadores torpes y tristes
TypeScript: Un lenguaje aburrido para programadores torpes y tristesTypeScript: Un lenguaje aburrido para programadores torpes y tristes
TypeScript: Un lenguaje aburrido para programadores torpes y tristes
 
Docker para Data Scientist - Master en Data Science URJC
Docker para Data Scientist - Master en Data Science URJCDocker para Data Scientist - Master en Data Science URJC
Docker para Data Scientist - Master en Data Science URJC
 
El Aprendizaje Basado en Proyectos y la Clase Invertida para acercar el mundo...
El Aprendizaje Basado en Proyectos y la Clase Invertida para acercar el mundo...El Aprendizaje Basado en Proyectos y la Clase Invertida para acercar el mundo...
El Aprendizaje Basado en Proyectos y la Clase Invertida para acercar el mundo...
 
El mundo real en el aula, con la ayuda del profesor
El mundo real en el aula, con la ayuda del profesorEl mundo real en el aula, con la ayuda del profesor
El mundo real en el aula, con la ayuda del profesor
 
Angular 2 Campus Madrid Septiembre 2016
Angular 2 Campus Madrid Septiembre 2016Angular 2 Campus Madrid Septiembre 2016
Angular 2 Campus Madrid Septiembre 2016
 

Recently uploaded

CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
Health
 

Recently uploaded (20)

CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
How to Choose the Right Laravel Development Partner in New York City_compress...
How to Choose the Right Laravel Development Partner in New York City_compress...How to Choose the Right Laravel Development Partner in New York City_compress...
How to Choose the Right Laravel Development Partner in New York City_compress...
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
 
10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
 

Testing fácil con Docker: Gestiona dependencias y unifica entornos

  • 1. Testing fácil con Docker: Gestiona dependencias y unifica entornos Micael Gallego (CodeURJC) @micael_gallego MAD · NOV 24-25 · 2017
  • 2. Testing fácil con Docker · @micael_gallego @micael_gallego micael.gallego@urjc.es @micaelgallego About me developer University professor Trainer & Consultor
  • 3. Testing fácil con Docker · @micael_gallego Consultancy / Training Cloud Computing WebTechnologies Extreme Programming Testing / Git / Jenkins Software Architecture Concurrent Programming Open source elastic platform for end to end testing http://codeurjc.es http://elastest.io Advanced log management Test orchestration Test execution comparison Web and Mobile testing Security testing IoT testing Performance testing
  • 4. Testing fácil con Docker · @micael_gallego Virtualizacion and Containers ● Developers want to reduce the differences between local, continuous integration and production environments ● Avoiding “It works in my machine” type of problems Virtualization Containers
  • 5. Testing fácil con Docker · @micael_gallego Virtualizacion and Containers ● Virtualization – Full fledged Virtual Machine (VirtualBox) – Developer friendly managed VM (Vagrant) ● Containers – Docker
  • 6. Testing fácil con Docker · @micael_gallego
  • 7. Testing fácil con Docker · @micael_gallego VirtualBox ● Developed by Oracle (was owned by Sun Microsystems) ● Mostly open source, several free (but closed source) modules ● Windows, Linux and Mac versions ● Advanced Desktop Virtualization – Shared folders host / guest – Keyboard and mouse advanced integration – Graphic 3D acceleration – Webcam https://www.virtualbox.org/
  • 8. Testing fácil con Docker · @micael_gallego VirtualBox
  • 9. Testing fácil con Docker · @micael_gallego VirtualBox ● Manual – Create an empty virtual machine – Connect to a ISO (simulating real CD devide) – Install a full fledged Operating System – It is time consuming and it is not easy to share VM between developers
  • 10. Testing fácil con Docker · @micael_gallego VirtualBox
  • 11. Testing fácil con Docker · @micael_gallego VirtualBox
  • 12. Testing fácil con Docker · @micael_gallego
  • 13. Testing fácil con Docker · @micael_gallego Vagrant ● It is a command line utility to manage VMs ● It makes very easy to download and start a new VM (only with a command) ● Allows to provisioning the new VM with command provisioning tools (script, chef, puppet, ansible…) ● VM configuration is specified in a text file, allowing to share it in the git repository https://www.vagrantup.com/
  • 14. Testing fácil con Docker · @micael_gallego Vagrant ● How to create a new VM with ubuntu Xenial ● Vagrant manages certificates and networking to make easy to connect to the new VM ● By default, working dir is shared with VM $ vagrant init ubuntu/xenial64 $ vagrant up $ vagrant ssh
  • 15. Testing fácil con Docker · @micael_gallego containers https://www.docker.com/
  • 16. Testing fácil con Docker · @micael_gallego Docker ● With VMs you can have the production environment in your laptop ● But… – VMs takes minutes to start up – VMs use (waste?) a lot of resources (memory and disk space)
  • 17. Testing fácil con Docker · @micael_gallego Docker ● In a first look, containers can be considered as “lightweight VMs” – They contain an isolated environment to run apps – Start in milliseconds – They use only the resources it needs – A container doesn't have a full fledged operating system, only the minimal software to execute apps
  • 18. Testing fácil con Docker · @micael_gallego Docker
  • 19. Testing fácil con Docker · @micael_gallego Docker ● Containers and VMs are very different Virtual Machines Containers Heavier Lighter Execute several processes per Virtual Machine Usually execute only one process per container Ssh connection Direct execution in the container (rarely needed) More isolated using hypervisor Less isolated because are executed using kernel features Can virtualizeWindows over Linux Linux containers must be executed in linux hosts* * More on that later
  • 20. Testing fácil con Docker · @micael_gallego Docker ● To install an application in a linux system you need all dependencies installed ● Can be incompatibilities between applications that need different version of the same dependency ● Docker include in a container all needed software isolated to the rest of the system
  • 21. Testing fácil con Docker · @micael_gallego Docker
  • 22. Testing fácil con Docker · @micael_gallego Docker ● Docker containers SO support – Linux containers ● Very mature technology ● It can be used in any* linux distribution – Windows containers ● Preliminary technology ● It only can be used in a very recent** Windows Server version * Kernel version 3.10 or greather. Published in June 2013 **Windows Server 2016 (Core and with Desktop Experience), Nano Server, andWindows 10 Professional and Enterprise (Anniversary Edition).
  • 23. Testing fácil con Docker · @micael_gallego Docker ● You can execute linux containers in any operating system ● It uses virtualization (under the covers) in Mac and Windows
  • 24. Testing fácil con Docker · @micael_gallego Docker ● Docker Toolbox for Mac and Windows – It uses VirtualBox as virtualization – It is not the same development experience than in linux ● Docker for Mac and Windows – Uses native virtualization technology in each operating system – Only available in new versions of that SOs
  • 25. Testing fácil con Docker · @micael_gallego Docker Docker concepts
  • 26. Testing fácil con Docker · @micael_gallego •Docker Image – Basic template for a container (hard disk of VM) – It contains SO (ubuntu), libs (Java) and app (webapp.jar) – A container always is started from an image – If you want to start a new container from an image that is not in your system, it is automatically downloaded from Internet Docker concepts
  • 27. Testing fácil con Docker · @micael_gallego •Docker Registry ● Remote service used to store and retrive docker images ● It can hold several versions of the same image ● All versions of the same image are located in the same repository (like in git) ● Docker Hub is a public registry managed by Docker Inc. ● You can buy private repositories in Docker Hub ● You can also operate your own private registry Docker concepts
  • 28. Testing fácil con Docker · @micael_gallego •Popular repositories in Docker Hub Docker concepts
  • 29. Testing fácil con Docker · @micael_gallego •Docker Container – It is the “equivalent” of a Virtual Machine – A container is created from a docker image – When a file is wrote, the image it is not modified, the container is modified – It can be started, paused or stopped Docker concepts
  • 30. Testing fácil con Docker · @micael_gallego •Docker Engine – Local service used to control docker – Manages images (download, create, pull, push…) – Manages containers (start, stop, commit...) – It can be used with the docker client or using its REST API Docker concepts
  • 31. Testing fácil con Docker · @micael_gallego •Docker client – Command line interface (CLI) tool to control docker engine – It is available when docker is installed in a system to connect to their local docker engine Docker concepts
  • 32. Testing fácil con Docker · @micael_gallego •Official documentation – https://docs.docker.com/ •Advanced tutorials – https://docs.docker.com/engine/tutorials/dockerizing/ – https://docs.docker.com/engine/tutorials/usingdocker/ – https://docs.docker.com/engine/tutorials/dockerimages/ – https://docs.docker.com/engine/tutorials/networkingcontainers/ – https://docs.docker.com/engine/tutorials/dockervolumes/ – https://docs.docker.com/engine/tutorials/dockerrepos/ ● Cheat Sheet – https://github.com/wsargent/docker-cheat-sheet/blob/master/README.md Docker documentation
  • 33. Testing fácil con Docker · @micael_gallego First steps with docker Install Docker – Windows: ● Microsoft Windows 10 Professional or Enterprise 64-bit: https://store.docker.com/editions/community/docker-ce-desktop-windows ● Other Windows versions: https://www.docker.com/products/docker-toolbox – Linux: ● Ubuntu: https://store.docker.com/editions/community/docker-ce-server-ubuntu ● Fedora: https://store.docker.com/editions/community/docker-ce-server-fedora ● Debian: https://store.docker.com/editions/community/docker-ce-server-debian ● CentOS: https://store.docker.com/editions/community/docker-ce-server-centos – Mac: ● Apple Mac OS Yosemite 10.10.3 or above: https://store.docker.com/editions/community/docker-ce-desktop-mac ● Older Mac: https://www.docker.com/products/docker-toolbox
  • 34. Testing fácil con Docker · @micael_gallego First steps with docker Hands on… https://github.com/docker/labs/tree/master/beginner
  • 35. Testing fácil con Docker · @micael_gallego First steps with docker Testing if docker is correctly installed $ docker run hello-world Unable to find image 'hello-world:latest' locally latest: Pulling from library/hello-world 03f4658f8b78: Pull complete a3ed95caeb02: Pull complete Digest: sha256:8be990ef2aeb16dbcb9271ddfe2610fa6658d13f6dfb8bc72074cc1ca369 66a7 Status: Downloaded newer image for hello-world:latest Hello from Docker. This message shows that your installation appears to be working correctly. ...
  • 36. Testing fácil con Docker · @micael_gallego First steps with docker Running your first container $ docker run alpine ls -l total 48 drwxr-xr-x 2 root root 4096 Mar 2 16:20 bin drwxr-xr-x 5 root root 360 Mar 18 09:47 dev drwxr-xr-x 13 root root 4096 Mar 18 09:47 etc drwxr-xr-x 2 root root 4096 Mar 2 16:20 home drwxr-xr-x 5 root root 4096 Mar 2 16:20 lib ...... ......
  • 37. Testing fácil con Docker · @micael_gallego First steps with docker Running your first container $ docker run alpine ls -l Command “run” Creates a new container and start it
  • 38. Testing fácil con Docker · @micael_gallego First steps with docker Running your first container $ docker run alpine ls -l Image name alpine is a minimal linux system (4.8Mb).The image is downloaded if not stored in local machine
  • 39. Testing fácil con Docker · @micael_gallego First steps with docker Running your first container $ docker run alpine ls -l Command “ls -l” This command will be executed inside the running container
  • 40. Testing fácil con Docker · @micael_gallego First steps with docker Inspecting the downloaded images $ docker images REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE alpine latest c51f86c28340 4 weeks ago 1.109 MB hello-world latest 690ed74de00f 5 months ago 960 B List all images stored in the system
  • 41. Testing fácil con Docker · @micael_gallego First steps with docker Executing a container $ docker run alpine echo "hello from alpine" hello from alpine Execute the command “echo” inside the container
  • 42. Testing fácil con Docker · @micael_gallego First steps with docker Inspecting containers (executing) $ docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a6a9d46d0b2f alpine "echo 'hello from alp" 6 minutes ago Exited (0) 6 minutes ago lonely_kilby ff0a5c3750b9 alpine "ls -l" 8 minutes ago Exited (0) 8 minutes ago elated_ramanujan c317d0a9e3d2 hello-world "/hello" 34 seconds ago Exited (0) 12 minutes ago stupefied_mcclintock It shows containers in the system. All of them has STATUS Exited.These containers are not currently executing (but using disk space)
  • 43. Testing fácil con Docker · @micael_gallego First steps with docker Interactive commands in containers $ docker run -it alpine /bin/sh / # ls bin dev etc home lib linuxrc media mnt proc root run sbin sys tmp usr var / # uname -a Linux 97916e8cb5dc 4.4.27-moby #1 SMP Wed Oct 26 14:01:48 UTC 2016 x86_64 Linux / # exit $ To execute an interactive command it is necessary to use the option “-it” to connect the console to the container command
  • 44. Testing fácil con Docker · @micael_gallego First steps with docker ● Interactive commands in containers – When you execute a /bin/sh command in a container it offers a “similar” experience than a ssh connection – Buy there are no ssh server neither ssh client – It is executing a shell inside the container
  • 45. Testing fácil con Docker · @micael_gallego First steps with docker ● Managing containers lifecycle $ docker run -d seqvence/static-site Option “-d” Executes the container in background
  • 46. Testing fácil con Docker · @micael_gallego First steps with docker ● Managing containers lifecycle $ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a7a0e504ca3e seqvence/static-site "/bin/sh -c 'cd /usr/" 28 seconds ago Up 26 seconds Container id is a7a0e504ca3e This id is used to refer to this container STATUS is UP
  • 47. Testing fácil con Docker · @micael_gallego First steps with docker ● Managing containers lifecycle – Stop running container – Delete files of the stopped container $ docker stop a7a0e504ca3e $ docker rm a7a0e504ca3e
  • 48. Testing fácil con Docker · @micael_gallego Net services with docker ● Start container exposing a port docker run --name static-site -e AUTHOR="Your Name" -d -p 9000:80 seqvence/static-site
  • 49. Testing fácil con Docker · @micael_gallego Net services with docker ● Start container exposing a port docker run --name static-site -e AUTHOR="Your Name" -d -p 9000:80 seqvence/static-site --name static-site Specifies a unique name for the container
  • 50. Testing fácil con Docker · @micael_gallego Net services with docker docker run --name static-site -e AUTHOR="Your Name" -d -p 9000:80 seqvence/static-site -e AUTHOR="Your Name" Set the environment variable AUTHOR to value “Your Name” ● Start container exposing a port
  • 51. Testing fácil con Docker · @micael_gallego Net services with docker docker run --name static-site -e AUTHOR="Your Name" -d -p 9000:80 seqvence/static-site -d Execute container as deamon ● Start container exposing a port
  • 52. Testing fácil con Docker · @micael_gallego Net services with docker docker run --name static-site -e AUTHOR="Your Name" -d -p 9000:80 seqvence/static-site -p 9000:80 Connects the host port 9000 to the port 80 in the container ● Start container exposing a port
  • 53. Testing fácil con Docker · @micael_gallego Net services with docker ● Use the service – Open http://127.0.0.1:9000 in a browser in your host to access 80 port in container
  • 54. Testing fácil con Docker · @micael_gallego Net services with docker ● Use the service – If you are using Docker Toolbox for Mac or Windows you can’t use 127.0.0.1 IP – Then you have to open http://192.168.99.100:9000/ in the browser $ docker-machine ip default 192.168.99.100
  • 55. Testing fácil con Docker · @micael_gallego Net services with docker ● Container management – Stop and remove the container – Stop and remove a running container – Remove all running containers $ docker rm -f static-site $ docker stop static-site $ docker rm static-site $ docker rm -f $(docker ps -a -q)
  • 56. Testing fácil con Docker · @micael_gallego Managing docker images ● List images in host Tag is like “version”. Latest is… the latest ;) $ docker images REPOSITORY TAG IMAGE ID CREATED SIZE seqvence/static-site latest 92a386b6e686 2 hours ago 190.5 MB nginx latest af4b3d7d5401 3 hours ago 190.5 MB python 2.7 1c32174fd534 14 hours ago 676.8 MB postgres 9.4 88d845ac7a88 14 hours ago 263.6 MB Containous/traefik latest 27b4e0c6b2fd 4 days ago 20.75 MB ...
  • 57. Testing fácil con Docker · @micael_gallego Managing docker images ● Managing versions – Download a concrete version – Download latest version $ docker pull ubuntu:12.04 $ docker pull ubuntu
  • 58. Testing fácil con Docker · @micael_gallego Managing docker images ● Searching images in DockerHub
  • 59. Testing fácil con Docker · @micael_gallego Managing docker images ● Searching images in Docker Store
  • 60. Testing fácil con Docker · @micael_gallego Managing docker images ● Image types – Base images ● Images without a parent image ● Examples: Ubuntu, debian, alpine… ● Used by Operating Systems – Child images ● Base images plus some additional software ● Examples: Nginx, Apache, MySQL...
  • 61. Testing fácil con Docker · @micael_gallego Managing docker images ● Official vs User images – Official images ● Images created by trusted companies or communities – User images ● Any user can create an account and upload her own images
  • 62. Testing fácil con Docker · @micael_gallego Managing docker images ● Create your first docker image – We will create a web application for display random cat pics using Python – Clone following repository and go to project folder $ git clone https://github.com/codeurjc/testing-docker $ cd web-python
  • 63. Testing fácil con Docker · @micael_gallego Managing docker images ● Create your first image – You have all source files for the web application – But you need Python and Flask to execute the app – To execute the web application, you will create a new image with dependencies (Python and Flask) and your application code – Then you can create a new container to execute your application
  • 64. Testing fácil con Docker · @micael_gallego Managing docker images ● Dockerfile – File used to describe a new image – Specifies ● Base image ● Commands to execute in the image ● Files to include in the image from the project folder ● Open ports ● Command to execute when start the image
  • 65. Testing fácil con Docker · @micael_gallego # Select base image FROM alpine:latest # Install python and pip RUN apk add ­­update py­pip RUN pip install ­­upgrade pip # copy app files COPY app.py /usr/src/app/ COPY templates/index.html /usr/src/app/templates/ COPY requirements.txt /usr/src/app/ # install Python modules needed by the app RUN pip install ­­no­cache­dir ­r      /usr/src/app/requirements.txt # tell the port number the container should expose EXPOSE 5000 # run the application CMD ["python", "/usr/src/app/app.py"]
  • 66. Testing fácil con Docker · @micael_gallego Managing docker images ● Dockerfile – FROM: Base image – COPY: Copy files from Dockerfile folder – RUN execute commands – EXPOSE: Public ports – CMD: Command to execute when container is started https://docs.docker.com/engine/userguide/eng- image/dockerfile_best-practices/
  • 67. Testing fácil con Docker · @micael_gallego Managing docker images ● Build the image – In the folder with a Dockerfile execute – Executed actions ● Create a new container with base image ● Execute commands and copy app files ● Create a new container with the result $ docker build -t webapp .
  • 68. Testing fácil con Docker · @micael_gallego Managing docker images ● Run the new image – Open http://127.0.0.1:9000/ in the browser – Windows and Mac users with Toolbox use the IP $ docker run -p 9000:5000 webapp * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
  • 69. Testing fácil con Docker · @micael_gallego Managing docker images
  • 70. Testing fácil con Docker · @micael_gallego Managing docker images ● Build the image again – Change some HTML in templatesindex.html – Create the image again – The Dockerfile steps without changes are not re- executed (are reused from previous execution) – The image is created very quickly because only the files copy is perfomed $ docker build -t webapp . $ docker run -p 9000:5000 webapp
  • 71. Testing fácil con Docker · @micael_gallego Volumes ● Volumes – Allow sharing files between host and container – Execute a container to show an nonexistent file – Create a text file $ docker run alpine cat /data/file.txt cat: can't open '/data/file.txt': No such file or directory $ echo "My file" >> file.txt
  • 72. Testing fácil con Docker · @micael_gallego Volumes Volumes ● Mount a host folder inside a container folder ● Host contents replace container contents of that folder ● Containers can write files in volumes to be available in the host $ sudo docker run -v "$PWD":/data alpine cat /data/file.txt My file
  • 73. Testing fácil con Docker · @micael_gallego Volumes ● Volumes – Docker images use volumes to read files from host – Official NGINX container can serve host files using http ● Serving current folder files ($PWD) ● Go to http://127.0.0.1:9000/file.txt https://hub.docker.com/_/nginx/ $ docker run -p 9000:80 -v "$PWD":/usr/share/nginx/html:ro -d nginx
  • 74. Testing fácil con Docker · @micael_gallego Volumes ● Volumes – Docker Toolbox for Win or Mac only allow folders inside user folder to be used as volume – You can use other folders but have to configure shared folders in VirtualBox https://hub.docker.com/_/nginx/
  • 75. Testing fácil con Docker · @micael_gallego Volumes ● Developing web app with volumes – If you mount your code folder in a container, when you edit a file, it is available to the container automatically – It is not necessary to recreate the image in each edition $ docker run -v "$PWD":/usr/src/app/ -p 9000:5000 webapp
  • 76. Testing fácil con Docker · @micael_gallego •Containers main use cases – Net service ● Executed in background long time... ● Used through network ● Ex: Databases, web servers... – Command ● Execute a single command and stop ● Read and write files from host with volumes ● Ex: Java Compiler, jekyll, ffmpeg... Docker container usage
  • 77. Testing fácil con Docker · @micael_gallego •Docker for building software – A container can have all needed environment to execute a developer tool – For example, you can have the compiler and the test dependencies in a container – You can clone a git repository and execute the (dockerized) compiler without install any software in your host Docker for software developers
  • 78. Testing fácil con Docker · @micael_gallego •Dockerized Java Maven – Clone a maven repo – Compile and exec tests Docker for software developers $ git clone https://github.com/codeurjc/testing-docker/ $ cd testing-docker/basic-java-tests $ docker run --rm -v "$PWD":/data -w /data maven mvn package
  • 79. Testing fácil con Docker · @micael_gallego •Dockerized Java Maven Docker for software developers $ docker run --rm -v "$PWD":/data -w /data maven mvn package https://hub.docker.com/_/maven/ --rm Remove container when execution finish
  • 80. Testing fácil con Docker · @micael_gallego •Dockerized Java Maven Docker for software developers $ docker run --rm -v "$PWD":/data -w /data maven mvn package https://hub.docker.com/_/maven/ --w Working dir for the command
  • 81. Testing fácil con Docker · @micael_gallego •Dockerized Java Maven Docker for software developers $ docker run --rm -v "$PWD":/data -w /data maven mvn package https://hub.docker.com/_/maven/ maven Official Maven image
  • 82. Testing fácil con Docker · @micael_gallego •Dockerized Java Maven Docker for software developers $ docker run --rm -v "$PWD":/data -w /data maven mvn package https://hub.docker.com/_/maven/ mvn package Maven command
  • 83. Testing fácil con Docker · @micael_gallego •Dockerized Java Maven – Jar package is generated in /target folder in host – As container command is executed as root user (by default), generated files are owned by root. – Change to your user Docker for software developers simple-maven-project-with-tests-1.0-SNAPSHOT.jar https://hub.docker.com/_/maven/ sudo chown -R username:group target
  • 84. Testing fácil con Docker · @micael_gallego •Dockerized Node tests – Clone a test repo Compile and exec tests Docker for software developers $ git clone https://github.com/codeurjc/testing-docker/ $ cd testing-docker/basic-js-tests $ docker run --rm -v "$PWD":/data -w /data node:9-alpine npm install; npm test
  • 85. Testing fácil con Docker · @micael_gallego •Advantages of dockerized dev tools – Avoid several developers having different versions of such tools – It is very easy to test the same code in different versions (Java 7, Java 8...) – Reduce tools configuration problems.You can compile and execute a project easily – The same tools can be executed in development laptops and also in CI environment Docker for software developers
  • 86. Testing fácil con Docker · @micael_gallego Docker for software developers
  • 87. Testing fácil con Docker · @micael_gallego •Docker in Continuous Integration – If you execute dev tools in containers, it is very easy to compile, test and package in CI environment – Only have to execute the same command in laptop and CI environment – If a tool changes, only have to change the command, it is not necessary to install anything Docker in CI servers
  • 88. Testing fácil con Docker · @micael_gallego https://jenkins.io/ •Docker in Continuous Integration Docker in CI servers – Download LTS version as .war – We need Java 8 to execute it $ java -jar jenkins.war
  • 89. Testing fácil con Docker · @micael_gallego •Docker in Continuous Integration Docker in CI servers
  • 90. Testing fácil con Docker · @micael_gallego Jenkins installation
  • 91. Testing fácil con Docker · @micael_gallego Jenkins installation
  • 92. Testing fácil con Docker · @micael_gallego New admin account Jenkins installation
  • 93. Testing fácil con Docker · @micael_gallego Jenkins installation
  • 94. Testing fácil con Docker · @micael_gallego ● Create new Jenkins job – Create a job with pipeline – Pipeline: ● Clone git repository ● Compile, test and package Java project ● Copy test results to Jenkins Jenkins Job
  • 95. Testing fácil con Docker · @micael_gallego Creamos una nueva tarea para descargar el proyecto y ejecutar los tests Jenkins Job
  • 96. Testing fácil con Docker · @micael_gallego Jenkins Job
  • 97. Testing fácil con Docker · @micael_gallego Change command to execute dockerized maven command
  • 98. Testing fácil con Docker · @micael_gallego node { // Mark the code checkout 'stage'.... stage 'Checkout' // Get some code from a GitHub repository git url: 'https://github.com/codeurjc/testing-docker.git' // Mark the code build 'stage'.... stage 'Build' // Run the maven build sh "cd basic-java-tests; docker run --rm -v "$PWD":/data -w /data maven mvn package" step([$class: 'JUnitResultArchiver', testResults: '**/target/surefire-reports/TEST-*.xml']) } Pipeline Jenkins Job
  • 99. Testing fácil con Docker · @micael_gallego Execute the new job Jenkins Job
  • 100. Testing fácil con Docker · @micael_gallego “Checkout” stage Jenkins Job
  • 101. Testing fácil con Docker · @micael_gallego Compilation and test stage Jenkins Job
  • 102. Testing fácil con Docker · @micael_gallego Successful finished job build (test passed) Jenkins Job
  • 103. Testing fácil con Docker · @micael_gallego Clic on build to see details Jenkins Job
  • 104. Testing fácil con Docker · @micael_gallego See console output Jenkins Job
  • 105. Testing fácil con Docker · @micael_gallego Repository clone and dockerized maven execution Jenkins Job
  • 106. Testing fácil con Docker · @micael_gallego ● Docker pipeline plugin – Allows a more powerful way to control docker container from pipelines – It is not installed by default Docker in CI servers https://jenkins.io/doc/book/pipeline/docker/ node { docker.image('node:7-alpine').inside { stage('Test') { sh 'node --version' } } }
  • 107. Testing fácil con Docker · @micael_gallego Docker in CI servers
  • 108. Testing fácil con Docker · @micael_gallego Docker in CI servers
  • 109. Testing fácil con Docker · @micael_gallego Docker in CI servers
  • 110. Testing fácil con Docker · @micael_gallego Docker in CI servers
  • 111. Testing fácil con Docker · @micael_gallego ● Docker pipeline plugin Docker in CI servers node { stage ('Checkout') { git url: 'https://github.com/codeurjc/testing-docker.git' } stage ('Build') { docker.image('maven').inside { sh 'cd basic-java-tests; mvn package' } step([$class: 'JUnitResultArchiver', testResults: '**/target/surefire-reports/TEST-*.xml']) } } sh "cd basic-java-tests; docker run --rm -v "$PWD":/data -w /data maven mvn package"
  • 112. Testing fácil con Docker · @micael_gallego ● Docker pipeline plugin – To cache Maven repository between executions, it is possible to mount a volume Docker in CI servers node { stage ('Checkout') { git url: 'https://github.com/codeurjc/testing-docker.git' } stage ('Build') { docker.image('maven').inside('-v $HOME/.m2:/root/.m2') { sh 'cd basic-java-tests; mvn package' } step([$class: 'JUnitResultArchiver', testResults: '**/target/surefire-reports/TEST-*.xml']) } }
  • 113. Testing fácil con Docker · @micael_gallego ● Advantages of using docker in CI – CI server just need docker installed, nothing more – All tools needed by devs are containerized – Tools are downloaded (and cached) automatically when needed – Several languages/stacks/dependencies can be used in the same CI server without conflicts – Sysadmins do not need to give access to CI server to developers (enforcing security) Docker in CI servers
  • 114. Testing fácil con Docker · @micael_gallego ● Testing different languages with Docker – Testing Node apps with mocha ● https://dzone.com/articles/testing-nodejs-application- using-mocha-and-docker – Testing C++ apps with Gtest ● https://github.com/yutakakinjyo/gtest-cmake-example – Testing Angular apps ● https://jaxenter.com/build-and-test-angular-apps-using -docker-132371.html Docker in CI servers
  • 115. Testing fácil con Docker · @micael_gallego ● Testing tools based on docker – TestContainers ● Define testing dependencies in your JUnit test ● https://www.testcontainers.org/ – Dockunit ● Test your code in several environments ● https://www.npmjs.com/package/dockunit – Many more... Docker in CI servers
  • 116. Testing fácil con Docker · @micael_gallego Docker Docker container as test dependencies Docker for tests
  • 117. Testing fácil con Docker · @micael_gallego Docker TestContainers ● Library to write tests in Java with JUnit that allows you to create and remove container in the context of a test ● It’s ideal for integration tests because external dependencies are managed in the test, not in CI server scripts ● It is possible to start any container, but there are wrappers for BBDD and Web browsers https://github.com/testcontainers/testcontainers-java Docker for tests
  • 118. Testing fácil con Docker · @micael_gallego Docker TestContainers ● How to start a generic container in the context of a test public class RedisBackedCacheTest { @Rule public GenericContainer redis = new GenericContainer("redis:3.0.6").withExposedPorts(6379); @Before public void setUp() throws Exception { Jedis jedis = new Jedis(redis.getIpAddress(),redis.getMappedPort(6379)); cache = new RedisBackedCache(jedis, "test"); } @Test public void cacheTest(){ //Test using cache connected to a real Redis database } } https://rnorth.org/junit-integration-testing-with-docker-and-testcontainers Docker for tests
  • 119. Testing fácil con Docker · @micael_gallego Docker TestContainers ● How to start a Web Browser in the context of a test public class SeleniumContainerTest { @Rule public BrowserWebDriverContainer chrome = new BrowserWebDriverContainer() .withDesiredCapabilities(DesiredCapabilities.chrome()) .withRecordingMode(RECORD_ALL, new File("target")); @Test public void simplePlainSeleniumTest() { RemoteWebDriver driver = chrome.getWebDriver(); System.out.println("Browser VNC URL: "+chrome.getVncAddress()); driver.get("https://wikipedia.org"); WebElement searchInput = driver.findElementByName("search"); //some asserts... } } https://rnorth.org/better-junit-selenium-testing-with-docker-and-testcontainers Docker for tests
  • 120. Testing fácil con Docker · @micael_gallego Docker TestContainers ● Execute sample code $ git clone https://github.com/testcontainers/testcontainers-java-examples $ cd cd testcontainers-java-examples $ cd redis-backed-cache/ $ mvn test //DDBB Tests... $ cd ../selenium-container $ mvn test //Browser Tests... Docker for tests Video recorded in /testcontainers-java-examples/selenium-container/target/recording*.flv
  • 121. Testing fácil con Docker · @micael_gallego Docker TestContainers ● You can execute “mvn test” inside a maven container ● ButTestContainers lib needs create new containers ● To allow a container to create new containers in the host, the file /var/run/docker.sock have to be mounted as volume $ docker run --rm -v "$PWD":/data -v /var/run/docker.sock:/var/run/docker.sock -w /data maven mvn test Docker for tests
  • 122. Testing fácil con Docker · @micael_gallego Docker TestContainers ● ExecutingTestContainers tests in Jenkins Docker for tests node { stage ('Checkout') { git url: 'https://github.com/testcontainers/testcontainers-java-examples' } stage ('Build') { docker.image('maven').inside('-v $HOME/.m2:/root/.m2 '+ '-v /var/run/docker.sock:/var/run/docker.sock') { sh 'cd redis-backed-cache; mvn test' } step([$class: 'JUnitResultArchiver', testResults: '**/target/surefire-reports/TEST-*.xml']) } }
  • 123. Testing fácil con Docker · @micael_gallego Docker Dockerized Jenkins ● Can we execute do this? ● CI Server only need Docker installed. All software is managed using containers Docker for tests Jenkins Maven Tests (TestContainers) Web Browser Docker Containers
  • 124. Testing fácil con Docker · @micael_gallego Docker Dockerized Jenkins ● It is possible, but official Jenkins image doesn’t allow it ● Jenkins container should have docker client installed ● Image trion/jenkins-docker-client is based on official jenkins image and install docker client Docker for tests docker run -it -e JENKINS_USER=$(id -u) --rm -p 8080:8080 -v $HOME/.jenkins:/var/jenkins_home -v /var/run/docker.sock:/var/run/docker.sock --name jenkins trion/jenkins-docker-client
  • 125. Testing fácil con Docker · @micael_gallego ● Conclusions – Docker containers are changing the way we develop, build, test and ship software – Containers allow developers to use the same dev tools and execute the project in the same environment – Containers ease the configuration and share of CI servers – If Continuous Integration is easier to use, more projects will use it and more test will be executed Docker in CI servers
  • 126. Testing fácil con Docker · @micael_gallego Thanks! Do you have any question?