One introduction to Docker is enough!

Article catalog

1 Introduction to docker

Docker is an open source application container engine, which is based on Go language and complies with Apache 2.0 protocol.

Docker allows developers to package their applications and dependencies into a lightweight, portable container, and then publish them to any popular Linux machine. It also enables virtualization.

1.1 advantages of docker

1.1.1 fast and consistent delivery of applications

Docker simplifies the development life cycle by allowing developers to work in a standardized environment using local containers of your provided applications or services.
Containers are ideal for continuous integration and continuous delivery (CI / CD) workflows, consider the following example scenario:

  1. Your developers write code locally and use the Docker container to share their work with colleagues.
  2. They use Docker to push their applications into the test environment and perform automatic or manual tests.
  3. When developers find errors, they can fix them in the development environment and then redeploy them to the test environment for testing and validation.
  4. After testing, pushing the patch to the production environment is as simple as pushing an updated image to the production environment.

1.1.2 responsive deployment and expansion

Docker is a container based platform that allows for highly portable workloads. Docker containers can run on developers' local machines, physical or virtual machines in data centers, cloud services or hybrid environments.
Docker's portability and lightweight features also enable you to easily complete the workload of dynamic management, and expand or dismantle applications and services in real time according to business requirements.

1.1.3 light and efficient

Docker is light and fast. It provides a feasible, economic and efficient alternative for virtual machine based on hypervisor, so you can use more computing power to achieve business goals. Docker is very suitable for high-density environment and small and medium-sized deployment, and you can do more with less resources.
 

1.2 Docker architecture

1.2.1 Image image

Docker Image is a root file system. For example, the official Image ubuntu:16.04 It contains a complete set of Ubuntu 16.04 minimum system root file system.

1.2.2 Container

The relationship between image and Container is just like the class and instance in object-oriented programming. Image is a static definition, and Container is the entity of image runtime. Containers can be created, started, stopped, deleted, paused, and so on.

1.2.3 Repository

The warehouse can be seen as a code control center for storing images. Similar to Maven's warehouse.

 

2 Docker installation

2.1 CentOS installation

2.1.1 one key installation

# Official script (using Alibaba image):
curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun

# One click installation of daocloud in China:
curl -sSL https://get.daocloud.io/docker | sh

2.1.2 manual installation

# If you have an older version, uninstall it first
sudo yum remove docker \
                  docker-client \
                  docker-client-latest \
                  docker-common \
                  docker-latest \
                  docker-latest-logrotate \
                  docker-logrotate \
                  docker-engine

# Set up yum Ali image
sudo yum-config-manager \
    --add-repo \
    http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

# Install, update yum warehouse first
sudo yum install -y yum-utils \
  device-mapper-persistent-data \
  lvm2

# Install. If prompted to accept the GPG key, select Yes.
sudo yum install docker-ce docker-ce-cli containerd.io

# start-up
sudo systemctl start docker

# Test available
sudo docker run hello-world

 

2.2 Ubuntu installation

2.2.1 one key installation

# Official script (using Alibaba image):
curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun

# One click installation of daocloud in China:
curl -sSL https://get.daocloud.io/docker | sh

2.2.2 manual installation

# If you have an older version, uninstall it first
sudo apt-get remove docker docker-engine docker.io containerd runc

# Update apt package index
sudo apt-get update

# Install apt dependency package
sudo apt-get install \
    apt-transport-https \
    ca-certificates \
    curl \
    gnupg-agent \
    software-properties-common

# Set up stable version warehouse
sudo add-apt-repository \
   "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
  $(lsb_release -cs) \
  stable"

# Update apt package index
sudo apt-get update

# Install the latest version of Docker
sudo apt-get install docker-ce docker-ce-cli containerd.io

# Test available
sudo docker run hello-world

2.3 Alibaba image acceleration

3 Docker common commands

3.1 Image related commands

3.1.1 search image

You can also search from the official warehouse website: https://hub.docker.com/

# Return value Description:
#	NAME: NAME of the image warehouse source
#	DESCRIPTION: DESCRIPTION of the image
#	OFFICIAL: OFFICIAL release of docker or not
#	stars: it's similar to the star in Github, which means "like" or "like".
#	AUTOMATED: auto build.
docker search ubuntu

3.1.2 pull image

# Download Image
docker pull ubuntu:13.10

# View image information
## Return value Description:
##	REPOSITORY: the warehouse source of the image
##	TAG: the label of the image
##	IMAGE ID: IMAGE ID
##	REATED: image creation time
##	SIZE: mirror SIZE
docker images

3.1.3 running image

In Docker, there are two ways to specify an image:

  • REPOSITORY:TAG
  • IMAGE ID: shorthand can be used when referring to IMAGE ID, that is, only the first two digits of IMAGE ID can be entered. If there are multiple mirror shorthand values that are consistent, an error will be reported when shorthand is used.
# -t: Terminal
# -i: Interactive operation
# -d: Background running, multiple parameters can be written together: - itd
# -p: Port mapping, 80:8080 represents port 8080 of the 80 port mapping container of the host.
#	  That is to say, port 80 of the access host is equivalent to port 8080 of the access container. This parameter can be reused for multi port mapping.
# --Name, test is the custom container name
# /bin/bash: the command is placed after the image name. Here we want to have an interactive Shell, so / bin/bash is used.
#			 If you use / bin/bash to report an error, use / bin/sh
docker run -t -i -d --name test openjdk:11 /bin/bash 

# The following commands have the same effect

# f5 here is the shorthand for the IMAGE ID of the image: f5de33dc9079
docker run -itd f5 /bin/bash 
docker run -tid f5de33dc9079 /bin/bash 

3.1.4 delete image

docker rmi openjdk

3.1.5 rename image

If the downloaded image name is too long, we can use the docker tag command to label the image again (it has the effect of rename, but not the real rename).
The docker tag command will not be described in detail in this article. You can visit it if you are interested

docker tag openjdk:11 myjdk:1


 

3.2 Container related commands

In Docker, we can specify a container in two ways:

  • NAME, container NAME, specified when starting the mirror.
  • IMAGE ID: shorthand can be used when referring to IMAGE ID, that is, only the first two digits of IMAGE ID can be entered. If there are multiple containers with the same shorthand value, an error will be reported when shorthand is used.

3.2.1 viewing containers

# By default, only the running
docker ps

# View all
docker ps -a

3.2.2 start / stop container

# start-up
docker start test

# restart
docker restart 242480ce4d72

# stop it
docker stop 24

3.2.3 delete container

docker rm test

3.2.4 entering the container

Entering the container is the same as entering a Linux system, just to make the image lighter, these Linux system tools will be very few, only the most basic commands, most of which do not even have vi commands, and need to be installed manually.
According to the selected basic system of the image, the installation tools are different:
· install Ubuntu system based on apt command.
· install via yum command based on RedHad system.
 
After entering the container, if you need to exit the container, enter exit to enter

# -t: Terminal
# -i: Interactive operation
# /bin/bash: the command is placed after the image name. Here we want to have an interactive Shell, so / bin/bash is used.
## If you use / bin/bash to report an error, use / bin/sh
docker exec -it myubuntu /bin/bash 

3.2.5 viewing container logs

Use docker logs to view the container logs.

# -f dynamic loading, i.e. new logs will be loaded in real time
# --tail 100 loads only the last 100 lines
# |grep * * * * searches for the specified character and outputs its line
# > temp.txt  "'"' > stands for output and '<' stands for input in Linux
#			 This means that the output value is stored in the current directory temp.txt In file
# All the above options can be selected on demand
docker logs -f --tail 100 myubuntu | grep The blogger is so handsome > temp.txt

3.2.6 viewing container information

If you need to check the configuration file of the container, especially when the container fails to start, you can find the path of the container in the host by using the docker inspect command to view the container information.
In the following figure, you can find the value in the red box by using the docker inspect myubuntu | grep Dir command: / var / lib / docker / overlay2 / eaafdf5cb63f8180ab5e26036d6e3ac2ab518ad8ca9a0d597dd106712878557, which is the path of the container. Some containers don't have a merged directory, so we can cd it to its parent directory.

# Output details of 'myybuntu' container
docker inspect myubuntu
# |grep Dir only outputs lines containing 'Dir' characters
docker inspect myubuntu | grep Dir

3.2.7 check the operation of the container

# View resource consumption of all started containers
docker stats

# View resource consumption of the specified container
docker stats myubuntu

# View the process information of the specified container
docker top myubuntu

# View process information for all containers
for i in  `docker ps |grep Up|awk '{print $1}'`;do echo \ &&docker top $i; done

 

4. Common application of docker installation

4.1 Docker installs Mysql

Official website warehouse: https://hub.docker.com/_/mysql?tab=tags
It needs to be mentioned that if there is a certain amount of data or pressure in the production environment, it is better not to use Docker to run Mysql. After all, there is a Docker in the middle, so the performance is certainly not installed directly on the server. It doesn't matter locally or in a test environment.

# When TAG is not specified, the latest version will be downloaded by default
docker pull mysql

# Assign label
docker pull mysql:8.0.20

# Search for available versions
docker search mysql

# Running a mirror
# -e user defined parameters, which are detailed in the official website above
# MYSQL_ROOT_PASSWORD=123456: set the password of the MySQL service root user.
docker run -itd --name mysql8 -p 3306:3306 -e MYSQL_ROOT_PASSWORD=123456 mysql

# Check whether it runs successfully
docker ps

4.2 Docker installation of Redis

Installation steps are similar to Mysql installation

# When TAG is not specified, the latest version will be downloaded by default
docker pull redis

# Assign label
docker pull redis:6.0.5

# Search for available versions
docker search redis

# Running a mirror
docker run -itd --name redis6 -p 6379:6379 redis

# Check whether it runs successfully
docker ps

5 Summary

This paper briefly introduces the basic use of Docker, and later has time to reorganize the knowledge points related to Dockerfile and Docker Compose.
Compared with the literal description of Docker architecture / advantages at the beginning of this article, I believe you will have a deeper understanding of Docker after actual operation.
If this article is helpful to you, remember to double-click meme~

Tags: Docker sudo MySQL Ubuntu

Posted on Mon, 15 Jun 2020 22:57:24 -0400 by ayampanggang