Introduction
Docker is an open platform designed for developing, shipping, and running applications. It allows you to isolate your applications from your infrastructure, enabling faster software delivery. With Docker, you can manage your infrastructure similarly to how you manage your applications. By leveraging Docker’s methods for quick shipping, testing, and deployment of code, you can greatly reduce the time between writing code and deploying it in production.
Docker adds an extra layer of abstraction and automation to operating system-level virtualization.
- Docker Client talks to “Docker Daemon” to execute docker operations (build, run, push, pull etc)
- Client sends a request to Daemon to build an image and Daemon creates an image locally
- Daemon push the created Image to Registry per client request
- Client sends a request to run a docker container to Docker Host:
- Docker Daemon checks if Image is present locally and spin-up a docker container using the local image
- If image doesn’t exist locally, daemon pulls it from Registry to store locally first and spin-up a new container using it
- Docker Client can communicate to Docker Host using either CLI or remote APIs
- Docker Registry can be any storage like: Docker Hub, Amazon ECS, Google Container Engine, S3 etc.
Docker Workflow
Here is a quick example of docker workflow from end-to-end starting from building an image to spin-up a container and remove.
# Create a docker image
docker build -t ${IMAGE_NAME}:${BUILD_NUMBER} .
# Tagging an image
docker tag -f ${IMAGE_NAME}:${BUILD_NUMBER} ${REGISTRY}/${IMAGE_NAME}:latest
# Pushing an image to docker Registry
docker push ${REGISTRY}/${IMAGE_NAME}:latest
# Spin-up a new docker container (using public image)
docker run -d --name=redisdb -p 6379:6379 redis
# Spin-up a new docker container (using internal registry) linking to another running docker container
docker run --link redisdb:rdb --env=REDIS_HOSTPORT=$(docker inspect --format '{{.NetworkSettings.IPAddress}}' redisdb):6379 --name=classy -d -p 1234:8080 ${REGISTRY}/${IMAGE_NAME}:latest
# Stop and Remove docker containers
docker stop redis classy
docker rm redis classy
# OR
# Spin-up a docker container with adding the "rm" flag to stop & remove the container itself after execution of CMD specified in DOCKERFILE
docker run --link redisdb:rdb --env=REDIS_HOSTPORT=$(docker inspect --format '{{.NetworkSettings.IPAddress}}' redisdb):6379 --rm=true --name=classy -d -p 1234:8080 ${REGISTRY}/${IMAGE_NAME}:latest
Docker CLI
Docker Command | Usages |
---|---|
INFO | |
docker info | To get the detailed information about docker |
docker –version | To check the “installed docker version” |
docker-compose –version | To check docker-compose installed version |
docker-machine –version | To check docker-machine version |
Images | |
docker images | To check the all available docker images |
docker pull <image_name> | To pull a docker image from registry |
docker rmi <image_name_or_id> | To delete a docker image from local |
docker build -t <image:tag> . | To create/build a docker image |
docker tag -f <image:oldtag> <image:newtag> | To tag a docker image |
docker push <registry / image:tag> | To push a docker image to registry |
docker commit [options] <container> [registry:tag] | To create a new image from a container’s change |
docker history <image> | To display the history of an image |
docker search <image> | To search an image from docker hub or registry |
Container – create & control | |
docker run -it <image_name_or_id> | To spin up a docker container in interactive mode |
docker run -d <image_name_or_id> | To create & run a docker container in background |
docker run -d –name=<container_name> <image_name_or_id> | To create & run a docker container by assigning a name as: docker run -d –name=redisdb redis |
docker run -d -p <exposed_port>:<original_port> <image_name_or_id> | To create & run a docker container by exposing a port as: docker run -d -p 6379:6379 redis |
docker rename old_name new_name | To rename the running docker container |
Container – Information | |
docker exec <container_name> env | To get a list of all environment variables of a running docker container |
docker exec -it <container_name> /bin/bash | To attach an already running container to run commands inside a docker container |
docker update [options] <container> | To update the docker resources like: CPU, memory etc |
docker inspect –format ‘{{ .NetworkSettings.IPAddress }}’ <container_id_or_name> | To get the IP of current running docker container by passing the container ID or name |
docker ps -l -q | To get the latest container ID of running docker container |
Operations | |
docker start <container_name_or_id> | To start a docker container |
docker stop <container_name_or_id> | To stop a docker container |
docker restart <container_name_or_id> | To restart a docker container |
docker pause <container_name_or_id> | To pause a running container |
docker unpause <container_name_or_id> | To unpause a container |
docker rm <container_name_or_id> | To delete a docker container |
docker rm $(docker ps -a -q -f status=exited) | Batch deletion of docker containers which are in state of “exited” |
docker attach <container_name_or_id> | To connect to a running container to view ongoing output or control it interactively |
Stats & Monitoring | |
docker ps | To check the current running docker containers |
docker ps -a | To check all docker containers which we ran |
docker logs -f <container> | To check the logs of a running container |
docker port <container> | To get the public facing port of container |
docker top <container> | To get the running process of a container |
docker stats <container> | To display live stream of container resource usage statistics |
docker diff <container> | To inspect changes on a container’s filesystem |
docker export –output=”latest.tar” <container> | To export a container’s filesystem as a tar archive |