Docker and popular questions

Tram Ho

What is docker and what is it used for?

According to Wikipedia:

Docker is an open source project that automates the deployment of Linux and Windows applications into virtualized containers.

It sounds abstract and confusing. Imagine this, on a beautiful day you are assigned to a new PHP project, you are excited to fly right into the code and read the Readme.md file, oh well … The project requires installing a bunch of things like are PHP, MySql, Nginx, Redis … and so on. Not to mention the PHP and MySql versions are completely different from the version in your computer, can you still use them or will they generate bugs in the sky because of the conflict version? So which way for me. 😦. Is there a way to run the code without installing that messy pile … .. The main solution is Docker. With Docker, things become much simpler, with just a few lines of code, you can quickly create môi trường ảo hóa contains all the settings needed to run the project. : 3 So what is Docker?

Docker is a tool that creates an “encapsulated” environment (also called a Container) on a computer that does not affect the current environment of the device.

Let’s find out …

What is the difference between Docker and Hypervisors?

What is Hypervisors?

Hypervisor is a software application that is responsible for running multiple virtual machines on a system, each virtual machine runs its own OS, it directly interacts with hardware resources. Some of the names using this technology are VitualBox, or VMware, you may have heard.

The difference between Docker and Hypervisors

Hypervisors

  • When using it, you need to add a virtual machine using a separate operating system, and give it a certain amount of hard drive and Ram. This means that even if you don’t do anything, the host computer still has to distribute resources that lead to waste.
  • Boot and shutdown time is also quite long, depending on the strength of the machine, can even up to several minutes.

Docker

  • Using containerlization technology, by using this technology, on one server we can generate multiple clients, but different from Hypervisors, which are used by the parent machine and share resources. of the parent machine. Therefore, there will be no waste of resources, how much the machine needs to be allocated, and the boot time is much faster.

Docker Image and Docker Container?

Docker Image

Docker Image is the foundation of Container, helps to shape Containers An Image includes operating system (ubuntu, debian, windows) and pre-installed specialized programming environments (PHP, MySQL, Nginx, Python, Rail ….) . Users can create their own images, or use other people’s images on the Docker Hub repository.

Docker Container

Docker Container can say it is similar to a virtual machine, obtained by using the command to launch Image. It is possible to understand Container as a bucket, in that container contains all the environments that the programmer needs to use to run the project, all operations in that bucket do not affect the external environment.

How to create a Docker Image?

As I mentioned above, Docker Hub is a repository, a place to store and share images, it is very simple, just go to Docker Hub homepage and type the image you want to find (here I have an example of MySQL Image) ) To retrieve, we just need to type the command: docker pull mysql

And then check the image list with the command: docker images

So how can I create an image and public on Docker Hub?

We need to use Dockerfile: Dockerfile is a text file without a tail. This file contains a list of commands that help set up the structure for the Image, stipulating where the Image is initialized, including which environments. To create a Docker Image, we need the following files:

The script.sh file contains the commands that are run when the container is turned on, the File Dockerfile has the following content:

File Script.sh :

Inside:

FROM : used to declare the parent image that I use, after build image and Docker read this command, it will look in the machine if it already has the image (in my example is ubuntu), if not, it will Go to the Docker hub and automatically pull back.

RUN : Used to run commands in the image build process, here I use to install Nginx and edit permissions for folders /venv

WORKDIR : define directory for CMD inside container, right after run Image into Container and access terminal in Container, we will be in this folder.

COPY : Copy the file on the host machine to the Container folder (here we copy the script.sh file to the venv folder of the Container , so that after the Container is formed will run into the script.sh file inside that Container)

ENTRYPOINT : The Navigation container runs into the cript.sh file when the Container is initialized

EXPOSE : Container will listen to the specified network port when running

Done creating Dockerfile, followed by any build image:

sudo docker build -t ubuntu-nginx .

Inside :

ubuntu-nginx is the image name.

The -t option here specifies the option to name an tag in the format: name:tag , otherwise the default designation will be the latest .

Ok, let’s see what result is: sudo docker images

-> Already have Image , let’s run it together:

Tarraaaaaa ☀️, after the created Container ran straight into the script.sh file, you can use this file to run the command (usually to start nginx, mysql, redis …).

Frequently used commands to manipulate Docker Image and Docker Container?

Command to manipulate Docker Image

  • docker images : The command checks the list of installed images
  • docker rmi <id/Name> : The command to delete an unopened image based on id or name
  • docker rmi -f <id/Name> : The command deletes an image even when running based on id or name

Command to manipulate Docker Container

  • docker ps : The command lists the running containers
  • docker ps -a : lists the running and disabled containers
  • docker inspect { container_id } : See the details of the container created
  • docker logs { container_id } : see container history
  • docker rm <id/Name> : delete 1 container based on id or name
  • docker rm -f <id/Name> : delete 1 container, even running
  • docker rm $(docker ps -a -q) : delete all containers
Share the news now

Source : viblo.asia