On the soul of Docker

Keywords: Docker CentOS vim github

1. Operation of docker common commands

docker pull image
 docker run creates a container based on a certain image
 -d. background operation
 --Name specifies the name of the container to create
 -p map the port of the container to the port of the host
 docker exec -it enters a container
 docker rmi delete image
 docker ps view running container s	
docker rm delete container 
docker ps -a view all container s

2,Image and Container

2.1 in depth discussion of Image


To put it bluntly, image is composed of layer s.

2.1.1 official image

github.com/docker-libr...

2.1.2 Dockerfile

Let's also create our own image image image. By the way, learn the common syntax in the Dockerfile file

MAINTAINER mouliu
LABEL name="dockerfile-demo" version="1.0" author="mouliu"
COPY cechealth-daq-worker-hospif-1.0-SNAPSHOT.jar dockerfile-image.jar
CMD ["java","-jar","dockerfile-image.jar"]

2.1.2.1 FROM

Specify the basic image, such as FROM ubuntu:14.04

FROM ubuntu:14.04

2.1.2.2 RUN

Execute some commands inside the image, such as installing software, configuring environment, etc. line breaking can be used

RUN groupadd -r mysql && useradd -r -g mysql mysql

2.1.2.3 ENV

Set the value of the variable, env MySQL ﹣ Ma job 5.7, which can be modified by docker run --e key=value, and can be used directly later

${MYSQL_MA JOR}
ENV MYSQL_MAJOR 5.7

2.1.2.4 LABEL

Set image label

LABEL email="15828626971@163.com"
LABEL name="dzzgml"

2.1.2.5 VOLUME

Specifies the directory to which the data is attached

VOLUME /var/lib/mysql

2.1.2.6 COPY

Copy the host's files to the image. If the directory does not exist, the required directory will be created automatically. Note that only copy, not extract and
decompression

COPY docker-entrypoint.sh /usr/local/bin/

2.1.2.7 ADD

COPY the host's files to the image, similar to COPY, except that ADD will extract and decompress the compressed files

ADD application.yml /etc/dzzgml/

2.1.2.8 WORKDIR

Specify the working directory of the image. Subsequent commands work based on this directory. If not, create

WORKDIR /usr/local
WORKDIR tomcat
RUN touch test.txt

The test.txt file will be created under / usr/local/tomcat

WORKDIR /root
ADD app.yml test/

Copying code will create an additional app.yml file under / root/test

2.1.2.9 CMD

The command that will be executed by default when the container is started. If there are multiple CMD commands, the last one will take effect

CMD ["mysqld"] or
CMD mysqld

2.1.2.10 ENTRYPOINT

Similar to the use of CMD

ENTRYPOINT ["docker-entrypoint.sh"]

When the different docker run from the CMD is executed, the CMD command will be overwritten, while ENTRYPOINT will not

2.1.2.11 EXPOSE

Specify the port to be exposed by the image. When starting the image, use - p to map the port to the host

EXPOSE 3306

2.1.3 Dockerfile actual combat Spring Boot project

(1)Create a Spring Boot project
(2)Write a controller @RestController
    public class DockerController {
        @GetMapping("/dockerfile")
        @ResponseBody
        String dockerfile() {
            return "hello docker" ;
        }
}
(3)mvn clean package Form a jar package
//Find "dockerfile-demo-0.0.1-SNAPSHOT.jar" under target (4) create a new directory "first-Dockerfile" in docker environment (5) upload "dockerfile-demo-0.0.1-SNAPSHOT.jar" to this directory, and create a Dockerfile in this directory
(6)Establish Dockerfile Document, content FROM openjdk:8
    MAINTAINER itcrazy2016
    LABEL name="dockerfile-demo" version="1.0" author="itcrazy2016"
    COPY dockerfile-demo-0.0.1-SNAPSHOT.jar dockerfile-image.jar
    CMD ["java","-jar","dockerfile-image.jar"]
(7)Be based on Dockerfile Constructing mirrors
docker build -t test-docker-image .
(8)Be based on image Establish container
docker run -d --name user01 -p 6666:8080 test-docker-image
(9)View startup log docker logs user01 (10)Access on host curl localhost:6666/dockerfile
hello docker
(11)You can also start another
docker run -d --name user02 -p 8081:8080 test-docker-image

2.1.4 image warehouse

2.1.4.1 docker hub

hub.docker.com
(

1)stay docker Login on machine docker login
(2)Enter user name and password
(3)docker push itcrazy2018/test-docker-image
[Note that the image name and docker id Consistent, otherwise push Unsuccessful]
(4)to image Rename and delete the original
docker tag test-docker-image itcrazy2018/test-docker-image docker rmi -f test-docker-image
(5)Push again, refresh hub.docker.com Background, discovery successful
(6)Others download and run
docker pull itcrazy2018/test-docker-image
docker run -d --name user01 -p 6661:8080 itcrazy2018/test-docker-image

2.1.4.2 Alibaba cloud docker hub

Alibaba cloud docker warehouse
cr.console.aliyun.com/cn-hangzhou...
Reference manual
cr.console.aliyun.com/repository/...

(1)Log in to alicloud docker Warehouse
sudo docker login --username=itcrazy2016@163.com registry.cn- hangzhou.aliyuncs.com
(2)Input password (3)Create a namespace, such as itcrazy2016
(4)to image hit tag
sudo docker tag [ImageId] registry.cn-hangzhou.aliyuncs.com/itcrazy2016/test- docker-image:v1.0
(5)Push image to docker Alibaba cloud warehouse
sudo docker push registry.cn-hangzhou.aliyuncs.com/itcrazy2016/test-docker- image:v1.0
(6)Others download and run
docker pull registry.cn-hangzhou.aliyuncs.com/itcrazy2016/test-docker-
image:v1.0
    docker run -d --name user01 -p 6661:8080 registry.cn-
hangzhou.aliyuncs.com/itcrazy2016/test-docker-image:v1.0

2.1.4.3 build your own Docker Harbor

(1)Visit github Upper harbor project
https://github.com/goharbor/harbor
(2)Download version, such as 1.7.1 https://github.com/goharbor/harbor/releases
(3)Find one to install docker-compose[This course will explain later],Upload and unzip tar -zxvf xxx.tar.gz
(4)Enter into harbor Catalog modification harbor.cfg Documents, mainly ip Change the address to the current machine's ip The address can also be seen Harbor The default is Harbor12345
(5)install harbor,It will take some time sh install.sh
(6)Browser access, such as 39.100.39.63,Just enter the user name and password

2.2 in depth discussion of Container

Since container is run by image, can it be understood that there is a relationship between container and image?

Understanding: in fact, it can be understood that the container is only based on the layer after the image, that is, a container can be created through docker run image.

2.2.1 container to image

Since the container is based on image, do you think it is possible to use a container to reverse the image?
It must be possible, for example, to run a container through docker run. At this time, some modifications are made to the container, and then a new image is generated. At this time, the origin of the image is not only through Dockerfile.

(1)Pull one. centos image docker pull centos
(2)according to centos Image creates a container
docker run -d -it --name my-centos centos
(3)Get into my-centos Container
docker exec -it my-centos bash
(4)input vim command
bash: vim: command not found
(5)What we have to do is to container Make changes, i.e. install vim Command, and then generate a new centos
(6)stay centos Of container Installation in China vim yum install -y vim
(7)Exit the container and generate a new centos,Name is"vim-centos-image" docker commit my-centos vim-centos-image
(8)View the list of mirrors based on"vim-centos-image"Create a new container
docker run -d -it --name my-vim-centos vim-centos-image
(9)Enter into my-vim-centos In container, check vim Does the command exist docker exec -it my-vim-centos bash
vim

You can regenerate an image based on a container by using the docker commit command, but it is not recommended to do so in the general way of getting the image, otherwise you will not know how the image came from

2.2.2 container resource limit

If the resource of the container is not limited, it will use the resource of the physical machine unrestricted, which is obviously not appropriate.
View resource status: docker stats

2.2.2.1 memory limit

--memory Memory limit
 If you do not set -- memory swap, its size is the same as memory
docker run -d --memory 100M --name tomcat1 tomca

t

2.2.2.2 CPU limit

 --cpu-shares weight
docker run -d --cpu-shares 10 --name tomcat2 tomcat

2.2.2.3 graphical resource monitoring

github.com/weaveworks/...
sudo curl -L git.io/scope -o /usr/local/bin/scope
sudo chmod a+x /usr/local/bin/scope
scope launch 39.100.39.63
 Stop scope scope stop
 Monitor both machines at the same time, and execute the following commands respectively in the two machines
scope launch ip1 ip2

2.2.3 common operations of container

(1)Create container from image
docker run -d --name -p 9090:8080 my-tomcat tomcat
(2)View running container docker ps
(3)View all container[Include exited] docker ps -a
(4)delete container
docker rm containerid docker rm -f $(docker ps -a)
(5)Enter into a container in
docker exec -it container bash
(6)according to container generate image docker
(7)View a certain container Log docker logs container
(8)View container resource usage docker stats
(9)View container details
docker inspect container
(10)Stop it/Starting container
docker stop/start container

2.3 underlying technical support

Container is a lightweight virtualization technology, which does not need to simulate hardware to create virtual machines.
Docker is a custom container format based on the technology of Namespace, CGroups and UnionFileSystem of Linux Kernel, which provides a set of virtual running environment.

Namespace: used for isolation, such as pid [process], net [network], mnt [mount point], etc
 Cggroups: controller groups are used to limit resources, such as memory and CPU
 Union file systems: used for image and container layering


At the early stage, I also collected a lot of information like many little friends, and later I found a lot of duplicate! The following are all self-organized! Now that BAT's dream comes true, I will contribute the information to those in need!

By the way, please pay attention. Haha ~ you can get it for free by private mail [Java] after you pay attention to me!

Published 4 original articles, won praise 0, visited 250
Private letter follow

Posted by rklapwijk on Fri, 17 Jan 2020 01:01:40 -0800