What the hell is Docker?

Tram Ho

Recently, I have heard about docker, heard yang rumor seems to be “O cloud zing gut peak” so I also researched and found it “true cloud zing gut tip” true =)). Let’s take a look at what the hell it is that yang rumored so much.

So what is docker?

Docker is a platform for developers and sysadmin to develop, deploy, and run applications with containers. It allows the creation of independent and separate environments for launching and developing applications and this environment is called the container. When you need to deploy to any server just run the Docker container, your application will be launched immediately.

What??

Suppose I have an app code I use Rails, MySQL, nginx … when the code is finished, I want to deploy for the user to use. In order to run on the server, we also have to install Ruby, Rails, MySQL … maybe we install another version locally or install some missing libraries that our app cannot run. That is why there is a saying “my computer is still running normally”. The other thing is that the installation is also time-consuming. Or suppose we want to give the app to a friend to show off but it doesn’t install the libraries needed for the program to run so how to run ??. That is also the reason that docker was born.

Benefits of Docker

  • Unlike a Docker virtual machine that starts and stops within seconds.
  • You can launch the container on each system you want.
  • Containers can be built and removed faster than virtual machines.
  • Easy to set up working environment. Just config only once and never have to reinstall dependencies. If you change machines or someone is new to the project, you just need to get that config and give it to them.
  • It keeps your word-space cleaner when you clear the environment that affects other parts.

Setting

You can install it here: download link Select the installation corresponding to your operating system and install according to the instructions for Linux and Windows and MacOS you just need to download and install as everything. Another application. After the installation is complete to check if the installation is successful or not? Open the command line:

Some concepts

  • Docker Client : is how you interact with docker through a command in the terminal. Docker Client will use the API to send commands to Docker Daemon.
  • Docker Daemon : is the Docker server for requests from the Docker API. It manages images, containers, networks, and volumes.
  • Docker Volumes : Best for persistent data storage for use and creation of apps.
  • Docker Registry : is the private repository of Docker Images. Images are pushed into the registry and the client pulls images from the registry. You can use your own registry or the registry of your provider such as: AWS, Google Cloud, Microsoft Azure.
  • Docker Hub : is the largest Registry of Docker Images (default). Images can be found and hosted on Docker Hub (free).
  • Docker Repository : is a collection of Docker Images with the same name but different tags. Example: golang: 1.11-alpine.
  • Docker Networking : allows to connect containers together. This connection can be on 1 host or more hosts.
  • Docker Compose : is a tool that allows to run apps with multiple Docker containers more easily. Docker Compose allows you to configure commands in the docker-compose.yml file for reuse. Available with Docker installed.
  • Docker Swarm : to coordinate container deployments.
  • Docker Services : containers in production. A service only runs an image but it encrypts the way to run the image – which port to use, how many copies of the container to run so that the service has the necessary and immediate performance.

Basic components of docker

Docker has 4 basic components:

  • Image
  • Container
  • Docker Engine
  • Docker Hub

  1. Image Is an image file, the base file of an operating system, a platform, a language (eg ubuntu image, ruby ​​image, rails image, mysql image …)

    From these images, you will use it to create containers.

    The images are read-only files.

    You can also create your own images.

    An image can be created from many other images (eg you create an image running ubuntu, with pre-installed ruby ​​2.3 and rails 5, your image is made up of 3 other images).

  2. Container A virtual machine that is composed of an image and has a writable-file-layer decorated. Changes made in this virtual machine (install software, create more files …) will be saved in this decoration layer.

    These containers will share the resources of the system (RAM, Disk, Network …), therefore, your containers will be very light, booting up, connecting, interacting will be very quick.

    If mapped to the object direction, the image is the class, and the container is the instance-1 instance of that class. From 1 class we can create many instances, similarly, from 1 image we can also create many completely identical containers.

  3. Docker Engine manages whether you create images, run containers, use existing images or download images that don’t already exist, connect to containers, add, edit, delete images and containers, etc., countless
  4. Docker Hub Is a page sharing images, it’s like github or bitbucket.

Dockerfile

  • Dockerfile is a config file for Docker to build the image. It uses a basic image to build the initial image class. Some basic images: python, unbutu and alpine. Then if there are additional layers then it is stacked on top of the base layer. Finally a thin layer can be stacked on top of other previous layers.
  • The config:
    • FROM – specifies the original image: ruby, unbutu, alpine …
    • LABEL – Provides metadata for the image. Can be used to add maintainer information. To see the labels for the images, use the docker inspect command.
    • ENV – set an environment variable.
    • RUN – Can create a command when building image. Used to install packages into containers.
    • COPY – Copy files and folders into the container.
    • ADD – Copy files and folders into the container.
    • CMD – Provide a command and argument for the executable container. Parameters can be overridden and only one cmd.
    • WORKDIR – Set up working directory for other directives such as: RUN, CMD, ENTRYPOINT, COPY, ADD, …
    • ARG – Defines the variable value used during image build.
    • ENTRYPOINT – Provides commands and arguments for an executable container.
    • EXPOSE – declares the listening port of the image.
    • VOLUME – creates a directory mount point for accessing and storing data.

Demo

Create dockerfile:

In Dockerfile for rails application, I need a basic image from Dockerhub.You need to find yourself a suitable basic image in dockerhub, you can search with keywords like rails or ruby, there will be many instructions to write a suitable Dockerfile file.

FROM ruby:2.7.1

This line pulls the ruby2.7.1 image from dockerhub, which will contain the essentials like the core libraries for a ruby ​​application. If there is an image on the host, it will use the image on the current host, for example FROM my_image

RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs

Effect: Running the commands during the build process, here I need to install some necessary environment settings

RUN mkdir -p /usr/src/app And create a directory for the app to run, and declare this directory into WORKDIR

ENV RAILS_ENV='development' ENV is the place to declare environment variables for the application.

COPY . /usr/src/app This will copy the entire code to the / usr / src / app directory of the image

As long as we have all the code of the rails application in the image. Next will build the rails app through the necessary commands.

RUN bundle install Run main process

CMD ["rails", "s"] And expose will define the port that communicates with the container

EXPOSE 3000

So we have a Dockerfile, next need to build the image, run the command in the following format to build the image:

docker build [OPTIONS] PATH | URL | -

trembling

docker build -t notification-api:dev .

Never see the following message that you have successfully built.

Basic commands in docker

  • List image / container:

    $ docker image/container ls

  • Delete image / container:

    $ docker image/container rm <tên image/container >

  • Delete all existing images:

    $ docker image rm $(docker images –a –q)

  • List all existing containers:

    $ docker ps –a

  • Stop a specific container:

    $ docker stop <tên container>

  • Run the container from the image and change the container name:

    $ docker run –name <tên container> <tên image>

  • Stop all container:

    $ docker stop $(docker ps –a –q)

  • Delete all existing containers:

    $ docker rm $(docker ps –a –q)

  • Show log a container:

    $ docker logs <tên container>

  • Build an image from the container:

    $ docker build -t <container name>.

  • Create a container running in the background: `

    $ docker run -d <image name>

  • Load an image on the docker hub:

    $ docker pull <tên image>

  • Start a container:

    $ docker start <tên container>

Conclude:

Because I am new to learning, I am still primitive to ask for your comments.

References:

https://docs.docker.com

https://medium.com/swlh/what-exactly-is-docker-1dd62e1fde38

Share the news now

Source : Viblo