Learn about Docker.

Tram Ho

Introduce

Docker is a tool that makes creating and deploying containers to develop, run applications easily. Containers are the environment in which programmers put in the components needed for their applications to run, by packaging the application with such containers, it ensures the application runs and is the same in different machines (Linux, Windows, Desktop, Server …)

Docker seems very similar to virtual machines (many people have created virtual machines with virtualization tools like Virtual Box, VMWare), but there is a difference with VM: instead of creating the whole system (though virtualization), Docker gives Allows the app to use the operating system kernel that is running Docker to run the application by adding the missing components provided by the container. This will increase performance and reduce application size.

Who uses Docker? Docker benefits both programmers and system administrators, using Docker programmers to focus on writing code, not worrying about deployment, not worry about programmers’ computers running, machines otherwise can’t run …

1. What is Docker?

Docker is a platform to provide a way to easily build, deploy, and run applications on a virtualization platform. There are many other container technologies such as Solaris Zones, BSD jails, and LXC. But why does Docker grow and spread quickly? These are the causes:

  • Ease of use: Docker is easy for everyone to use from developers, systems admins, architects … v … v .. it takes advantage of containers to build and test quickly. Can pack applications on their laptops and run on public cloud, private cloud..vv … The mantra is “Build once, run anywhere”.
  • Speed: Docker container is very light and fast, you can create and run the docker container in seconds compared to VMs, each time running VMs takes a lot of time to start up.
  • DockerHub: is an “app store for docker images”. On DockerHub there are thousands of public images created by the community. Easily find the images you need and just pull back and use them with a few minor modifications.
  • Modularity and Scalability: You can break down the functionality of the application into separate containers. For example, Database runs on one container and Redis cache can run on another while Node.js application runs on another. With Docker, it is easy to link containers together to form an application, making it easy to scale and update components independently of each other.

To go deeper into Docker we need to learn the concepts related to it. Let’s take a look at the model below

  • Images: a template for creating a container. Usually the image will base on another image with additional options. For example, you build an image based on ubuntu image to run your Apache web service and application and customize and configure your application to run. You can build your own image or use published images from the Docker Hub community. An image will be built based on the instructions of Dockerfile.
  • Containers: is an instance of an image. You can create, start, stop, move or delete containers based on the Docker API or Docker CLI.
  • Registry (Docker Hub): is a repository of images published by the Docker community. It is similar to GitHub and you can find the necessary images and pull them to use.
  • Docker Client: is a tool that helps users communicate with the Docker host.
  • Docker Daemon: listen to requests from the Docker Client to manage objects such as Containers, Image, Network and Volumes. Docker Daemons also communicate with each other to manage Docker Services.
  • Dockerfile: is a file containing instructions for building an image.

2. The difference between Docker and Hypervisors.

What are hypervisors?

Hypervisor: It is a combination of either a single software, firmware or hardware. The virtual machine runs on top of the virtualization process and the virtualization process runs on the physical computer. This computer is called the server. All resources like Ram, CPU, etc. are provided by the server.

What is docker?

Docker: This is the technology being evaluated as the future of virtualization technology (future of virtualization). The big difference between Docker and Vagrant (or Virtual Machine in general) is that it significantly saves resources. With docker you can run 20 containers (similar to a small operating system) on the same host machine (host machine) which if used Vagrant will need a server with extremely terrible configuration. Docker does this because it differs from Virtual Machine in that instead of separating between guest and host environments, Docker’s containers share resources with the host machine.

Difference between Docker and Hypervisors

Docker: Shared kernel, runs independently on the Host Operating System and can run on any operating system as well as the cloud. Start and get the application ready to run in 500ms, making it highly feasible for projects that need fast expansion.

Virtual Machine(Hypervisors): Need to add a Guest OS so it will consume more resources and slow down the real machine when used. The average boot time of 20s can be up to minutes, varies depending on the speed of the drive.

3. Difference between Docker Image and Docker Container

What is Docker Images?

The docker image is an image of a development environment (also known as snapshot). In short, we can package our environment settings (OS, package, software, etc.) into a single file, which is the docker image. Once the docker image is available, we can initialize the docker container from the docker image.

If we think of OOP, we can view the docker image as a class, while the docker container is the object / instance of that class! So if we have a docker image on our computer, we can create one or more containers with their internal environment identical.

What is docker container?

Docker Container packed an application software. It is an invisible box that includes everything like operating system, application code, runtime, system tools, system library, etc. Docker Image creates Docker Container. This docker image is only readable. Docker container is a virtual machine that contains the software environment.

For example, 1 container may contain the environment:

  • OS: Ubuntu 16.04
  • Already installed a number of packages such as: git, curl, wget, nano, … (or any package you want depending on the needs you choose image or write the corresponding Dockerfile).
  • Installed the web app you write.
  • The web app is running on port 8080 in that virtual environment.

4. How to create a Docker Image with Dockerfile

What is dockerfile?

Dockerfile is a text file containing a set of commands to create a new Image in Docker.

Some commands in Dockerfile:

FROM <base_image>: < script >: this is a mandatory command in any Dockerfile. It is used to declare base Image which we will build our Image.

MAINTAINER <name_name>: this statement is used to declare on the author that created Image, whether we can declare it or not.

RUN <command>: we use this command to run a command for the installation of the tools needed for our Image.

CMD <statement>: in a Dockerfile, we only have one CMD command, this command is used to determine the execution rights of the commands when we create a new Image.

ADD <src> <dest>: This statement is used to copy a local or remote file (declared by <src>) to a certain location on the Container (declared by dest).

** LABEL: ** provide metadata for the image. Can be used to add maintainer information. To view the labels of images, use the docker inspect command.

ENV <variable name>: defines an environment variable in a Container.

COPY: copy files and folders into containers.

ARG: defines the value of the variable used during the build image.

WORKDIR: Set up the working directory for other directives such as: RUN, CMD, ENTRYPOINT, COPY, ADD, …

EXPOSE: Declare the listening port of the image.

ENTRYPOINT <command>: defines the default commands, which will be run when the container is running.

VOLUME <directory name>: used to access or link a folder in a Container.

5. Demo

Create a dockerfile file:

Proceed to build the Dockerfile file

$ docker build .

$ docker run 4cc010d9d657

6. Conclusion

So I have completed the basic steps to create a Dockerfile file. Hope you guys will succeed, if there’s any problem then comment below to discuss.

Share the news now

Source : Viblo