Personal blog address :https://alexaccele.github.io/
Docker The use and command summary of
Help related
- docker –help You can see docker All the orders of ( Notice that there are two parameters in front of the word spelling ‘-’ Number )
- docker info see docker Information
- docker version see docker Version number
The most useful help command
docker XXX –help Check the help manual for specific commands (XXX Refers to all kinds of orders )
Mirror related
docker images View all mirrors
Parameter description :
- -a : Lists all mirrors locally ( Contains an intermediate image layer )
- -q : Image only ID.
- –digests : Displays summary information for the image
- –no-trunc : Displays complete mirror information
docker search XXX Search by keyword docker Mirror image
Parameter description :
- -s : List the images with the number of collections no less than the specified value .
- –limit int : Maximum number of displayed results ( Default 25 individual )
- –no-trunc : Show full image description
- –automated : List only automated build Type of mirror ;
docker pull XXX Download mirroring
have access to docker pull XXX:Tag Specifically point out to download some Tag Mirror image
docker rmi ID Delete ID The mirror image refers to
Parameter description :
-f: Mandatory deletion
Example :
- Delete a single image :docker rmi -f ID
- Delete multiple images :docker rmi -f Mirror name 1:TAG Mirror name 2:TAG
- Delete all images :docker rmi -f $(docker images -qa)
Container related
docker run [OPTIONS] IMAGE [COMMAND] [ARG…] Create and start the container
Parameter description :
-
–name=“ New container name ”: Specify a name for the container
-
-d: Background running container , And return to the container ID, That is, start the daemon container
-
-i: Run container in interactive mode , Usually with -t Use at the same time
-
-t: Reassign a pseudo input terminal to the container , Usually with -i Use at the same time
-
-P: Random port mapping
-
-p: Specify port mapping , There are four formats
-
ip:hostPort:containerPort
-
ip::containerPort
-
hostPort:containerPort
-
containerPort
Start interactive container : Mirror image centos:latest Start a container in interactive mode , Execute in container /bin/bash command .docker run -it centos /bin/bash
Background running container :docker run -d centos
docker ps View all running containers
Parameter description :
- -a : Lists all currently running containers + Historically run
- -l : Displays the most recently created container .
- -n: According to recent n Containers created .
- -q : silent mode , Only the container number is displayed .
- –no-trunc : Untruncated output .
Exit the container
- exit: Container stop exit
- Ctrl+P+Q: The container does not stop exiting
Start stop and restart of the container
- docker start ID/ Container name Start the container
- docker restart ID/ Container name Restart container
- docker stop ID/ Container name Stop container
- docker kill ID/ Container name Forced stop container
stop and kill The difference between :stop It’s like shutting down a computer , Will carry out a series of program closing operations
kill It’s similar to a direct power failure , Forced stop container , Do not perform a series of operations required for program shutdown
Delete container
- docker rm -f ID Delete the specified ID The container of
- docker rm -f $(docker ps -q) Batch delete all running containers
How to re-enter the container
docker exec -it ID BashShell Enter the running container and interact at the command line
for example :docker exec -it ID /bin/bash
docker attach ID Reenter the container
difference :
attach Go directly to the terminal of the container startup command , No new process will be started
exec Is to open a new terminal in a container , And you can start a new process
Copy files from the container to the host
docker cp ID: Container path Target host path
for example :docker cp ID:/usr /home
docker commit Submit a copy of the specified container as a new image
docker commit -m=“ Description information submitted ” -a=“ author ” Containers ID The target mirror name to create :[ Tag name ]
Application scenarios : When we are from hub After the downloaded image runs successfully locally , The content has been modified , For example, configuration parameters, etc , When we’re done with our changes , have access to docker commit command , Create a new image for the modified container , So when we run this image , The content in it is what we have modified .
Container related details command
docker logs -f -t –tail ID View container log
Parameter description :
- -t It’s adding a timestamp
- -f Follow the latest log print
- –tail Numbers Show how many last
docker top ID View the processes in the container
docker inspect ID Look inside the container for details (JSON The form )
Container data volume
Container data volumes are designed to persist data in containers , Data volumes are completely container independent lifecycles , It won’t disappear because the container is deleted .
The data volume exists in the host , Independent of the container , And the lifecycle of the container is separate , Data volumes exist in the host’s file system , Data volumes can be directories or files , Containers can use data volumes to share data with hosts , It realizes the data sharing and exchange between containers .
Characteristics of data volume :
- The container is initialized when it starts , If the image used by the container contains data , This data will also be copied to the data volume .
- The modification of data volume by container is timely .
- The change of data volume will not affect the update of mirror image . Data volumes are independent of the federated file system , The image is based on the federated file system . There is no interaction between the mirror and the data volume .
- A data volume is a directory in the host , Isolated from the container lifecycle .
Command addition
Use -v You can mount a local directory into a container as a data volume .
docker run -it -v / Host absolute path directory :/ In-container directory Mirror name
Use -v The running container will create a file directory in the host and container , If you use docker inspect Command, you can see , The two directories have been successfully connected 、 Mount and have read and write permissions at the same time , At this time, the container and the corresponding directory in the host are data shared , And after the container stops , Data is still shared , And the data remains consistent after the container is restarted .
You can add permissions to data volumes docker run -it -v / Host absolute path directory :/ In-container directory :ro Mirror name
(ro Express Read-Only) At this time, the data volume can only be viewed in the container , Do not modify . In the host, you can modify .
DockerFile add to
Can be found in Dockerfile Use in VOLUME Directive to add one or more data volumes to the image
That is to say DockerFile Add the following command to the :
VOLUME["/dataVolumeContainer","/dataVolumeContainer2","/dataVolumeContainer3"]
for example :
# volume test
FROM centos
VOLUME ["/dataVolumeContainer1","/dataVolumeContainer2"]
CMD echo "finished,--------success1"
CMD /bin/bash
Writing well dockerfile After the document , You can use docker build Command to create a mirror image , Execute the order as follows :
docker build -f /mydocker/dockerfile -t xxx/centos .(‘.’ Indicates the current directory )
-f: Appoint dockerfile The location of the file
-t: Generated image namespace
At this point, a named xxx/centos Mirror image , After running, it can pass through docker inspect Command to view the host directory corresponding to the data volume of the container .
Be careful :Docker Mount the host directory Docker The visit appears cannot open directory .: Permission denied
terms of settlement : Add one more… After mounting the directory –privileged=true Parameters can be
Data volume container
Named container mounts the data volume , Other containers mount this ( Parent container ) Implement data sharing , A container for mounting data volumes , Call it a data volume container .
The data volume container is mounted with a local directory , Other containers connect to this container to share data ( Copy of data address ).
First, you need a parent container as the data volume container
docker run -it --name dc01 xxx/centos
Reuse --volumes-from
The command causes the child container to inherit from the parent container
docker run -it --name dc02 --volumes-from dc01 xxx/centos
Now the parent container dc01 And sub containers dc02 The data in is shared , And all changes can be synchronized .
Even if you delete any parent or child container , Data is still shared .
Conclusion : Transfer of configuration information between containers , The life cycle of a data volume continues until no container USES it
DockerFile
Dockerfile It’s used to build Docker The build file for the image , Is a script composed of a series of commands and parameters .
The basic content
- Each reserved word instruction must be uppercase and followed by at least one parameter
- Command from top to bottom , Sequential execution
- # Notation
- Each instruction creates a new image layer , And submit the image
Perform the general process
- docker Run a container from the underlying image
- Execute an instruction and make changes to the container
- Execution is similar to docker commit Submit a new image layer
- docker Then run a new container based on the image just submitted
- perform dockerfile The next instruction in until all instructions are executed
Reserved words
- FROM: base image , Which image is the current new image based on
- MAINTAINER: Image maintainer’s name and email address
- RUN: Commands that need to be run when the container is built
- EXPOSE: The port exposed by the current container
- WORKDIR: Specifies that after the container is created , Terminal default login in the working directory , A foothold
- ENV: It is used to set environment variables during the process of building image
- ADD: Copy the files in the host directory into the image and ADD The command will be processed automatically URL And decompress tar Compressed package
- COPY: similar ADD, Copy files and directories into the image . From the build context Directory < The source path > The file of / The directory is copied to the image of the new layer < The target path > Location
- VOLUME: Container data volume , For data preservation and persistence
- CMD: Specify a command to run when the container starts ;Dockerfile There can be more than one CMD Instructions , But only the last one works ,CMD Will be docker run After the parameter replacement
- ENTRYPOINT: Specify a command to run when the container starts ;ENTRYPOINT The purpose and CMD equally , Start the program and parameters in the specified container
- ONBUILD: When building an inherited Dockerfile Run the command , The parent image is inherited by the child onbuild Be triggered
dockerfile Case study
Custom mirror mycentos
FROM centos
MAINTAINER zzyy<[email protected]>
ENV MYPATH /usr/local
WORKDIR $MYPATH
RUN yum -y install vim
RUN yum -y install net-tools
EXPOSE 80
CMD echo $MYPATH
CMD echo "success--------------ok"
CMD /bin/bash
Custom mirror Tomcat9
FROM centos
MAINTAINER zzyy<[email protected]>
# Put the current context of the host c.txt Copy to container /usr/local/ Under the path
COPY c.txt /usr/local/cincontainer.txt
# hold java And tomcat Add to container
ADD jdk-8u171-linux-x64.tar.gz /usr/local/ADD apache-tomcat-9.0.8.tar.gz /usr/local/
# install vim Editor
RUN yum -y install vim
# Set up the WORKDIR route , Log in to the landing point
ENV MYPATH /usr/local
WORKDIR $MYPATH
# To configure java And tomcat environment variable
ENV JAVA_HOME /usr/local/jdk1.8.0_171
ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
ENV CATALINA_HOME /usr/local/apache-tomcat-9.0.8
ENV CATALINA_BASE /usr/local/apache-tomcat-9.0.8
ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:$CATALINA_HOME/bin
# Port on which the container listens at runtime
EXPOSE 8080
# Run on startup tomcat
#ENTRYPOINT ["/usr/local/apache-tomcat-9.0.8/bin/startup.sh" ]
#CMD ["/usr/local/apache-tomcat-9.0.8/bin/catalina.sh","run"]
CMD /usr/local/apache-tomcat-9.0.8/bin/startup.sh && tail -F /usr/local/apache-tomcat-9.0.8/bin/logs/catalina.out
summary
From the perspective of application software ,Dockerfile、Docker The mirror with Docker Containers represent three different phases of software
- Dockerfile It’s the raw material of software
- Docker A mirror is a software deliverable
- Docker The container can be thought of as the running state of the software .
Dockerfile Facing the development of ,Docker Mirroring becomes the delivery standard ,Docker Containers involve deployment and operations , All three are indispensable , To act as Docker The cornerstone of the system .
- Dockerfile, I need to define a Dockerfile,Dockerfile Defines everything the process needs .Dockerfile It involves executing code or files 、 environment variable 、 Dependency package 、 Runtime environment 、 Dynamic link library 、 Distribution of the operating system 、 Service process and kernel process ( When application processes need to deal with system services and kernel processes , You need to think about how to design namespace Permission control ) wait ;
- Docker Mirror image , In use Dockerfile After defining a file ,docker build There will be a Docker Mirror image , When running Docker Mirror image , Will really start providing services ;
- Docker Containers , Containers are direct service providers .