Docker Primer
When we think of virtualization today, we may think of Virtual Box, which abstracts away the system processes, and lets you run a complete system from another. Think of Docker as Virtual Box, but extremely lightweight (in terms of resource consumption). Obviously I’m over simplifying the explanation a little, and a whole lot of things are getting lost in simplification. But for now, this will do.
Installation
Install docker:
apt remove docker docker-engine docker.ioapt update && apt -y upgradeapt install -y linux-image-extra-$(uname -r) linux-image-extra-virtualapt update && apt -y upgradeapt install -y apt-transport-https ca-certificates curl software-properties-commoncurl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add -add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"apt update && apt -y upgradeapt install -y docker-ceInstall docker-compose:
curl -L https://github.com/docker/compose/releases/download/1.21.2/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-composechmod +x /usr/local/bin/docker-composePost Installation
Allow docker usage without sudo access:
sudo groupadd dockersudo usermod -aG docker $USERnewgrp dockerTerminology
- Images: Read-only templates uploaded to Docker Hub; analogous to classes in OOP
- Containers: Runtime instances spawned from images; similar to object instances
- Tags: Version identifiers for images
- Container ID or Image ID: Unique identifiers assigned to each container or image
Usage
A Dockerfile can be used to build docker images.
List all images available on your system:
docker imagesList all containers:
docker ps -aPull images from the docker hub:
docker pull ubuntuBy default the latest tag is pulled. To pull a specific tag:
docker pull ubuntu:12.04Delete an image:
docker rmi ubuntu:latestor
docker rmi IMAGE_IDCreate a container from an image and run it:
docker run -it ID /bin/bashistands for interactivetstands for pseudo ttyIDcan be anIMAGE-IDorCONTAINER-ID
Note: Every time you use docker run with an IMAGE-ID, a new container is created based on the image. Using --rm flag automatically deletes the container after exiting.
Exit a container:
exitStart / stop a container:
docker start CONTAINER_IDdocker stop CONTAINER_IDDelete a container:
docker rm CONTAINER_IDBuild your own docker image using a Dockerfile:
docker build -t username/image_name:image_tag .Delete dangling images:
docker rmi $(docker images -f dangling=true -q)Save an existing docker container as an image:
docker commit CONTAINER_ID IMAGE_NAME:IMAGE_TAGSave an image as a tar file:
docker save IMAGE_ID -o file_name.tar