Docker
1. A Docker Image is a read-only template. Images can be used to create Docker containers, and one image can create many containers
A container is an object, and a mirror is a class
Student s1 = new Student() Student s2=new Student() Student s3=new Student()
Student is a template, a mirror image.
The warehouse is where the image is stored
Official summary:
Dokcer uses a Container to run one or a group of applications independently. Containers are running instances created with images.
It can be started, started, stopped, and deleted. Each container is an isolated, secure platform.
Think of the container as a simplified version of the Linux environment.
Why Docker
1. Lighter weight: Container-based virtualization, which only contains the runtime environment required for service running. The CentOS/Ubuntu basic image is only 170M. Hosts can deploy 100-1000 containers
2. More efficient: No OPERATING system virtualization overhead
3. Be more agile and flexible
The container is a simplified version of the Linux virtual machine environment, stripped of some hardware.
You can’t create a container without an image, that’s the basic premise
1. Create and start a container:
docker run [options] image[command][arg...]
Copy the code
Some have one minus sign, some have two minus signs
– name= “New container name” Specifies a name for the container
-d: Runs the container in the background and returns the container Id, that is, starts the daemon container
-I: Runs the container in interactive mode, usually in conjunction with -t
-t: reassigns a pseudo-input terminal to the container. It is usually used together with -i.
-p: indicates random port mapping
-p: specifies the port mapping. The value can be in the following formats
ip:hostPort:containerPort
ip::containerPort
hostPort:containerPort
containerPort
Docker runit Imageid interactively starts the Dockers image to generate container instances. Docker PS lists all running containers to see which containers are loadedCopy the code
docker ps [options]
Options Description Common:
-a: Lists all currently running containers + historically run containers
-l: displays the newly created container
-n: Displays the n containers created recently
Docker Ps-N 3 Last 3
-q: displays only container ids in silent mode
– no-trunc: does not truncate the output
Exit the container in two ways
1. Exit The container stops exiting
2. CTRL +P+Q The container exits without stopping
Enter the running container and interact with it on the command line
Docker execit container id ls -l/TMP can be viewed from the outside, while attach can be attached to the inside
Docker exec -it Container ID bashShell
Re-enter the Docker Attach container ID
Attach directly enters the terminal of the container start command and does not start a new process
Exec opens a new terminal in the container and can start a new process
Start the container
Docker start Container instance ID or name
docker restart
Stop container Docker stop force stop docker kill
Delete stopped containers
Docker RM Container Id deleted closed
Docker rm f Container ID Forcibly deleted
Docker RMI delete image
Delete multiple containers at once
docker rm -f $(docker ps -a -q)
docker ps -a -q|xargs docker rm
Start a container in background mode
Start the daemon container
docker run -d centos
Problem: Then docker ps-a to check, will find that the container has quit, it is important to clarify a point: Docker container background operation, there must be a foreground process. Commands run by the container that are not always suspended (such as running top,tail) will exit automatically.
The dockers mechanism problem, such as your Web container, let’s take nginx as an example. Normally, we configure to start the service just by starting the responding service. For example, service nginx start. However, if nginx runs in daemon mode, there will be no applications running in the Docker foreground. Such a container will commit suicide immediately after it starts in daemon mode because it feels it has nothing more to do. Run the program as a console process
Viewing container Logs
Docker logs -f -t –tail Container ID
-t is to add the timestamp -f follows the latest log print –tail number shows the last number
Docker logs -f-t –tail 3
Docker Top Container ID of the process running in the container
Docker inspect Container ID
Copy files from container to host docker cp Container ID: path Path
Image: An image is a lightweight, executable, standalone package used to package a software runtime environment and software developed based on the runtime environment. It contains everything needed to run a piece of software, including code, runtime, libraries, environment variables, and configuration files
Docker run it -p 8888:8080 tomcat Starts Tomcat
-p indicates the exposed port
P Host port number: port number of the Docker container
-p is random assignment. I: interaction. T: terminal
Docker data volume synchronization Docker run-it -v/Host absolute path directory :/ The image name of the directory in the container is unlimited
Docker data volume synchronization Docker run-it -v/Host absolute path directory :/ Container directory :ro Image name Not limited Yes Protection restrictions
The DockerFile architecture (reserved word directives) must be all uppercase
FORM Base image, which image the current new image is based on
MAINTAINER New name and email address of the image MAINTAINER
RUN Command to RUN when the container is built
EXPOSE The port that the current container object exposes to the public
WORKDIR Specifies the working directory, a landing point, that the terminal logs in to by default after creating the container
ENV is used to set environment variables during image building
ENV MY_PATH /usr/mytest This environment variable can be used in any subsequent RUN directive, just as it was specified before the command; You can also use these environment variables directly in other directives, such as WORKDIR $MY_PATHCopy the code
ADD copies files in the host directory to the image, and the ADD command automatically processes the URL and decompresses the tar package
COPY Is similar to ADD. Copies files and directories to an image. Copy files/directories from the < source path > directory in the build context directory to the < destination path > location within the image in a new layer
VOLUME A VOLUME used for data storage and persistence
CMD specifies the command to run when a container is started. There can be multiple CMD commands in the Dockerfile, but only the last one takes effect, and CMD is replaced by the argument after the docker run
ENTRYPOINT specifies the command to run when a container is started. The purpose of ENTRYPOINT, like CMD, is to specify the container launcher and parameters
ONBUILD When the command is run to build an inherited Dockerfile, the ONBUILD of the parent image is triggered when the quilt inherits the parent image
Base Images (Scratch) 99% of the images in Dokcer Hub are built by installing and configuring the required software in the Base image
FROM centos
MAINTAINER HYF
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
Copy the code
The FROM centos MAINTAINER hyf # hosting c.t xt COPY of the current context to the container/usr/local/directory COPY c.t xt/usr/local/cincontainer. TXT # Java with tomcat Usr /local/ ADD apache-tomcat-9.0.8.tar.gz /usr/local/ # install vim editor RUN yum -y Install vim # install vim # install vim # Configure the Java and Tomcat environment variables 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 # 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. OutCopy the code
docker run -d -p 9080:8080 --name myt11 -v / zzyyuse/mydockerfile/tomcat9 / test: / usr/local/apache tomcat - 9.0.8 / webapps/test - v / zzyyuse/mydockerfile/tomcat9 tomcat9logs / : / usr/local/apache tomcat - 9.0.8 / logs - ring = true zzyytomcat9Copy the code