Some Notes Regarding Docker, Kubernetes, and Azure Pipelines

Some Notes Regarding Docker, Kubernetes, and Azure Pipelines

Docker Compose

What is Docker Compose?

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

  • In docker-compose.yml file, we will list all services (web API, frontend, DB, etc.)
  • Check the validity of YML file by running docker-compose config (path or space if inside the path where the file is)
  • Fix any issue in the docker-compose.yml file
  • Run all services (containers) by running docker-compose up -d
  • Stop all services (containers) by running docker-compose down

Install Docker Compose

sudo curl -L "https://github.com/docker/compose/releases/download/1.24.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose

Here’s where you can find more info about Docker Compose as well as how to build docker-compose.yml.

For more information about how to work with Docker like pulling a Docker image, running a Docker image, and working with containers, see Docker for beginners.

Docker Multi-Stage Build and Docker Builder Pattern

Docker Builder Pattern

It involves two Docker images with two Dockerfiles. One for each image:

  1. “build” image with all the build tools installed, capable of creating production-ready application files.
  2.  “service” image capable of running the application. 

Docker Multi-Stage Build

Multi-stage builds are a new feature requiring Docker 17.05 or higher. It helps to optimize Dockerfiles while keeping them easy to read and maintain. So, I can have multiple From in the same Dockerfile, which means I can implement a builder pattern without maintaining many Dockerfiles.

We define each stage using AsStage Name in the From statement. e.g

  •  From myImageName As build
  • From myImageName AS test 

I can also use the stage as my base image instead of using image e.g:

  •  From myImageName As build 
  •  From build As test 

So I create the stage for a purpose, for example, to build the application, to unit test the application, and then copy my application or test result (artifacts) to a mounted volume, then copy artifact from mounted volume to another stage if needed. In the final stage, I create the image that I will deploy to the prod.

The main idea is that if I have a stage image, I don’t need it as a stored image. I just need to have it on the fly to produce some files and that’s it. I only create and store images that I need to be deployed to an environment

Docker Create

The docker create command creates a writeable container layer over the specified image and prepares it for running the specified command. The container ID is then printed to STDOUT. This is similar to docker run -d except the container is never started. You can then use the docker start <container_id> command to start the container at any point.

This is useful when you want to set up a container configuration ahead of time so that it is ready to start when you need it. The initial status of the new container is created.

More info about Docker multi-stage build and Docker create here. For more information about how to work with Kubernetes cluster and deploy it to Azure Kubernetes Service (AKS) and work with Azure Container Registry, see Kubernetes cluster for beginners.

COPY vs. ADD in a Dockerfile

COPY and ADD are both Dockerfile command that serve similar purposes. Copy files from a specific location into a Docker image.

COPY takes in a src and destination. It only lets you copy in a local file or directory from your host (the machine building the Docker image) into the Docker image itself.

ADD lets you do that, too, but it also supports two other sources. First, you can use a URL instead of a local file/directory. Secondly, you can extract a tar file from the source directly into the destination.

Copy Files Over SSH task (Azure Pipeline)

Use this task in a build or release pipeline to copy files from a source folder to a target folder on a remote machine over SSH.

SSH Task (Azure Pipeline)

Use this task in a build or release pipeline to run shell commands or a script on a remote machine using SSH. This task enables you to connect to a remote machine using SSH and run commands or a script.

Shell Task (Azure Pipelines)

Use this task in a build or release pipeline to run a shell script using bash.

Links:

Bash and PowerShell reference

from DZone Cloud Zone

Sharing is caring!

Comments are closed.