Docker: Platform for Developing, Shipping, and Running Applications

  • Post category:Containers
  • Post last modified:July 30, 2024

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 Architecture

  • 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 CommandUsages
INFO
docker infoTo get the detailed information about docker
docker –versionTo check the “installed docker version”
docker-compose –versionTo check docker-compose installed version
docker-machine –versionTo check docker-machine version
Images
docker imagesTo 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_nameTo rename the running docker container
Container – Information
docker exec <container_name> envTo get a list of all environment variables of a running docker container
docker exec -it <container_name> /bin/bashTo 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 -qTo 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 psTo check the current running docker containers
docker ps -aTo 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
 

 

Ashutosh Dixit

I am currently working as a Senior Technical Support Engineer with VMware Premier Services for Telco. Before this, I worked as a Technical Lead with Microsoft Enterprise Platform Support for Production and Premier Support. I am an expert in High-Availability, Deployments, and VMware Core technology along with Tanzu and Horizon.

Leave a Reply