Docker
A two part shell/management layer for building and running virtual linux containers, based on LXC.
A tool for deploying and running applications. Docker provides a way to run an application securely isolated in a container in a way that is platform agnostic.
Docker Elements
Image
Read-only template for a docker container.
- An immutable file that's essentially a snapshot of a container. Images are created with the build command, and they'll produce a container when started with run.
- Uses a union file system (UFS) to 'layer' file system branches on top of each other. Every time a change is made to a Docker image, a new layer is created.
- Docker images are built from a set a steps called instructions. These instructions can be built either by executing commands manually or automatically through Dockerfiles.
- As more layers (tools, applications, etc.) are added on top of the base, new images can be formed by committing these changes – like a version control system!
- Images are stored in a Docker registry such as registry.hub.docker.com.
- Because they can become quite large, images are designed to be composed of layers of other images, allowing a miminal amount of data to be sent when transferring images over the network.
Layers
Commands
Search
docker search [image_name]
docker pull [image_name]
List all images on your system
docker images
Build an image from a dockerfile
docker build -t NAME_OF_IMAGE [directory where Dockerfile lives]
Commit an image
sudo docker commit [container ID] IMAGE_NAME
Remove
docker rmi -f IMAGE_ID
Container
A Linux Container, (sort of) like a directory, it holds everything needed for an app to run.
Docker containers are essentially directories that can be packed (e.g. tar-archived), then shared and run on other hosts. The only dependency is having docker installed on the hosts.
Docker containers allow:
- Application portability
- Process isolation
- Preventing access beyond the container's own filesystem
- Lightweight, esp. relative to VMs
When everything is self-contained and the risk of system-level changes are eliminated, the container becomes immune to external exposures which could put it out of order (i.e. 'dependency hell').
NB: docker depends on a single process to run. When that process stops, the container stops.
If an image is a class, then a container is an instance of a class.
Commands
List
docker ps # those running
docker ps -l # both running & dormant
Create (either from an existing image or creating a new one)
docker run IMAGE_NAME COMMAND_TO_RUN
docker run my_image echo 'hello'
Running
docker run CONTAINER_ID
docker run IMAGE_NAME COMMAND_TO_RUN
Start an interactive shell within your container
docker run -it IMAGE_NAME /bin/sh
Forward port on the host to a port on the container
docker run --publish 3000:3000 IMAGE_NAME COMMAND_TO_RUN
Stopping a container
docker stop CONTAINER_ID
Removing a container
docker rm CONTAINER_ID
Attaching yourself to a container (your console will run commands within the container itself)
docker attach CONTAINER_ID
Detach the current container: type ^
+ P
followed by ^
+ Q
Mounting
Installations
CentOS
https://docs.docker.com/engine/installation/linux/centos/
Update Yum
sudo yum update
Add the yum repo
sudo tee /etc/yum.repos.d/docker.repo <<-'EOF'
[dockerrepo]
name=Docker Repository
baseurl=https://yum.dockerproject.org/repo/main/centos/$releasever/
enabled=1
gpgcheck=1
gpgkey=https://yum.dockerproject.org/gpg
EOF
Install docker image
sudo yum install docker-engine
Start the daemon
sudo service docker start
Verify it worked
sudo docker run hello-world
Create docker group and add your user
sudo usermod -aG docker your_username
Start docker daemon at boot
sudo chkconfig docker on
Helpful Visuals
Docker Tools
Docker Engine | runs on Linux to create the operating environment for your distributed applications. |
Docker Machine | automate Docker provisioning |
Docker Toolbox | an installer to quickly and easily install and setup a Docker environment on your computer |
Kitematic | build and run containers through a GUI |
NOTE: Docker Machine deprecates Boot2Docker
References
- Deploy Rails Application using Docker
- Docker Explained: How To Containerize and Use Nginx as a Proxy
- Docker Hub
- Docker: Create a base image
- Docker: Docs
- Docker: Get Started with Docker for Mac OS X
- How To Install and Use Docker: Getting Started
- Intro to Docker
- LearningDocker.com: Developing with Docker Containers
- Quora: What is the difference between Docker and Vagrant? When should you use each one?
- StackOverflow: Docker image vs container
- GitHub: veggiemonk/awesome-docker: curated list of Docker resources and projects