Is that a Vagrant in your Computer?
Using Vagrant to set up your environment
Slides & Examples:
Introduction
Timothy Lim
@thewheat
+TimothyLimSH
thewheatfield.org
bruneigeekmeet.com
Let's start development!
In Theory
- Go to website & install requirements
- Code
- Profit!
Let's start development!
In Reality
- Go to website
- Try to install requirements
- See something breaks
- Google Bing the problem
- Try to fix it
- Go to Step 2
Vagrant
vagrantup.com
Create and configure reproducible & portable development environments
How-To Use
- Install
- Download configuration file (i.e. Vagrantfile).
Manual download or quickstart via command line
vagrant init hashicorp/precise32 - Start the box
vagrant up - Environment set up perfectly and ready to go
Where does the Vagrant environment live?
- Depends on the provider
- Typically Virtual Machines: VirtualBox, VMWare
- But many more including AWS
Vagrant Terminology
- Boxes:
- base operating system image to create the Vagrant environment from (e.g. .iso file to install an OS)
- Vagrantfile:
- configuration file that creates a Vagrant Environment from a Box
- Vagrant Environment:
- the actual running instance of a environment built from a Box via a Vagrantfile
- Providers:
- where the Vagrant Environment actually runs
- specified in the Vagrantfile
Vagrantfile
configuration file
- Box: base OS image to create environment from
- Environment settings: Name, memory usage
- Networking: Port forward to be accessible from host
- Synced Folders: by default /vagrant in the Vagrant environment is linked to host machine's filesystem where the Vagrantfile is
- Provisioning: install your tech stack
Vagrantfile
sample
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| config.vm.box = "hashicorp/precise32" config.vm.hostname = "GDGBNdevfest2014"
config.vm.network "forwarded_port", guest: 80, host: 8000 config.vm.network "forwarded_port", guest: 8080, host: 8080 config.vm.network "private_network", ip: "192.168.10.19"
config.vm.synced_folder "www", "/var/www" config.vm.synced_folder "var_cache_apt", "/var/cache/apt/"
config.vm.provision "shell", :path => "setup.sh" end
Vagrant Commands
- Initialize Vagrantfile from vagrantcloud.com
vagrant init hashicorp/precise32 - Start Machine
vagrant up - Suspend Machine (your files are safe)
vagrant halt - Nuke it all!
vagrant destroy - SSH into its heart (Windows may need Putty)
vagrant ssh
Vagrant Commands
- Provision, we must (runs first time with vagrant up)
vagrant provision - Box Managment
vagrant box- All locally downloaded boxes
vagrant box list
- All locally downloaded boxes
Vagrant Cloud
vagrantcloud.com
- Search / Download existing boxes
- Share your boxes
Problems
It's never just a walk in the clouds
- Corrupted boxes:
- Need to manually delete locally downloaded box
- Shared folder not set up: Google Bing it
- Incompatible Vagrant and VirtualBox:
- Update to latest else slowly downgrade VirtualBox
- git cloning UNIX vs Windows line endings:
- dos2unix
- git config --global core.autocrlf input
- Possibly time consuming and painful to setup
And that's Vagrant in a nutshell
Hopefully....
But wait, there's more!
Computer Whaling
Using Docker to set up your environment
Docker
docker.io
- Open platform for developers and sysadmins to build, ship, and run distributed applications.
- Portable and lightweight (FAST!)
- Focused more on deployment and production
- Development solutions exist such as Fig
Docker Terminology
Understanding Docker
- Images: (build component)
- Read-only template to create containers from
- Similar to Vagrant Boxes
- Registries: (distribution component)
- Storage of images
- Public Docker Hub or Private Docker Registry
- Containers: (run component)
- Seen has processes on your computer
- Once it finished, it will close
- Dockerfile:
- a configuration file to build Docker Images
Docker Commands (1)
- Search for a docker image
docker search *search string*
- Download image locally
docker pull user/imagename:tag
- Run commands in Container
docker run user/imagename
command_to_run arguments_to_command - Get bash shell in Container
docker run -t -i user/imagename /bin/bash
Docker Commands (2)
- Save changes
docker commit container_id
-m="message for the commit"
-a="name of author"
user/imagename:tag - Listing containers
docker ps -l # Last Runningdocker ps # Currently runningdocker ps -a # All
- Stop Container
docker stop container_id_or_name
- List all downloaded Images
docker images
Docker Commands (3)
- Inspect
docker inspect container_name_or_id
- Deleting
docker rm container_name_or_iddocker rmi image_name_or_id
- Build image from a Dockerfile
# in folder containing Dockerfiledocker build -t image_name .
Dockerfile
- FROM user/imagename:tag (specify base image)
- RUN echo "Run install commands e.g. apt-get install"
- WORKDIR /default/path/for/all/things
- CMD default action to take when running container
- ENTRYPOINT enables whole container to runs as if it was an executable
- EXPOSE Port#1 Port#2
Mapping ports
- Ports of services in container to be exposed to the host machine
- Auto mapped
docker run
-P training/webapp python app.py - Manually mapped
docker run
-p hostPort:ContainerPort
-p 8080:80
training/webapp python app.py - See port mapping
docker port container_id
Working Directory
- Specify where to start any RUN , CMD or ENTRYPOINT
docker run
-p 8080:8080 -p 35729:35729
-w "/slides/presentation-tabs/"
devfest/node
gulp serve
Linking containers
- For interaction between containers
- Name a container and reference it
docker run --name db training/postgres
docker run -P --name web --link db:thedb
training/webapp python app.py
docker run --name web --link db:thedb
training/webapp cat /etc/hosts
172.17.0.32 thedb - the 'db' container must have necessary ports EXPOSEd from the originating Dockerfile
Managing Data in Containers
1. Data Volumes
- Persist until no containers use them (non host file mounts)
- Changes not included when you update an image
- Examples:
- Create a /webapp folder in the container
docker run -d -P --name web -v /webapp
training/webapp python app.py - Mount host file system in the container
docker run -d -P --name web
-v /host/webapp:/container/webapp
training/webapp python app.py - Create a /webapp folder in the container
Managing Data in Containers
2. Data Volume Containers
- Containers that act solely as a data volume
- Creation
docker run -d -v /dbdata --name dbdata
training/postgres echo Data-only container - Usage
docker run -d --volumes-from dbdata
--name db1 training/postgresdocker run -d --volumes-from dbdata
--name db2 training/postgres
Findings
- Docker is more complicated as whole
- Docker provision is easier
- Allows quicker testing and experimentation
- You can work with same container that will be pushed to production
- Much smaller footprint in terms of CPU and Disk Space used
- It's all the rage now
In conclusion
Dock all the things!
- boot2docker is your friend for non Linux users!
- No reason not to learn it
Acknowledgements & Links
- vagrantup.com
- docker.io: Interactive Docker Tutorial
- Should I use Vagrant or Docker.io for creating an isolated environment?
- Why You Shouldn’t Use Vagrant: Real talk from a Vagrant burn-out
- boot2docker (for OSX and Windows)
- Docker Cheat Sheet
- Where are Docker Images Stored
- Using Docker as a Development Environment
- giphy.com
- Installing node on Ubuntu