1. This document provides instructions for a workshop on the basic usage of Docker. It covers preparing the environment, running simple containers, publishing ports, sharing volumes, using environment variables, building images with Dockerfiles, pushing images to Docker Hub, linking containers, and provisioning containers with Vagrant.
3. 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 directory
2. Optionally configure PHPStorm/Webstorm vagrant settings
3. Run vagrant up in the tutorial directory
4. Run vagrant ssh in the tutorial directory
4. 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 it
3. Run a ping on www.google.ca in a container as a daemon
5. 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 images
docker ps
docker ps –a
docker start test
docker logs -f test
docker ps
docker attach test
<CTRL+C>
docker stop test
docker rm test
docker ps –a
docker images
docker run -d --name test phusion/baseimage:0.9.15 ping www.google.fr
docker ps
docker logs -f test
docker rm test
docker rm -f test (ou docker stop test puis docker rm ou docker kill test)
docker ps -a
(docker rmi phusion/baseimage:0.9.15)
6. 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 nodejs
1. Inside it, run the script /var/docker/install-devtools.sh on it
2. Launch zsh
3. Install nodejs-legacy and npm packages
4. Create the dir /var/www/tuto and create a test.js file inside it (given file)
5. Test nodejs locally
6. Exit the container
2. Commit the container new image as tribalnova/nodejs
3. Show the history of tribalnova/nodejs
9. Port publishing
Run the nodeJS container by forwarding its port 80 the host:
1. Try to reach the port 80
2. Inspect the current ndoeJS container
3. Remove the nodeJS container
4. Run the nodeJS container test.js file, mapping the port 80
5. Inspect the nodeJS container
11. Volume share
Run the nodeJS container by sharing its project volume:
1. Remove the previous nodejs container
2. Run it again by sharing /var/www/tuto with the host
3. Try to reach its port 80
4. Remove the nodeJS container
5. Launch it by mapping the port 80
6. Retry to reach its port 80
13. Environment variable
Run the nodeJS container and pass an environment variable:
1. Remove the previous nodejs container
2. Run it again by passing a variable TEST=TUTO to it
3. Inspect the container
4. Connect to it
5. Display the environment variable
6. Remove the container
15. Dockerfile
Run the nodeJS container from Dockerfile images
Documentation : 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/nodejs
3. 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/tuto
5. Run in a container the image tribalnova/tuto, by sharing the volume and
publishing the port as before
6. Test it
16. Dockerfile
cd docker
mkdir nodejs
cd nodejs
vi Dockerfile
docker build -t tribalnova/nodejs .
cd ..
mkdir tuto
cd tuto
vi test.js
vi Dockerfile
docker build -t tribalnova/tuto .
docker run --rm -ti -p 80:80 tribalnova/tuto
docker run --name nodejs -d -v /var/www/tuto:/var/www/tuto -p 80:80 tribalnova/tuto
vi /var/www/tuto/test.js <change the hello world message>
docker stop nodejs
docker start nodejs
FROM tribalnova/baseimage-ubuntu1404
MAINTAINER me
RUN apt-get update && apt-get install -y nodejs-
legacy npm
RUN /var/docker/install-devtools.sh && rm
/var/docker/install-devtools.sh
nodejs/Dockerfile
FROM tribalnova/nodejs
MAINTAINER me
ADD test.js /var/www/tuto/test.js
WORKDIR /var/www/tuto
EXPOSE 80
CMD node /var/www/tuto/test.js
tuto/Dockerfile
17. Docker push
Push the docker images to Dockerhub
1. Login
2. Push tribalnova/nodejs
3. Push tribalnova/tuto
4. Logout
19. 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 container
2. Run the container tribalnova/tuto in daemon, by linking the mongo
container to it
3. Inspect the container
4. Execute a zsh command on it
1. Display the environment variables
2. Test the mongoDB connection
3. Install the nodeJS mongodb package with npm
4. Create the test2.js file (provided file)
5. Commit this container image as tribalnova/tuto2
6. Run it on the port 81 in rm mode, launching test2.js through nodeJS
22. Linked container Dockerfile
Create a Dockerfile for a container that embeds the two nodeJS projects
1. Create a docker/tuto2 directory
2. Put the project files inside
3. Create a Dockerfile with:
1. An entrypoint that launch the node executable
2. The container must launch test.js by default
4. Build the image, naming it tribalnova/tuto2
5. Run a container with the default command, with the rm option
6. Run a container with node executing test2.js, with the rm option
23. Linked container Dockerfile
cd docker
mkdir 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/nodejs
MAINTAINER me
RUN npm install mongodb
ADD test.js /var/www/tuto/test.js
ADD test2.js /var/www/tuto/test2.js
WORKDIR /var/www/tuto
EXPOSE 80
ENTRYPOINT ["node"]
CMD ["test.js"]
tuto2/Dockerfile
24. 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 images
2. Alter Vagrantfile
3. Run the provising