Docker - The Linux Container Engine
What is Docker?
Docker is an open-source engine that automates the deployment of any application as a lightweight, portable, self-sufficient container that will run virtually anywhere.
Docker containers can encapsulate any payload, and will run consistently on and between virtually any server. The same container that a developer builds and tests on a laptop will run at scale, in production*, on VMs, bare-metal servers, OpenStack clusters, public instances, or combinations of the above.
Common use cases for Docker include
- Automating the packaging and deployment of applications
- Creation of lightweight, private PAAS environments
- Automated testing and continuous integration/deployment
- Deploying and scaling web apps, databases and backend services
docker, the Linux Container Runtime, runs Unix processes with strong guarantees of isolation across servers. Your software runs repeatably everywhere because its Container includes any dependencies.
docker runs three ways
- as a daemon to manage LXC containers on your Linux host (sudo docker -d)
- as a CLI which talks to the daemon’s REST API (docker run ...)
- as a client of Repositories that let you share what you’ve built (docker pull, docker commit).
Each use of docker is documented here. The features of Docker are currently in active development, so this documentation will change frequently.
It is like Vagrant for LXC - Linux Container.
- Heterogeneous payloads
Any combination of binaries, libraries, configuration files, scripts, virtualenvs, jars, gems, tarballs, you name it. No more juggling between domain-specific tools. Docker can deploy and run them all.
- Any server (Linux)
Docker can run on any x64 machine with a modern linux kernel - whether it's a laptop, a bare metal server or a VM. This makes it perfect for multi-cloud deployments.
Docker isolates processes from each other and from the underlying host, using lightweight containers.
Because each container is isolated in its own filesystem, they behave the same regardless of where, when, and alongside what they run.
Under the hood
Under the hood, Docker is built on the following components:
- The cgroups and namespacing capabilities of the Linux kernel;
- aufs - a powerful union filesystem with copy-on-write capabilities;
- The Go programming language;
- lxc - a set of convenience scripts to simplify the creation of linux containers.
Filesystem isolation: each process container runs in a completely separate root filesystem.
Resource isolation: system resources like cpu and memory can be allocated differently to each process container, using cgroups.
Network isolation: each process container runs in its own network namespace, with a virtual interface and IP address of its own.
Copy-on-write: root filesystems are created using copy-on-write, which makes deployment extremeley fast, memory-cheap and disk-cheap.
Logging: the standard streams (stdout/stderr/stdin) of each process container is collected and logged for real-time or batch retrieval.
Change management: changes to a container's filesystem can be committed into a new image and re-used to create more containers. No templating or manual configuration required.
Interactive shell: docker can allocate a pseudo-tty and attach to the standard input of any container, for example to run a throwaway interactive shell.
NOTE: For Ubuntu 12.04, to avoid a bug install the Linux 3.8 kernel (linux-image-lts-raring or linux-current-generic).
1. add Docker official APT Repository - Preferred
Deprecated - Add the PPA
3. Run it (download the base 'ubuntu' container and run bash inside it while setting up an interactive shell)
Ctrl-D or type exit to quit.
Runtime data directory => /var/lib/docker
Docker With Vagrant
Fetch the docker sources (this includes the Vagrantfile for machine setup) and spin it up
- Download the ‘official’ Precise64 base ubuntu virtual machine image from vagrantup.com
- Boot this image in virtualbox
- Add the Docker PPA sources to /etc/apt/sources.lst
- Update your sources
- Install lxc-docker
You now have a Ubuntu Virtual Machine running with docker pre-installed.
NOTE: Ctrl-D or type exit to quit the shell.
This is the most basic example available for using Docker.
Download the base image (named “ubuntu”):
Alternatively to the ubuntu image, you can select busybox, a bare minimal Linux system. The images are retrieved from the Docker repository.
- "sudo" - super user do - execute the following commands as user root
- "docker run" run a command in a new container
- "ubuntu" is the image we want to run the command inside of.
- "/bin/echo" is the command we want to run in the container
- "hello world" is the input for the echo command
Hello World Daemon
This example assumes you have Docker installed and the Ubuntu image already imported with docker pull ubuntu. We will use the Ubuntu image to run a simple hello world daemon that will just print hello world to standard out every second. It will continue to do this until we stop it.
We are going to run a simple hello world daemon in a new container made from the ubuntu image.
- "docker run -d " run a command in a new container. We pass "-d" so it runs as a daemon.
- "ubuntu" is the image we want to run the command inside of.
- "/bin/sh -c"is the command we want to run in the container
- "while true; do echo hello world; sleep 1; done"is the mini script we want to run, that will just print hello world once a second until we stop it.
- $CONTAINER_ID the output of the run command will return a container id, we can use in future commands to see what is going on with this process.
Check the logs make sure it is working correctly
"docker logs" This will return the logs for a container
$CONTAINER_ID The Id of the container we want the logs for.
Attach to the container to see the results in realtime
"docker attach" This will allow us to attach to a background process to see what is going on.
$CONTAINER_ID The Id of the container we want to attach too.
Exit from the container attachment by pressing Control-C.
Check the process list to make sure it is running
"docker ps" this shows all running process managed by docker.
Stop the container
"docker stop" This stops a container
$CONTAINER_ID The Id of the container we want to stop.