Vagrant/Docker

From My Wiki
Jump to: navigation, search
  • vagrant: vm
  • docker: containers (much more lightweight as they share the host's kernel)

Vagrant

more info: http://docs.vagrantup.com/

Installation instructions: https://www.digitalocean.com/community/tutorials/how-to-install-vagrant-on-a-vps-running-ubuntu-12-04

Basics Getting Ubuntu 12.04 LTS 32-bit

vagrant init precise32 http://files.vagrantup.com/precise32.box
vagrant up
vagrant ssh
vagrant destroy

vagrant suspend

Windows test boxes

vagrant box add {title} {url}
vagrant init {title}
vagrant up

Note: before your start, may need to fix your kernel: https://alaning.me/index.php/Linux

Intro to Docker

Docker is an open source tool to run applications inside of a Linux container, a kind of light-weight virtual machine. Isolation. Makes applications easier to handle.

VMs can do isolation already. Except issues are the heavy weightness of it. Costly in terms of time for it to start and also resources required to keep it running (remember, each vm has to run a full os).

LXC: Linux Containers, which allow individual processes to run at a higher level of isolation than regular Unix process. The term used for this is containerization: a process is said to run in a container. Containers support isolation at the level of: File system (except for files specifically mounted), User namespace, Process namespace, Network namespace (ip and port).

Much more lightweight (in booting and keeping it running).

Docker Basics

docker run -t -i ubuntu /bin/bash

It downloads (or if you've used it before it grabs it locally) then runs /bin/bash in ubuntu to give you a shell. -t attaches a virtual terminal. -i provides interactivity (allowing you to type commands).

After starting this and running some commands your changes aren't saved! touch a file for example.

when you type exit the /bin/bash process stops. as a result the docker container is stopped. to detach the tty without exiting the shell do Ctrl-p + Ctrl-q

if you were to run the same command again, you would be creating a new container based on the same ubuntu image (it should start up instantly this time if it didn't before).

Right now, if you want to go back into the same container you can start (docker start <id>) the docker container based on id. you can find all the docker containers with their ids that are running by doing docker ps (docker ps -a for all including stopped containers). After starting you can then attach (docker attach <id>) to that container. and if you do you should be able to see the file you touched before and continue using the docker container as usual.

another method to reuse a container is to commit it, and then give it a name. commit:think of a docker as a git for operating systems. committing a container preserves the changes that have been made, gives it a name and stores it in the local image library (docker images) for future use. base images like ubuntu and centos can be used to create custom images.

docker run --name=testme -t -i ubuntu /bin/bash

then you can start/stop/attach/rm containers based on name instead of the id given

Docker containers are instances of docker images.

More information: https://gist.github.com/wsargent/7049221

More complex bash example

docker run -it ubuntu bash -c "sudo apt-get update; sudo apt-get -y upgrade; apt-get install -y git vim; /bin/bash"

Installing Docker

the below command is completely safe

curl -sL https://get.docker.io/ | sh

Note that this requires a clean (unmodified) kernel. See instructions here: http://alaning.me/index.php/Linux#Reset_Kernel

Docker commands

docker stop $(docker ps -a -q)
docker rm $(docker ps -a -q)

docker ps -a #lists all containers
docker ps #lists only running containers
docker images #lists all docker images

docker pull centos
docker run -i -t centos /bin/bash
docker run centos /bin/echo Hello World
docker run ubuntu /bin/echo Hello World

Docker clean up

There is not official way. My favorite way of removing all stopped docker containers is:

docker ps -q | xargs docker rm

it will list all images (docker ps), but only show the id's. And then run a docker rm command for each one of them.

Similarly for removing all unused docker images

docker images -q | xargs docker rmi

Will list all docker images and then docker rmi them.


Docker normally prevents removal of images that are in use by containers.