docker architecturedocker architecture
Spread the love

Overview

Docker is an open platform for developing, shipping, and running applications. Docker enables you to separate your applications from your infrastructure so you can deliver software quickly. With Docker, you can manage your infrastructure in the same ways you manage your applications. By taking advantage of Docker’s methodologies for shipping, testing, and deploying code quickly, you can significantly reduce the delay between writing code and running it in production.

What is a Container?

A container is a sandboxed process on your machine that is isolated from all other processes on the host machine. That isolation leverages kernel namespaces and cgroups, features that have been in Linux for a long time. Docker has worked to make these capabilities approachable and easy to use. 

To summarize, a container:

  • is a runnable instance of an image. You can create, start, stop, move, or delete a container using the DockerAPI or CLI.
  • can be run on local machines, virtual machines, or deployed to the cloud.
  • is portable (can be run on any OS)
  • Containers are isolated from each other and run their own software, binaries, and configurations.

What is a Container Image?

When running a container, it uses an isolated filesystem. This custom filesystem is provided by a container image. Since the image contains the container’s filesystem, it must contain everything needed to run an application – all dependencies, configuration, scripts, binaries, etc. The image also contains other configurations for the container, such as environment variables, a default command to run, and other metadata.

Quick list of Docker Commands


• docker version – Echoes Client’s and Server’s Version of Docker
• docker images – List all Docker images
• docker build <image> – Builds an image form a Docker file
• docker save <path> <image> – Saves Docker image to .tar file specified by path
• docker run – Runs a command in a new container.
• docker start – Starts one or more stopped containers
• docker stop <container_id> – Stops container
• docker rmi <image> – Removes Docker image
• docker rm <container_id> – Removes Container
• docker pull – Pulls an image or a repository from a registry
• docker push – Pushes an image or a repository to a registry
• docker export – Exports a container’s filesystem as a tar archive
• docker exec – Runs a command in a run-time container
• docker ps – Show running containers
• docker ps -a – Show all containers
• docker ps -l – Show latest created container
• docker search – Searches the Docker Hub for images
• docker attach – Attaches to a running container
• docker commit – Creates a new image from a container’s changes

To display the current version

$ docker –version 

To pull docker repository image

$ docker pull <image>

$ docker pull ubuntu

For generating a container from an image

$ docker run -it -d <image name> 

$ docker run -it -d ubuntu

$ docker run -d-p80:80 docker/getting-started

You’ll notice a few flags being used. Here’s some more info on them:

  • -d – run the container in detached mode (in the background)
  • -p 80:80 – map port 80 of the host to port 80 in the container

Displays running and exited containers

$ docker ps -a

Command for accessing running container

$ docker exec -it <container id> bash 

docker kill <container id> –Example:

Code:

$ docker kill d61153bc

To create a new image of an edited container on the local system

$ docker commit <container id> <username/image name>  

 To push an image to the docker hub repository

$ docker push <username/image name> 

Listing all images stored in a docker

$ docker images 

Deleting a container which has stopped execution.

$ docker rm <container id>

Create a Docker Image

$ docker build -t <application_name> .

Docker Compose

Compose is a tool for defining and running multi-container Docker applications. With Compose, you use a YAML file to configure your application’s services. Then, with a single command, you create and start all the services from your configuration.

Compose works in all environments: production, staging, development, testing, as well as CI workflows. You can learn more about each case in Common Use Cases.

Using Compose is basically a three-step process:

  1. Define your app’s environment with a Dockerfile so it can be reproduced anywhere.
  2. Define the services that make up your app in docker-compose.yml so they can be run together in an isolated environment.
  3. Run docker compose up and the Docker compose command starts and runs your entire app. You can alternatively run docker-compose up using the docker-compose binary.

Compose has commands for managing the whole lifecycle of your application:

  • Start, stop, and rebuild services
  • View the status of running services
  • Stream the log output of running services
  • Run a one-off command on a service

A docker-compose.yml looks like this:

version: “3.9”  # optional since v1.27.0

services:

  web:

    build: .

    ports:

      – “8000:5000”

    volumes:

      – .:/code

      – logvolume01:/var/log

    links:

      – redis

  redis:

    image: redis

volumes:

  logvolume01: {}

Automated testing environments

An important part of any Continuous Deployment or Continuous Integration process is the automated test suite. Automated end-to-end testing requires an environment in which to run tests. Compose provides a convenient way to create and destroy isolated testing environments for your test suite. By defining the full environment in a Compose file, you can create and destroy these environments in just a few commands:

$  docker-compose up -d

 $ ./run_tests

 $ docker-compose down

 

Setting Up Superset Using Docker

If you are familiar with Docker, then getting starting with Superset is super easy.

It is a two-step process.

Step 1 Clone Superset Repo

The first step is to clone the latest stable version of the superset, as follows:-

$ git clone https://github.com/apache/superset.git

Step 2 Starting Superset in Non-Dev Mode with PostgreSQL

$ docker-compose -f docker-compose-non-dev.yml up

Samples Tailored to Demo Compose

These samples focus specifically on Docker Compose:

Quickstart: Compose and Django – Shows how to use Docker Compose to set up and run a simple Django/PostgreSQL app.

Quickstart: Compose and Rails – Shows how to use Docker Compose to set up and run a Rails/PostgreSQL app.

Quickstart: Compose and WordPress – Shows how to use Docker Compose to set up and run WordPress in an isolated environment with Docker containers.

By Hassan Amin

Dr. Syed Hassan Amin has done Ph.D. in Computer Science from Imperial College London, United Kingdom and MS in Computer System Engineering from GIKI, Pakistan. During PhD, he has worked on Image Processing, Computer Vision, and Machine Learning. He has done research and development in many areas including Urdu and local language Optical Character Recognition, Retail Analysis, Affiliate Marketing, Fraud Prediction, 3D reconstruction of face images from 2D images, and Retinal Image analysis in addition to other areas.