Docker from entry to pit (1)

Docker introduction

A simple introduction to docker can be understood as a container for application execution. However, docker itself and the virtual machine still have obvious differences. I have summarized it roughly, which can be summarized as follows:

Docker itself also has many advantages, about its advantages, can be summarized as the following:

Install docker

Starting in March 2017, docker has been divided into two branch versions on the original basis: Docker CE and Docker EE.

Docker CE is the community free version, and Docker EE is the enterprise version, which emphasizes security, but requires a fee.

First we need to remove the old docker version:

$ sudo yum remove docker \
                  docker-client \
                  docker-client-latest \
                  docker-common \
                  docker-latest \
                  docker-latest-logrotate \
                  docker-logrotate \
                  docker-selinux \
                  docker-engine-selinux \
                  docker-engine

Then we need to install some necessary system tools:

sudo yum install -y yum-utils device-mapper-persistent-data lvm2

Next we need to add software source information:

sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

Update yum cache:

sudo yum makecache fast

Verify the container

The above operations are some preparation operations. If there is no abnormality during the steps, you can install docker later.

sudo yum -y install docker-ce

After the installation is complete, start the docker background service steps:

sudo systemctl start docker

When our docker image is downloaded, there will be a helloworld image for testing by default:

Test run hello-world

[root@runoob ~]# docker run hello-world

Since there is no hello-world image locally, a hello-world image will be downloaded and run in the container.

About docker image and container introduction

The essence of a container is a process. Different containers have their own independent root file system, network configuration, and an isolated space. Once the container disappears, the data contained in it will also be lost. All storage information in the container should use data volumes or other forms. The life cycle of the data volume is independent of the container. The container dies, and the data volume will not die. Therefore, after the data volume is used, the data will not be lost after the container is deleted or rerun.

Docker image is a special file system. In addition to providing the programs, libraries, resources, configuration and other files required by the container runtime, it also contains some configuration parameters prepared for runtime (such as anonymous volumes, environment variables, users, etc.) ). The image does not contain any dynamic data, and its content will not be changed after it is built.

docker install nginx

After the initial installation of docker, we will start to enter some actual combat links.

First, we need to pull the related nginx image in docker:

At the beginning, the host did not start the docker container, and the docker service needs to be started

service docker start

Then pull the mirror down and start

docker run -d -p 80:80 --restart=always nginx:latest

Parameter explanation: -d start in the background -p port 80 of the host is mapped to port 80 of the container. Test the relevant page, if successful, the following figure:

 

Next we can look at the mirror tag

 

docker images

This command queries only the id of the image, but we cannot enter the container based on the id of the image. To enter the container, the id of the container is required:

So you can view the id of the container with this command:

docker ps

Then enter the container to modify the information
. There are many commands to enter the container, and it is recommended to use this type:

docker exec -it 999318c7d567 /bin/bash

Enter the container by selecting the corresponding tag-id. After entering the container, you will find that it seems to be a new linux operating system:

How to close the docker container?

It’s relatively simple. I won’t say much here, you can understand by yourself.

docker kill 4591552a4185

File copy

After understanding the basic docker container operation, let's try how to upload files to the docker container. This requires file exchange with the host.

First we need to get the full name of the container id:

[root@localhost html]# docker ps 
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                NAMES
0d4173ecf31f        nginx:latest        "nginx -g 'daemon of…"   9 minutes ago       Up 9 minutes        0.0.0.0:80->80/tcp   sleepy_gould

[root@localhost html]# docker inspect -f '{
   
   {.Id}}' 0d4173ecf31f
0d4173ecf31f894e234ae9eb3d9eddd1b9d10b7af5c46608679fe11918af9644

Then copy the file into the docker container:

[root@localhost html]# docker cp /opt/html/user_login.html 0d4173ecf31f894e234ae9eb3d9eddd1b9d10b7af5c46608679fe11918af9644:/root/user_login.html
[root@localhost html]#

The next operation is relatively simple, just check the mobile page:

root@0d4173ecf31f:/usr/share/nginx/html# ls
50x.html  index.html  user_login.html
root@0d4173ecf31f:/usr/share/nginx/html#

The next steps are basically normal operations, enter our nginx configuration:

root@0d4173ecf31f:/etc/nginx# vi nginx.conf 
bash: vi: command not found
root@0d4173ecf31f:/etc/nginx#

Found that there is no vi command, need to download and install

When using a docker container, sometimes vi is not installed inside, and the prompt saying: vi: command not found when you type the vi command, you need to install vi at this time, but when you type the apt-get install vi command, it prompts:

Reading package lists... Done  
Building dependency tree         
Reading state information... Done  
E: Unable to locate package vi

At this time, you need to type: apt-get update. The function of this command is to synchronize the indexes of the sources listed in /etc/apt/sources.list and /etc/apt/sources.list.d, so that you can get the latest Software package. After the update is complete, type the command: apt-get install vi command.

Now that the docker has and the nginx mirroring is turned on, you can modify the nginx configuration.

Install redis in docker

First of all, the old rules, pull mirror

[root@izwz9ic9ggky8kub9x1ptuz docker]# docker pull redis:4.0

After the mirror is pulled, the content is as follows:

[root@izwz9ic9ggky8kub9x1ptuz docker]# docker pull redis:4.0
4.0: Pulling from library/redis
b8f262c62ec6: Pull complete 
93789b5343a5: Pull complete 
49cdbb315637: Pull complete 
e244975d5eb1: Pull complete 
25dca704d939: Pull complete 
db11ecc9cf34: Pull complete 
Digest: sha256:615b872e3af387206b62ffe66c1052f1653ed31b3afcfaa292a9244c92f2007a
Status: Downloaded newer image for redis:4.0
docker.io/library/redis:4.0

ok, the basic image is available, then open the container directly

Here we have modified the port number of the host machine to 8879, which is 6379 in the mapping container.

[root@izwz9ic9ggky8kub9x1ptuz /]# 
docker run -p 8879:6379 
 -v $PWD/data:/data  
 -v $PWD/conf/redis.conf:/etc/redis/redis.conf  
 --privileged=true
 --name myredis 
 -d redis:4.0 redis-server /etc/redis/redis.conf

Parameter explanation

  • -p 8879:8879:  Map port 8879 of the container to port 6379 of the host

  • -v $PWD/data:/data:  Mount the data in the current directory in the host to the container's /data

  • redis-server —appendonly yes:  execute the redis-server startup command in the container and open the redis persistence configuration

After the startup, everything started to run normally.

Enter the container and start to check whether redis is normal:

It is recommended to make a redis configuration file on the host when starting redis and map it to the container, otherwise the corresponding redis configuration file cannot be found in the container, which is very painful.

Install MySQL in docker

The content of this part is relatively simple, just pull down the mirror image:

docker run --name 【容器名】 -e MYSQL_ROOT_PASSWORD=【mysql密码】 -d -i -p 3309:3306 mysql:5.7.24

Then wait for a while, finally our MySQL container is installed:


The memory resource consumption of the container.
Since I purchased a small server, the related configuration is as follows, so when assigning resources to each running container, we need to consider the resource occupied by each docker container.

At present, a mysql container, a redis container, and an nginx container have been built. If you add a java application, you need at least 500m of memory to support the java container visually.

By using the docker stats command to see the memory of the current server, because the amount of visits on weekdays is not high, you can allocate smaller memory resources to the current mysql and redis:

At this time, you can use the docker update command to manage the memory size allocation:

docker update -m 350m  --memory-swap -1  mysqlserver

--memory-swap -1 The parameter refers to not allowing the container to exchange memory with the host. If necessary, additional adjustments are required. After
setting the memory limit, the result is as follows:

Docker container startup failure analysis actual combat

When configuring a new docker container, for example, to configure certain attributes of the my.cnf file for mysql, you need to restart the container to take effect. But sometimes due to hand errors, the configuration may be abnormal and the container restart fails. What should I do at this time?

The simplest and rude way at this time is to rm the container first and then reinstall it, but this operation is too violent. In case there are other important data in the container, direct rm operation is not advisable.

In fact, docker is essentially installed in the host machine, so the corresponding configuration files can also find traces in the host machine. Here are some remedies for my pitfall:

docker inspect [容器名称]

Through this command, you can view the internal configuration property information of the container:

At this time, we can view a directory folder called MergedDir, enter the upper level of the folder,

 

 cd /var/lib/docker/overlay2/f4ab9eff7a18f20f977595af38f5eb273127e0de8845a42fe71fa9b8850a988b

Then we check the structure of the directory, we will find a folder called diff

With curiosity, step by step into the folder, you will find the mysql file that was previously configured in the docker container, manually modify the original configuration back, and the container will restart successfully.

 

 

 

 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/bj_chengrong/article/details/103233652