Docker is a way to simplify the process of building applications, shipping them, and then running them in different environments.
Image
Image is a file that's essentially a snapshot of a container. Images are created with the build command.
docker build -t rraj/nodeexpress .
The above command will create a image using Dockerfile and tag that image as rraj/nodeexpress. When you build a new image, Docker does this for each instruction (RUN, COPY etc.) in your Dockerfile:
- create a intermediate (temporary) container from the previous image layer (or the base FROM image for the first command.
- run the Dockerfile instruction in the temporary "intermediate" container;
- save the temporary container as a new image layer.
- remove intermediate container
The final image layer is tagged with whatever you name the image. Once the image is built, you can view all the layers that make up the image with the docker history command.
docker history rraj/nodeexpress
The intermediate image will not show when you run docker images, but these are cached, so if you build it again it will use cache until it find new instruction after which it will create intermediate contain and follow the same step for subsequent instruction. Try this by changing one of the instruction.
Pull
Because image can become quite large, images are designed to be composed of layers of other images, allowing a minimal amount of data to be sent when transferring images over the network. Layers can be reused by images. Run docker pull node:6.14.3 and then run docker pull node:8.11.3, you will notice that some of the layer already exist and it will not pull them and it will show them as Already exists.
Container
Image is like a blue print which isn't very useful of it own, so you use them to create running instance of container. Container are actually where the live application run. So image is a read only template with layering, for example you will have a layer specific to your linux (or windows) OS and then you will have layer for your application framework like asp.net core or nodejs and so on. Once you have an image you can build an isolated container. Image and file system they have is read only and can be shared across multiple containers, whereas container has thin read/write layer
docker run -e 'ACCEPT_EULA=Y' -e 'SA_PASSWORD=mysecurepwd' -p 1433:1433 -d microsoft/mssql-server-linux
Remove unused data
docker system prune -a
Benefits of Using Docker
Build it once and run it anywhere
You develop an application using programming run-time (like nodejs, .net) on you dev box and then you add some dependencies to it like angularjs, newtonsoft, entityframework etc. The instruction on how to do this will differ depending on what OS you are using MacOS, Linux or Windows or may be even between different version of same OS. This raises a big complexity in developing application and sharing and deploying it to different environments. With docker you create a docker file which you can think of it as a recipe for how to set up your application. You only need to define this file once and then build it. Once that Docker image is built, all you have to do is move it to another machine and run it.Resource efficiency and density
Since docker does not require guest OS as compared to VM, you can run many more containers than VMs on a single server.Effective isolation and resource sharing
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. Docker uses a technology called namespaces to provide the isolated workspace called the container. When you run a container, Docker creates a set of namespaces for that container. These namespaces provide a layer of isolation. Each aspect of a container runs in a separate namespace and its access is limited to that namespace.Speed
Starting, creating, replicating or destroying containers is very fast. Run following command and you will have sql server instance. Also if you have image already on your m/c running a container will be in secondsdocker run -e 'ACCEPT_EULA=Y' -e 'SA_PASSWORD=mysecurepwd' -p 1433:1433 -d microsoft/mssql-server-linux
Handy Command
List Images
docker images
Remove Image
docker rmi <few character of images id>
Launch docker container
docker run <image name>
Options
-d - detached mode
-name - name container
--rm - automatically clean up container and remove file system when container exists
-it - For interactive processes like a shell (docker run -it ubuntu /bin/bash)
-p hostport:containerport - Expose container port to host post
-e - To set environment variable refer this for automatically set environment variable in Linux
--memory=27m Max amount of memory a container can use
--cpus=".1" Specify how much of the available CPU resources a container can use.
--memory=27m Max amount of memory a container can use
--cpus=".1" Specify how much of the available CPU resources a container can use.
Other container command
docker ps -a
docker ps -as #with container size
docker stop <container>
docker rm <container>
docker inspect <container>
docker container logs -f <container> # follow documentation for more options
docker stats <container name>
Start a shell in a container
docker ps -a
docker ps -as #with container size
docker stop <container>
docker rm <container>
docker inspect <container>
docker container logs -f <container> # follow documentation for more options
docker stats <container name>
Start a shell in a container
docker exec -it <containername> /bin/bash
Remove unused data
docker system prune -a