Notes: Docker, Dockerfile Foundation

Keywords: Docker CentOS MySQL Tomcat

Article directory

Basic concepts of Docker

Image: image is a read-only template. Images can be used to create Docker containers. A single image can create multiple containers

Container: the container is to create a running instance that loves you with the image. It can be started, started, stopped and deleted. Each container is isolated from each other

The relationship between images and containers is similar to that between objects and classes

Docker Object-oriented
image class
container container

Warehouse: the warehouse is a place for centralized storage of image files

There is a difference between a Repository and a Registry. There are many warehouses on the warehouse registration server, and each warehouse contains multiple images, each image has a different tag. The warehouse is divided into public library and private library. The largest public library is Docker Hub. Domestic Alibaba cloud, Netease cloud, etc

Docker installation

#1. Check the kernel version, which must be 3.10 or above
uname -r

#2. Install docker
yum install docker

#3. Enter y to confirm installation

#4. Start docker
[root@localhost ~]# systemctl start docker

#View docker version
[root@localhost ~]# docker -v
Docker version 1.12.6, build 3e8e77d/1.12.6

#5. Set the startup docker
[root@localhost ~]# systemctl enable docker
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.

#6. Stop docker
systemctl stop docker

Frequently used commands

#View version
docker version
#View docker information
dockerinfo
#Help is similar to man XXX in linux
docker --help

docker images

#View images in the local warehouse#
docker images 
#-a: List all local images - q: display only the image ID - digests: display the re needed information of the image - no TRUNC: display the complete image information

docker search

#Search image from Docker Hub warehouse
 docker search image name
 #-For example, docker search -s 30 tomcat searches for tomcat images with likes greater than 30
 #--No TRUNC displays the complete image description. For example, docker search - s 30 -- no TRUNC tomcat searches tomcat images with likes greater than 30 and displays the detailed description information
 #--Antimated only displays images of the type antimated build

docker pull

#Download Image
 docker pull image name: [tag]
#If the tag tag is omitted, it defaults to latest

docker rmi

#delete mirror
docker rmi Mirror name or id
#-F force to delete a mirror docker rmi -f tomcat:latest delete tomcat image

#Delete multiple mirrors at a time
docker rmi -f Mirror name or id Mirror name or id  

#Delete all mirrors
docker rmi -f $(docker images -qa)

docker run

#New and start container
docker run [options] image [command][tag]
#options parameter description:
#--Name = container new name specifies a name for the container
#-d run the container in the background. And return the container id, that is, return the guard container
*#-i runs the container in interactive mode, usually with - t
*#-t reassigns a pseudo input terminal to the container, usually with - i
#-p port mapping

docker ps

#List currently running containers
docker ps [options]
#-a list all currently running containers and historically running
#-l show recently created containers
#-N displays the last n created containers
#-q silent mode, only container number is displayed
#--No TRUNC does not truncate output

Two ways to exit a container

#Container stop and exit
exit

#Container does not stop exiting
ctrl+P+Q

docker start

#Start container
 docker start container name or id

docker restart

#Restart container
 docker restart container name or id

docker stop

#Stop container
 docker stop container name or id

docker kill

#Force stop container
 docker kill container name or id

docker rm

#Delete container
docker rm Container name or id #Delete closed containers
docker rm -f Container name or id #Force deletion of containers, both running and deleted
docker rm -f $(docker ps -a -q) #Delete all containers
docker rm 

docker logs

#View container id
docker logs -f -t --tail container id
#-t: Time stamp
#-f: Latest log printing
#-tail number: display the last number

docker top

#View the processes running in the container
 docker top container id

docker inspect

#View details in container
 Docker inspection container id

Container entry

#Open a new terminal in the container and start a new process
docker exec -it container id bashShell

#Execute the command in the container (centos container) in linux and return the result to linux
docker exec -it container id ls -l /tmp

#Enter the terminal of the container start command directly, and no new process will be started
docker attach container id

docker cp

#Copy files from container to host
 docker cp container id: path inside container destination host path

Container operation example

Software image (QQ installer) ---- run image ----- > generate a container (running software, running QQ)

Example:

#1. Search image
[root@localhost ~]# docker search tomcat
#2. Pull image
[root@localhost ~]# docker pull tomcat
#3. Start container from mirror
docker run --name mytomcat -d tomcat:latest
#4. View running containers
docker ps  
#5. Stopping a running container
docker stop  Container id
#6. View all containers
docker ps -a
#7. Start container
docker start container id
#8. Delete a container
 docker rm container id
#9. Start a tomcat with port mapping
[root@localhost ~]# docker run -d -p 8888:8080 tomcat
-d: Background operation
-p: Map the host's port to one of the container's ports    Host port:Port inside the container

#10. The firewall of linux is simply closed for demonstration
service firewalld status  #View firewall status
service firewalld stop   #Turn off firewall
#11. View the container's log
docker logs container-name/container-id

//For more commands, see
https://docs.docker.com/engine/reference/commandline/docker/
//You can refer to the documentation of each image

Docker mirror image

Image is a kind of lightweight and executable independent software package, = = it is used to package the software running environment and the software developed based on the running environment, = = it contains all the contents needed to run a certain software, including code, runtime, library, environment variables and configuration files.

The bottom layer of docker is essentially UnioFS (Federated file system).

UnioFS (Federated file system):

UnioFS (joint file system) is a hierarchical, lightweight and high-performance file system, which supports the file system modification as a layer by layer of submission, and can mount different directories to the same virtual file system. The Union file system is the foundation of Docker image. The image can be inherited by layering. The basic image (without the parent image) can make a variety of specific application images.

Features: multiple file systems are loaded at the same time, but from the outside, only one file system can be seen. Joint loading will stack the file systems of each layer, so that the final file system will contain all the underlying files and directories.

The docker image is read-only. When the container is started, a new writable layer is loaded on the top of the image. This layer is usually called the container layer, and the one under the container layer is called the image layer

docker commit

#Commit the container to the replica, making it a new mirror
 docker commit -m = "description of submitted" - a = "author" container id target image name to create: [tag name]

Docker container data volume

Command mode mount data volume

docker run -it -v /Host Directory:/Container directory [--privileged=true] Mirror name or id
docker run -it -v /myDataVolume:/dataVolumeContainer centos
#[-- privileged=true] can be omitted. If it is not writable, add [-- privileged=true]
#The folder / myDataVolume will be generated under the host, and the folder / dataVolumeContainer will be generated in the centos container

docker run -it -v /Host Directory:/Container directory:ro Mirror name or id
docker run -it -v /myDataVolume:/dataVolumeContainer: ro centos 
#After adding ro, the data volume in the container is only readable

docker file adding data volume

Only one data volume can be added by command, and multiple data volumes can be added at one time by using docker file

Case study:

1. Create a new mydocker folder in the root directory

2. Create a file and write a doccker file

#volume test
FROM centos
VOLUME ["/dataVolumeContainer1","/dataVolumeContainer2"]
CMD echo "finished,---success1"
CMD /bin/bash

3. docker build generates a new image file

#Docker build - F / location of dockerfile - t filename
docker build -f /mydocker/Dockerfile -t  zzr/centos .
#After successful execution, docker images can view the image named zzr/centos

4. Start a new container

docker run -it zzr/centos /bin/bash
#After entering the container, you can see that there are two data volume files, dataVolumeContainer1 and dataVolumeContainer2

5. Local data volume file

#The new container id of docker inspect ion shows that the local data volume is in the "/ var/lib/docker/volumes /" directory
{
	"Type": "volume",
	"Name": "8bd18596447998239495f79bc1aa8a2e50f165c491c64432f9dcf30962a38d19",
"Source":"/var/lib/docker/volumes/8bd18596447998239495f79bc1aa8a2e50f165c491c64432f9dcf30962a38d19/_data",
	"Destination": "/dataVolumeContainer1",
	"Driver": "local",
	"Mode": "",
	"RW": true,
	"Propagation": ""
},
{
	"Type": "volume",
	"Name": "c5ff5670a17d6b13a381859a375270741322eb80f014555ee0e497d711b32471",
	"Source":"/var/lib/docker/volumes/c5ff5670a17d6b13a381859a375270741322eb80f014555ee0e497d711b32471/_data",
	"Destination": "/dataVolumeContainer2",
	"Driver": "local",
	"Mode": "",
	"RW": true,
	"Propagation": ""
}

Transfer shares between containers (– volumes from)

The named container mounts the data volume, and other containers realize data sharing by mounting this (parent container). The container in which the data volume is mounted is called the data volume container.

#First generate a centos container dc01
docker run -it --name dc01 zzr/centos

#Then generate another container dc02, "inherit dc01"
docker run -it --name dc02 --volumes-from dc01 zzr/centos
#No matter how to inherit, delete, as long as there is a container, its data is shared.

The transmission of configuration information between containers. The life cycle of data volume lasts until there is no container

Dockerfile

Dockerfile is a build file used to build Docker image. It is a script composed of a series of commands and parameters.

**Dockerfile: * * dockerfile defines everything a process needs. The contents of dockerfile include execution code or file, environment variable, dependency package, runtime environment, distribution of DLL operating system, service process and kernel process, etc.

**Docker image: * * after a file is defined in the dockerfile, a docker image will be generated during docker build. When the docker image is running, services will actually be provided.

**Docker container: * * the container provides services directly.

Construction steps:

1. Write Dockerfile file
2,docker build
3,docker run

Content basics of Dockerfile:

1. Each reserved word instruction must be an uppercase letter followed by at least one parameter
 2. The instructions are executed from top to bottom
 3. A comment
 4. Each instruction creates a new image layer and commits the image

The general process of Dockerfile execution:

1. docker runs a container from the underlying image
 2. Execute an instruction and make changes to the document
 3. Perform an operation similar to docker commit to submit a new image layer
 4. docker runs a new container based on the new image just submitted
 5. Execute one command in the dockerfile until all instructions are executed

Dockerfile architecture (reserved word instruction)

FROM: basic image. The current image is based on that image

MAINTAINER: name and email address of image MAINTAINER

RUN: the command to RUN when building the container

EXPOSE: the port number of the current container exposed to the outside world

WORKDIR: Specifies the working directory that the terminal will log in by default after creating the container. It is a foothold

ENV: used to set environment variables during image building

ADD: copy the files under the host directory into the image, and the ADD command will automatically process the url and decompress the tar package

COPY: similar to ADD, COPY files to image

VOLUME: container data VOLUME for data preservation and persistence

CMD: Specifies the command to be executed when a container starts. (there can be multiple CMD instructions in the dockerfile, but only the last one takes effect. CMD will be replaced by parameters after docker run)

ENTRYPOINT: Specifies the command to be executed when a container is started. (the purpose of ENTRYPOINT is the same as that of CMD, but the CMD will be replaced by the last one, and ENTRYPOINT is added for execution.)

On build: runs the command when building an inherited Dockerfile. After the parent image is inherited by the child image, the ONBUILD of the parent image is triggered

Case: modify centos image, change its login directory, and add Toolkit

1. Write a dockerfile file

FROM docker.io/centos #Specify the base image as centos
MAINTAINER zzr<zzr946@126.com> #Author and author email
ENV MYPATH /usr/local #environment variable
WORKDIR $MYPATH #The foothold after entering the container refers to the environment variable defined above
RUN yum -y install vim #vim tools after installation
RUN yum -y install net-tools #install
EXPOSE 80 #Exposed port
CMD echo $MYPATH
CMD echo "uccess------OK"
CMD /bin/bash #Enter the container after transporting that container

2. docker build generates a new image

#Docker build - F / location of dockerfile - t filename: tag
docker build -f /Docker/Dockerfile2 -t mycentos:1.0 .

3. docker run runs a new image

docker run -it mycentos:1.0 /bin/bash

After entering the container, the destination directory is usr/local

You can use the vim editor

Example of Docker installing mysql

1. Search image

docker search mysql

2. Pull image

docker pull docker.io/mysql

3. Run the image to get the mysql container

docker run -p 12345:3306 --name mysql01 
-v /zzruse/mysql/conf:/etc/mysql/conf.d 
-v /zzruse/mysql/logs:/logs -v zzeuse/mysql/data:/var/lib/mysql #Mount data volume 
-e MYSQL_ROOT_PASSWORD=123456 #Specify root password 
-d docker.io/mysql #Background operation

Push the local image to alicloud

$sudo docker login --username = [user name] registry.cn-hangzhou.aliyuncs.com

$sudo docker tag [ImageId] registry.cn-hangzhou.aliyuncs.com / [namespace] / [warehouse]: [image version No.]

$sudo docker push registry.cn-hangzhou.aliyuncs.com / [namespace] / [warehouse]: [image version No.]
Published 9 original articles, won praise 5, and visited 767
Private letter follow

Posted by kanenas.net on Wed, 19 Feb 2020 02:27:08 -0800