What is Docker?
In the official website here how Docker is defined: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.
When you use Docker, you are creating and using two main artifacts: images and containers.
What is a Docker Image?
A Docker image is a read-only template with instructions for creating a Docker container. For example, you may build an image which is based on the Ubuntu image but installs the Apache web server and your application, as well as the configuration details needed to make your application run. Often, an image is based on another image, with some additional customizations. You might create your own images or you might only use those created by others and published in a registry. To build your own image, you create a Dockerfile with a simple syntax for defining the steps needed to create the image and run it. Each instruction in a Dockerfile creates a layer in the image. When you change the Dockerfile and rebuild the image, only those layers which have changed are rebuilt. This is part of what makes images so lightweight, small, and fast when compared to other virtualization technologies.
What is a Container?
Docker is mainly based on the Container concept. According to the official website, a Container is:
a runnable instance of a docker image. You can create, start, stop, move, or delete a container using the Docker API or CLI. You can connect a container to one or more networks, attach storage to it, or even create a new image based on its current state. By default, a container is relatively well isolated from other containers and its host machine. You can control how isolated a container’s network, storage, or other underlying subsystems are from other containers or from the host machine. A container is defined by its image as well as any configuration options you provide to it when you create or start it. When a container is removed, any changes to its state that are not stored in persistent storage disappear.PHOTO
How to install Docker
Docker official documentation contains detailed steps to install it, I suggest following the instructions reported there to install the product on your machine. I suggest also to read the Getting Started guide to get familiar with commands of Docker CLI.
Docker cheat sheet
When you start working with Docker you need to get familiar with basic commands that are critical for your job. For this reason, I created this cheat sheet on the most important commands that I use every day.
To list all the available images you can use the following command:
docker image ls
At the very beginning, the list is empty. Now suppose you want to download an Ubuntu 14.04 (Trusty) image and store it on your system. The command to run is the following:
docker pull ubuntu:14.04
Now if you run the "docker image ls" command the following will appear:
ubuntu 14.04 a35e70164dfb 1 minute ago 222MB
You have installed the image on your system. You can use it to start a container using "ubuntu" as name:
docker run -it ubuntu /bin/bash
A bash shell will start inside this container. Now you are inside an Ubuntu sandbox completely isolated from your host system. On this sandbox, you can run whatever Ubuntu command. In another host shell you can run the command to list all docker containers running on your system:
docker container ls
Whenever you close a container you lost all the changes you applied to it. In order to avoid this, you can create a snapshot of the current container state so you can recover it whenever you need it. The command to do that is:
docker commit <container id> <tag>
A new image <tag> is created with the status the container had when you committed it.
How to build an image from a Docker file
The strategy to commit containers to avoid to lose the work you have done on a docker image is a good strategy but sometimes you need to create a custom image starting from a basic one downloaded from a docker registry and share it with your team or customer or simply using it as starting point for your activity. In this case could be useful to create it using a Docker file and the Docker build capabilities. For example, in order to install PostgreSQL, we need that our Docker image contains some prerequisites like:
- wget, to download the package via HTTP;
- unzip, to extract zip file;
You can write a Docker file with the following content.
FROM ubuntu:14.04
RUN apt-get update
RUN apt-get install -y wget unzip
FROM is the keyword used by Docker to specify the docker image to use as starting point. In our example, we start from Ubuntu 14.04. In order to download software packages, you need to run the Ubuntu apt-get update command. RUN is the keyword used by Docker to run a command on the target image. Then we installed the three packages as prerequisites.
To build this image you can run the following shell command:
docker build -t <tag> .
Docker will build the Dockerfile in the current folder. If you issue the "docker image ls" command, you'll notice the new image <tag> appeared in the list. To run the docker image just created give the following command:
docker run -it <tag> /bin/bash
A new docker container is started with a bash shell. In this image, you can use the wget and unzip commands required to install the SQLite software.