Docker – summarizes some basic knowledge

Tram Ho

1. Docker

1.1 Introduction

Docker is a platform that gives us the tools and services to build, deploying and running our programs easily and quickly by containers. The use of containers to deploy applications is called containerization.

1.2 Docker engine

Docker Engine is a client-server application .

Main components of Docker Engine include:

  • Server , also known as docker daemon (dockerd) : is responsible for creating and managing Docker objects such as: image , container , network , volume .
  • REST API : Provides interfaces that programs can use to call the docker daemon and instruct what it will do.
  • CLI (Command Line Interface) client : is a set of commands to use api for the client to manipulate Docker.

1.2 Some concepts

  • Docker Client: when you interact with the docker via a command on the terminal, the docker client will use the API and send commands to the Docker Daemon .
  • Docker Deamon: this is the Docker server that will issue requests from the Docker API, which is responsible for creating and managing Docker objects such as: image , container , network , volume
  • Docker Registry: this is the private storage area of ​​Docker Images. The most common is the Docker Hub or you can build your own Registry.
  • Docker Hub: In a docker image , it’s like a github, which is where the docker image , you can search for the image you need or store your own image on Docer Hub.
  • Docker Repository: this is a collection of Docker images with the same name but different tag.
  • Docker Networking: Functions to connect containers together. They can connect on one host or many different hosts.
  • Docker Compose: is a tool that allows you to run applications with multiple Docker containers 1 quickly and easily.
  • Docker Services: these are containers included in production, and with only one service you can only run one image but it encodes the way to run the image.

2. Install docker

Installation instructions are here

3. Working with Docker

Perhaps you already know, if you want to know which one, I just use --help

For example, if you want to see using docker guy, docker --help will show you his commands. and instructions for use.

3.1 Docker Image

3.1.1 Concept

Docker image is a software, a packaged service. It can be operating system, system environment or programming language such as ubuntu, macOS, centos, nginx, mysql, ruby, php, python …

In docker, images can only be read but cannot be edited.

When Image is launched by Docker, its executable version is called a container.

3.1.2 Frequently used commands

  • Perhaps you already know, if you want to know which one, I just use --help .

docker image --help it shows for docker commands for image operations. And want to be more specific what the command does, then type the same command. e.g. docker image tag --help .

1. List existing images

2. Pull an image from the Docker Hub

  • Note:
    • By default, without {tag} , it will load the lasted version
    • Go to the docker hub to find the images you need available: https://hub.docker.com/

3. Delete an image

  • Note:
    • You do not need to write all the image_id, just the first characters of image_id you can delete it then for example: image_id is 77be327e4b63 , you just need docker rmi 77 is enough. But make sure the first few characters are uniq. If not, the wrong image is deleted, the size =))
    • Sometimes an image that is referencing it adds the -f option to force the deletion

3.2 Docker Container

3.2.1 Concept

The container is an executable version of the image. If interpreted by OOP, image is a class and container is an instance of that class. And from an image we can also create 1 or more completely identical containers.

You can understand that software such as ruby, rails, mysql … are put into separate containers. or bundle all the software your application needs in a bucket, and your application can run in that bin, because it already has everything it needs to run your application. The box is not affected from outside and vice versa, it does not affect outside.

So containers have the following advantages:

  • Flexible: Any application can containerize. 1 software can be packed into 1 container, many software for one application can be packed into 1 container …
  • Lightweight: Containers also use images together, so they don’t consume much memory.
  • Mobile: You can build it locally and deploy it to other computers and servers.
  • Identity: When multiple people develop on the same project, there is no environmental difference.
  • Fast: Due to shared host OS, containers can be created almost instantly. This is different from vagrant – creating virtual environments at the hardware level, so booting takes more time.

3.2.2 Commonly used commands

1. Build container from image

  • Note: Some options
    • -it to allow the newly created container to receive interaction ( -i ), and connect to terminal ( -t ).
    • --name container_name of the container you want after launch. By default, if not, it will automatically name the container as a name j. so it’s best to set yourself easy to remember: v
    • -p {host_port}:{container_port} , -h {container_host} optional
    • There are many other options when used, I note later

2. start / stop a container Launch the built container we have

  • Note: You can use the docker exec to stand from the host and assign the container to execute the command.

    Example: Standing from the host, and listing all the files contained in container_1, we have the command: docker exec container_1 ls

3. Exit the container you are accessing

4. List containers

5. Delete a container

6. Rename a container

7. View changes on containers

8. Save the container as image

In case you want to save your container as an image for convenient sharing with others or to install on another computer, use the following command.

  • Note:
    • Must stop the container before you save.
    • {image_name}:{tag} name the image and version for the container after saving.

Save the image as a file for easy sharing

  • Note:
    • filename.tar will be saved at the position you are in the terminal

Load image from file to use

  • Note:
    • When loading the image from the file, the image_name and image_tag will be empty to name and tag the image using the command

3.3 Data Volume

3.3.1 Concept

Data volume is used to share data, usually we will use for the following cases:

  • To keep data of container
  • To share data between the host and the container doker
  • To share data between different containers

3.3.2 Sharing data through a folder

1. Between host and container

For example:

2. Between containter with each other:

To create a container named C2 and share C1 data together (/ home / share_data) we use the command

3.3.2 Share data via drive

1. Check for an existing drive

2. Create a new Volume drive

3. Check the Volume drive information

4. Delete the Volume drive

5. Share data via Volume drive

  • Note:
    • --mount parameter to assign a drive to
    • source=Disk_1 the drive letter you want to assign
    • target=/home/disk_1 location where Disk_1 drive is mapped to / container / home directory of disk_1

6. Share Volume drive data with host and container

Create a mapping drive with the host

Assign drive to the container

  • Note: Once you have mapped a drive to a host, you will not use --mount but -v

References

https://docs.docker.com/get-started/

https://www.youtube.com/playlist?list=PLwJr0JSP7i8At14UIC-JR4r73G4hQ1CXO

https://kienthuclinux.com/linux-tool/tim-hieu-ve-docker-phan-3-kien-truc-va-thanh-phan-cua-docker/

https://theanhgroup.com/docker-la-gi-dac-diem-cua-docker-nhu-the-nao.html#loi-ich-cua-docker-mang-lai

Share the news now

Source : Viblo