Example of using Vagrant, Docker and Puppet for establishing development environment for simple Node app.
Steps below shows how to iterative establish the configurations and existing configuration is updated for each step. Link to file revisions are included below (TODO) while step descriptions are more documentation of how to reproduce the process in other examples and scenarios.
Following version has been used in this project:
- Vagrant v1.8.4
- VirtualBox v5.0.24
- Docker v1.12.0-rc4 (on local laptop) and v1.11.2 with Vagrant
- Puppet (TODO)
Im using OSX as host.
This project has been inspired by Activelamp Blog posts:
- Hasing out docker workflow - Part 1
- Using Vagrant with a Docker Workflow - Part 2
- Setting up a Docker development environment with Vagrant - Part 3
- Using Jenkins to bake images in a Docker Workflow - Part 4
and Lessons building node app docker
-
Install Docker on your local laptop
-
Create Dockerfile. See https://docs.docker.com/engine/reference/builder/#dockerfile-reference for more information
-
Build Docker image
docker build -t <your username>/<image name> .
-
Verify that new image has been created
docker images
-
Run the Docker image (create new Docker container)
docker run -p 49160:8080 -d --name <container name> <your username>/<image name>
. Running your image with-d
runs the container in detached mode, leaving the container running in the background. The-p
flag redirects a public port to a private port in the container.--name
flag assigns container name for easy access later on -
Verify that that container has been created
docker ps
-
Verify that node app has been started
docker logs <container name>
. -
Test your node app with
curl -i localhost:49160
or openlocalhost:49160
in your web browser. You should see something like:HTTP/1.1 200 OK X-Powered-By: Express Content-Type: text/html; charset=utf-8 Content-Length: 12 Date: Sat, 16 Jul 2016 15:46:54 GMT Connection: keep-alive Hello world
-
SSH into running Docker container by
docker exec -i -t <container name> /bin/bash
and verify that files are placed in src folderls /src
. Typeexit
to leave bash. -
Stop the running Docker container by
docker stop <container name>
and start it withdocker start <container name>
- https://docs.docker.com/engine/understanding-docker/
- https://docs.docker.com/v1.10/engine/examples/nodejs_web_app/
-
Install Vagrant on your local laptop
-
Install VirtualBox on your local laptop
-
Make sure to install Vagrant plugin
vagrant-vbguest
to keep VirtualBox and VirtualBox Guest Additions in syncvagrant plugin install vagrant-vbguest
-
Create 2
Vagrantfiles
(template can be created byvagrant init
). One vm for Docker Host (placed in /host folder) and second container for Docker Container (placed in the root folder). Note that Vagrantfile is using Ruby syntax. See https://www.vagrantup.com/docs/vagrantfile/ for more information -
Create and configure guest machines according to Vagrantfiles by
vagrant up
. See https://www.vagrantup.com/docs/cli/ for other Vagrant CLI commands -
Verify that Vagrant environments are running correctly by
vagrant global-status --prune
. You should see output like this:id name provider state directory cb02b94 default virtualbox running /Users/ismarslomic/src/vagrant-docker-puppet/host 8290b77 default docker running /Users/ismarslomic/src/vagrant-docker-puppet
As you can see, we have two environments: first is the Docker Host running inside VirtualBox while second is the Docker Container. Both has
running
state. -
Test your node app with
curl -i localhost:49161
or openlocalhost:49161
in your web browser. You should still see something like:HTTP/1.1 200 OK X-Powered-By: Express Content-Type: text/html; charset=utf-8 Content-Length: 12 Date: Sat, 16 Jul 2016 15:46:54 GMT Connection: keep-alive Hello world
-
If you do any changes to
Vagrantfiles
orDockerfile
after environment are running (vagrant up
) you should usevagrant reload --provision
. The flag--provision
will make sure to re-run the Docker provision -
You can shut down running Vagrant machines by using
vagrant halt <machine id>
(machine id can be found byvagrant global-status
) -
You can remove running Vagrant machines by using v
agrant destroy -f <machine id>
. Note that this command will destroy all resources created during creation process, including data inside vm/container -
Login into guest machine
docker-host
(by using VirtualBox GUI). Usevagrant
as username and password. You can also SSH into the machine by runningvagrant ssh <virtualbox machine id>
. After you have SSH into Docker Host you can SSH into running Docker Container bydocker exec -i -t <container name> /bin/bash
. Typeexit
to leave bash.