Docker
by Juan Manuel González Garzón
What a container is and how it differs from a virtual machine, as well as the benefits that containers provide in software development:
A container is an executable unit of software in which application code is packaged, along with its libraries and dependencies, in common ways so that it can be run anywhere, whether on a desktop, on-premises, or in the cloud. To do this, containers take advantage of a form of operating system virtualization in which features of the operating system are leveraged to both isolate processes and control the amount of CPU, memory, and disk storage that those processes have access to. In the case of the Linux kernel, namespaces and cgroups are the operating system primitives that enable this virtualization.
Containers are small, fast, and portable because unlike a virtual machine, they do not need to include a guest operating system in every instance and can simply leverage the features and resources of the host operating system instead.
They are a standardization for how we package and ship software. All the dependencies needed to run an application are packaged with it, making it easy to move it around and run it in a variety of locations.
Containers are lightweight. They share the machine operating system kernel, eliminating the need for a full operating system instance per application. This makes container files small and less taxing on resources. Their smaller size, especially compared to virtual machines, means they can spin up quickly and are better able to support cloud-native applications that scale horizontally.
Containers carry all their dependencies with them, meaning that software can be written once and then run without needing to be re-configured across laptops, cloud, and on-premises computing environments. This portability also provides consistency in your environment so that you don’t have to worry about differing dependencies installed in different environments.
Containers go even further because they also enable a microservice architecture, which means that application components can be deployed and scaled more granularly. This granularity provides an attractive alternative to having to scale up an entire monolithic application when, for example, a single component is struggling with load.
Container Images
Docker is a software platform for building and running applications as containers.
A Dockerfile is the blueprint from which an image is built. The Dockerfile outlines all the steps to be taken to build the desired image; Docker then builds that image.
An image is an immutable file that contains the source code, libraries, and dependencies that are necessary for an application to run. That immutability means that images are read-only; if you change an image, you create a new image. In a sense, images are templates or blueprints for a container.
As Docker instructions are run, new read-only layers are created for the image. Depending on the size of an image, there can be a few layers or many.
# First line of a Dockerfile (Define base image)
FROM
# Execute arbitrary command
RUN
# Set enviroment variables
ENV
# Copy files and directories
# Can copy from an url
ADD
# Ca only copy local files or dirs
COPY
# Define default command for container execution (Only use the last one, only one of these intended)
CMD
FROM ubuntu:18.04
COPY . /app
RUN make /app
CMD python /app/app.py
Container registry
Storage and distribution of named container images
hostname/repository:tag
docker.io/ubuntu:18.04
docker.io is the default hostname
Running containers
FROM node:9.4.0-alpine
COPY app.js .
COPY package.json .
RUN npm install && \
apk update && \
apk upgrade
CMD node app.js
# -t specifies the name for the image
# . === Current directory. In this comand is the build context (where is the Dockerfile)
docker build -t my-app:v1 .
docker images
# REPOSITORY TAG IMAGE ID CREATED SIZE
# my-app v1 b8b30c69b420 2 minutes ago 78.2MB
docker tag my-app:v1 second-app:v1
docker images
# REPOSITORY TAG IMAGE ID CREATED SIZE
# my-app v1 b8b30c69b420 2 minutes ago 78.2MB
# second-app v1 b8b30c69b420 1 minutes ago 78.2MB
docker run my-app:v1
docker push my-app:v1
I. Instalación
Development and test is more efficient, deployment and disaster recovery is greatly simplified, and you’re able to run multiple instances of the app without conflicting with other apps.