Tribal Nova Docker workshop

26
Docker workshop Workshop about the basic usage of Docker Nicolas DEGARDIN <[email protected] >

Transcript of Tribal Nova Docker workshop

Page 1: Tribal Nova Docker workshop

Docker workshopWorkshop about the basic usage of Docker

Nicolas DEGARDIN <[email protected]>

Page 2: Tribal Nova Docker workshop

Summary

1. Preparation2. The ping example3. Container4. Port publishing 5. Volume share6. Environment variables7. Dockerfile8. Docker push9. Linked container10. Linked container Dockerfile11. Vagrant provisioning

Page 3: Tribal Nova Docker workshop

Preparation

Needed:•Virtual Box•Vagrant•ZIP workshop file tuto.zip(if unavailable, the archive is here: https://hmhco.box.com/docker-tuto)•Nothing should run on ports 80 and 81 of the host machine

1. Extract the ZIP file in a directory2. Optionally configure PHPStorm/Webstorm vagrant

settings3. Run vagrant up in the tutorial directory4. Run vagrant ssh in the tutorial directory

tuto.zip

Page 4: Tribal Nova Docker workshop

The ping example

Run a container that echoes a ping

•Based on phusion/baseimage:0.9.15•Try to see what happen with docker images, docker ps, docker logs

•When in background, try to play with docker attach•Manipulate docker rm, docker kill, docker start, docker stop•Documentation

–Commands docker and docker <instruction> --help–https://docs.docker.com/reference/commandline/cli/

1. Run a ping on www.google.ca in a container 2. Restart the container and stop it3. Run a ping on www.google.ca in a container as a daemon

Page 5: Tribal Nova Docker workshop

The ping example

(docker search phusion)(docker pull phusion/baseimage:0.9.15)docker run --name test phusion/baseimage:0.9.15 ping www.google.fr<CTRL+C>docker imagesdocker psdocker ps –adocker start testdocker logs -f testdocker psdocker attach test<CTRL+C>docker stop testdocker rm testdocker ps –adocker imagesdocker run -d --name test phusion/baseimage:0.9.15 ping www.google.frdocker psdocker logs -f testdocker rm testdocker rm -f test (ou docker stop test puis docker rm ou docker kill test)docker ps -a

(docker rmi phusion/baseimage:0.9.15)

Page 6: Tribal Nova Docker workshop

Container

Run a container, install a nodeJS server onto it, and commit it:

•Based on tribalnova/baseimage-ubuntu1404

1. Run a bash command in the container, named nodejs1. Inside it, run the script /var/docker/install-devtools.sh on it2. Launch zsh3. Install nodejs-legacy and npm packages4. Create the dir /var/www/tuto and create a test.js file inside it

(given file)5. Test nodejs locally6. Exit the container

2. Commit the container new image as tribalnova/nodejs3. Show the history of tribalnova/nodejs

Page 7: Tribal Nova Docker workshop

Container

var http = require("http");

http.createServer(function (request, response) { response.writeHead(200, { 'Content-Type': 'text/plain' }); response.end('Hello');}).listen(80);

console.log("Server running");

test.js

Page 8: Tribal Nova Docker workshop

Container

(docker pull tribalnova/baseimage-ubuntu1404)docker run -ti --name nodejs tribalnova/baseimage-ubuntu1404 bash

#/var/docker/install-devtools.sh#rm /var/docker/install-devtools.sh#zsh

#apt-get update#apt-get install nodejs-legacy npm

#mkdir /var/www#mkdir /var/www/tuto#cd /var/www/tuto#npm install http#vi test.js#node test.js &#curl 127.0.0.1<CTRL+P> <CTRL+Q>

docker commit nodejs tribalnova/nodejsdocker history tribalnova/nodejs

Page 9: Tribal Nova Docker workshop

Port publishing

Run the nodeJS container by forwarding its port 80 the host:

1. Try to reach the port 802. Inspect the current ndoeJS container3. Remove the nodeJS container4. Run the nodeJS container test.js file, mapping the port 805. Inspect the nodeJS container

Page 10: Tribal Nova Docker workshop

Port publishing

docker inspect nodejscurl <IP>

docker psdocker imagesdocker rm -f nodejs

docker run -d -p 80:80 --name nodejs tribalnova/nodejs node /var/www/tuto/test.jsdocker logs nodejsdocker inspect nodejscurl <IP>

Page 11: Tribal Nova Docker workshop

Volume share

Run the nodeJS container by sharing its project volume:

1. Remove the previous nodejs container2. Run it again by sharing /var/www/tuto with the host3. Try to reach its port 804. Remove the nodeJS container5. Launch it by mapping the port 806. Retry to reach its port 80

Page 12: Tribal Nova Docker workshop

Volume share

docker rm -f nodejsvi /var/www/tuto/test.jsdocker run -d -p 80:80 -v /var/www/tuto:/var/www/tuto --name nodejs tribalnova/nodejs node /var/www/tuto/test.jsdocker logs nodejsdocker inspect nodejscurl <IP>

Page 13: Tribal Nova Docker workshop

Environment variable

Run the nodeJS container and pass an environment variable:

1. Remove the previous nodejs container2. Run it again by passing a variable TEST=TUTO to it3. Inspect the container4. Connect to it5. Display the environment variable6. Remove the container

Page 14: Tribal Nova Docker workshop

Environment variable

docker rm -f nodejsdocker run -d -p 80:80 -e TEST=TUTO -v /var/www/tuto:/var/www/tuto --name nodejs tribalnova/nodejs node /var/www/tuto/test.jsdocker inspect nodejsdocker exec -ti nodejs zsh

#env#exit

docker rm -f nodejs

Page 15: Tribal Nova Docker workshop

Dockerfile

Run the nodeJS container from Dockerfile imagesDocumentation : https://docs.docker.com/reference/builder/

1. Create a Dockerfile image with only a nodeJS server installed, in the directory docker/nodejs

2. Build it, and name the image tribalnova/nodejs3. Create a Dockerfile image that inherits from the image

tribalnova/nodejs, that embeds the test.js file, and launch it through nodeJS as a default command, in the directory docker/tuto

4. Build it and name the image tribalnova/tuto5. Run in a container the image tribalnova/tuto, by sharing the volume

and publishing the port as before6. Test it

Page 16: Tribal Nova Docker workshop

Dockerfile

cd dockermkdir nodejscd nodejsvi Dockerfile

docker build -t tribalnova/nodejs .

cd ..mkdir tutocd tutovi test.jsvi Dockerfile

docker build -t tribalnova/tuto .

docker run --rm -ti -p 80:80 tribalnova/tutodocker run --name nodejs -d -v /var/www/tuto:/var/www/tuto -p 80:80 tribalnova/tutovi /var/www/tuto/test.js <change the hello world message>docker stop nodejsdocker start nodejs

FROM tribalnova/baseimage-ubuntu1404MAINTAINER me

RUN apt-get update && apt-get install -y nodejs-legacy npmRUN /var/docker/install-devtools.sh && rm /var/docker/install-devtools.sh

nodejs/Dockerfile

FROM tribalnova/nodejsMAINTAINER me

ADD test.js /var/www/tuto/test.jsWORKDIR /var/www/tuto

EXPOSE 80

CMD node /var/www/tuto/test.js

tuto/Dockerfile

Page 17: Tribal Nova Docker workshop

Docker push

Push the docker images to Dockerhub

1. Login2. Push tribalnova/nodejs3. Push tribalnova/tuto4. Logout

Page 18: Tribal Nova Docker workshop

Docker push

docker logindocker push tribalnova/nodejsdocker search nodejsdocker logoutdocker search nodejs

Page 19: Tribal Nova Docker workshop

Container linking

Launch a mongoDB container and create a nodeJS container with an application that connects to it

1. Find and launch the mongoDB official container2. Run the container tribalnova/tuto in daemon, by linking the mongo

container to it3. Inspect the container4. Execute a zsh command on it

1. Display the environment variables2. Test the mongoDB connection3. Install the nodeJS mongodb package with npm4. Create the test2.js file (provided file)

5. Commit this container image as tribalnova/tuto26. Run it on the port 81 in rm mode, launching test2.js through

nodeJS

Page 20: Tribal Nova Docker workshop

Container linkingvar http = require("http");var MongoClient = require('mongodb').MongoClientvar url = 'mongodb://mongo:27017/tuto';

http.createServer(function (request, response) { response.writeHead(200, { 'Content-Type': 'text/plain' }); MongoClient.connect(url, function(err, db) { if(err) throw err; var collection = db.collection('samples'); collection.insert([ {'hello' : 'world'}, {'et' : 'cetera'}, {'et' : {'cetera':'et cetera'}} ], {w:1}, function(err, result) { collection.find().toArray(function(err, docs) { response.end(JSON.stringify(docs)); }); }); });}).listen(80);

console.log("Server running");

test2.js

Page 21: Tribal Nova Docker workshop

Container linking

docker search mongodocker run -d --name mongo mongodocker run --name nodejs --link mongo:mongo -d -p 80:80 tribalnova/tutodocker inspect nodejsdocker exec -ti nodejs zsh

#env#more /etc/hosts#nc -zv mongo 27017#cd /var/www/tuto#npm install mongodb#vi test2.js#exit

docker commit nodejs tribalnova/tuto2docker run --rm --link mongo:mongo -p 81:80 tribalnova/tuto2 node test2.js

Page 22: Tribal Nova Docker workshop

Linked container Dockerfile

Create a Dockerfile for a container that embeds the two nodeJS projects

1. Create a docker/tuto2 directory2. Put the project files inside3. Create a Dockerfile with:

1. An entrypoint that launch the node executable2. The container must launch test.js by default

4. Build the image, naming it tribalnova/tuto25. Run a container with the default command, with the rm option6. Run a container with node executing test2.js, with the rm option

Page 23: Tribal Nova Docker workshop

Linked container Dockerfile

cd dockermkdir tuto2<copy files test.js and test2.js inside>vi Dockerfile

docker build -t tribalnova/tuto2 .docker run --rm --link mongo:mongo –p 80:80 tribalnova/tuto2<CTRL+C>docker run --rm --link mongo:mongo –p 80:80 tribalnova/tuto2 test2.js

FROM tribalnova/nodejsMAINTAINER me

RUN npm install mongodbADD test.js /var/www/tuto/test.jsADD test2.js /var/www/tuto/test2.js

WORKDIR /var/www/tutoEXPOSE 80ENTRYPOINT ["node"]CMD ["test.js"]

tuto2/Dockerfile

Page 24: Tribal Nova Docker workshop

Vagrant provisioner

Modify the Vagrant file to build and launch the containers upon provisioning.

Documentation: http://docs.vagrantup.com/v2/provisioning/docker.html

1. Clear the containers and images2. Alter Vagrantfile3. Run the provising

Page 25: Tribal Nova Docker workshop

Vagrant provisioner

docker ps -qa | xargs docker rm -fdocker images -q | xargs docker rmi –f

ON THE HOST MACHINE:<insert the Vagrantfile docker section>vagrant provision

config.vm.provision "docker" do |d| d.run "mongo", daemonize: true, args: "--name mongo"

d.build_image "./docker/nodejs", args: "-t tribalnova/nodejs"

d.build_image "./docker/tuto", args: "-t tribalnova/tuto"

d.build_image "./docker/tuto2", args: "-t tribalnova/tuto2"

d.run "tribalnova/tuto", daemonize: true, args: "--name tuto \ --link mongo:mongo \ -p 80:80"

d.run "tribalnova/tuto2", daemonize: true, args: "--name tuto2 \ --link mongo:mongo \ -p 81:80"end

Vagrantfile

Page 26: Tribal Nova Docker workshop