Hello everyone, I am xiao CAI, a desire to do CAI Not CAI xiao CAI in the Internet industry. Soft but just, soft praise, white piao just! Ghost ~ remember to give me a three – even oh!
This article mainly introduces the basic use of Docker
Refer to it if necessary
If it is helpful, do not forget the Sunday
Wechat public number has been opened, xiao CAI Liang, did not pay attention to the students remember to pay attention to oh!
Docker profile
Docker is an open source application container engine, which is based on the Go language and complies with the Apache2.0 protocol. Docker allows developers to package their applications and dependencies into a lightweight, portable container that can then be distributed to any popular Linux machine, as well as virtualization. Containers are completely sandboxed, have no interfaces with each other (like iPhone apps), and most importantly, have very low performance overhead.
Docker application scenarios
-
Automated packaging and distribution of Web applications
-
Automated testing and continuous integration, release
-
Deploy and adjust databases or other backend applications in a service environment
-
Build from scratch or extend existing OpenShift or Cloud Foundry platforms to build your own PaaS environment.
The advantages of the Docker
- Deliver applications quickly and consistently
- Responsive deployment and scaling
- Running more workloads on the same hardware
The three cores of Docker
- Image
Is the basis for creating containers, similar to snapshots of virtual machines
- The container
A running instance created from an image that can be started, stopped, and deleted. Each container is isolated from each other and cannot be seen from each other, ensuring the safety of the platform
- warehouse
A centralized place to save images
The image above shows the Docker LOGO, which also explains the concept of Docker.
Docker borrows the concept of shipping containers, which are used to ship software and applications.
Docker architecture
- Client
Docker client. Docker is the main way for many Docker users to interact with Docker. Docker clients can communicate with multiple daemons.
- Docker Daemon
Docker daemon. Responsible for listening for Docker API requests and managing Docker objects such as images, containers, networks, and volumes. Daemons can also communicate with other daemons to manage Docker services.
- Registry
The Docker registry stores Docker images. Docker Hub is a public registry that anyone can use.
VM vs container
features | VM | The container |
---|---|---|
Isolation level | Operating system level | Process level |
Isolation strategy | Hypervisor | CGroups |
System resources | 5 ~ 15% | 0 ~ 5% |
The startup time | Minutes of class | Second level |
Image storage | GB~TB | KB~MB |
The cluster size | Hundreds of | Tens of thousands of |
High availability policy | Backup, Dr, and migration | Elasticity, load, dynamic |
- Containers: An application-layer abstraction for packaging code and dependent resources together. Multiple containers can run on the same machine, sharing the operating system kernel, but each running as a separate process in user space. Compared to virtual machines, containers take up less space (container images are usually only a few tens of megabytes in size) and can be started instantaneously.
- Virtual machine: Is a physical hardware layer abstraction for turning one server into multiple servers. Hypervisors allow multiple VMS to run on one machine. Each VM contains a complete set of operating systems, one or more applications, the necessary binaries, and library resources, and therefore takes up a lot of space.
Docker to fit
1. Mirroring operations
1. List the mirror
Statement: Docker images
-
REPOSITORY: represents the REPOSITORY source of the image
-
TAG: indicates the TAG of the mirror
-
IMAGE ID: indicates the ID of an IMAGE
-
CREATED: indicates the time when a mirror is CREATED
-
SIZE: indicates the mirror SIZE
2. Search for the mirror
Docker search ${image_name}
- NAME: indicates the NAME of the mirror repository source
- DESCRIPTION: indicates the DESCRIPTION of the mirror
- Starts: user comments reflecting the popularity of an image
- OFFICIAL: Is it OFFICIAL with Docker
- auto commit: Indicates that the image is automatically builtDocker HubCreated by the automated build process
3. Pull the mirror
Docker pull ${image_name}:${image_version}
4. Delete the mirror
-
Delete a single:
docker rmi ${image_name} (or ${id})
-
Delete multiple:
docker rmi ${image_name}/${id} ${image_name}/${id} ...
-
Delete all:
docker rmi
`docker images -q`
5. View the metadata of the mirror
docker inspect ${image_name}
docker inspect -f ='{{.NetworkSettings.IPAddress}}' ${image_name}
- -f: Can be replaced by -format
Container operation
1. Create a container
docker run [option] --name=${name} image command [args...]
Option options:
-i
: Interactive container-t
: tty, terminal-d
: Runs in the background and prints the containerid
Example:
docker run -i -t -d --name=centOS1 centos /bin/bash
=
docker run -itd --name=centOS1 centos /bin/bash
Note: The name of the created container must be unique
2. Enter the container
Method 1:
docker attach ${name}/${id}
- Example:
docker attach centOS1
Note: When you exit a container using exit inside the container, the container stops
Method 2:
docker exec -it ${name}/${id} /bin/bash
- Example:
Docker exec it centOS1 /bin/bash Docker exec it centOS1 /bin/bash
3. Check the container
- View the running container
docker ps
- View containers that have been run historically
docker ps -a
- View the container that was last run
docker ps -l
4. Stop the container
docker stop ${name}/${id}
5. Start the container
docker start ${name}/${id}
6. Delete the container
- Delete a container
docker rm ${name}/${id}
- Delete multiple containers
docker rm ${name1}/${id1} ${name2}/${id2} ...
- Delete multiple containers
docker rm
` docker ps -a -q`
7. View the container metadata
docker inspect ${name}
docker inspect -f ='{{.NetworkSettings.IPAddress}}' ${name}
- -f: Can be replaced by -format
8. View container logs
docker logs ${name}/${id}
9. Copy files
Docker cp File or directory to copy Container name: container directory
Example: docker cp 1.txt c2:/root
10. Mount a directory
Directory mount maps the host’s directory to the efforts in the container, so that when we change the contents of the host’s mount directory, the directories in the corresponding mount directory in the container also change
Docker run ‐ ID ‐‐name=centOS1 ‐v /opt/:/usr/local centos
If permissions are insufficient, we should use:
Docker run ‐id ‐privileged=true ‐name=c4 ‐v /opt/:/usr/local/myhtml centos
Three, mirror production
We can not only pull images from Docker Hub to create containers, but also manually customize Docker system images. At present, there are two ways to build images:
- use
docker commit
The command - use
docker build
Cooperate withDockerfile
file
1. docker commit
1)To make the mirror
To make an image with Docker Commit, we need a running container:
- Step 1
We need to first pull a centOS as our base image: Docker pull centOS
- Step 2
Docker run -it –name=centOS1 centos:latest
- Step 3
Install environment in container:
- tomcat
Upload the Tomcat installation package:
Docker cp apache ‐ tomcat ‐ 8.5.54. Tar. Gz centOS1: / root /
Install tomcat:
Tar ‐ ZXVF apache‐tomcat‐8.5.54.tar.gz ‐C /usr/local/
Edit the /bin/setclsspath. Sh file in tomcat and add the following information:
Export JAVA_HOME = / usr/local/jdk1.8.0 _161 export JRE_HOME = / usr/local/jdk1.8.0 _161 / jreCopy the code
- JDK
Upload JDK installation package:
Docker cp JDK 8 u161 ‐ ‐ Linux ‐ x64. Tar. Gz centOS1: / root /
The JDK installation:
Tar ‐ ZXVF JDK ‐8u161‐ Linux ‐x64.tar.gz ‐C /usr/local/
Edit the **/etc/profile** file and add the following:
JAVA_HOME = / usr/local/jdk1.8.0 _161 export PATH = $JAVA_HOME/bin: $PATHCopy the code
- Step 4
Submit the container we are running as a new image
docker commit centOS1 cbucImage
2)Port mapping
- Step 1
Docker run ‐ ITd ‐name= T1 ‐ P 8888:8080 cbucImage /bin/bash
- Step 2
‐tomcat‐7.0.47/bin/startup.sh: docker exec t1 /usr/local/apache‐tomcat‐7.0.47/bin/startup.sh
This way we can access the page through http://ip:port
3)Image packaging
Package image:
Docker Save ‐o/MNT/myimage.tar cbucImage
Using images on other servers:
docker load -i myImage.tar
4)The container packing
Packing container:
Docker export ‐ O/MNT /mycentos.tar centOS1
Import container:
docker import mycentos.tar centOS2:latest
2. docker builder
Dockerfile uses basic DSL grammar-based instructions to build a Docker image, and then uses the Docker Builder command to build a new image based on the instructions in the Dockerfile
1)DSL grammar
keywords | explain |
---|---|
FROM | Base image |
MAINTAINER | Maintainer information |
RUN | Install the software |
ADD | The COPY file is automatically decompressed |
WORKEDIR | CD Switch to the working directory |
VOLUME | Directory mount |
EXPOSE | Internal service port |
CMD | Execute the commands in Dockerfile |
ENV | Setting environment Variables |
Resolution:
- 1. FROM
Specify the base image. Must be specified and must precede other directives in Dockerfile. Subsequent directives depend on the image specified by the directive. The underlying image specified by the FROM directive can be either in an official remote repository or in a local repository. The FROM command tells Docker which (distribution) image we are building based on. You can use multiple FROM directives when creating multiple images in the same Dockerfile.
Format:
FROM or :
- 2. MAINTAINER
Specifies the image creator information. Use to write information about the creator of an image to the image. When we execute the docker inspect command on the image, there are corresponding fields in the output to record the information.
Format:
MAINTAINER <name>
- 3. RUN
Use the installation software. You can run any command supported by the underlying image. If ubuntu is selected for the base image, the software management section can only use ubuntu commands.
Format:
RUN <command>
- 4. CMD
Set the operations to be performed when the Container is started. This operation can be performed by executing custom scripts or system commands. This directive can exist only once in the file, and if there are more than one, only the last one is executed.
Format:
CMD command param1 param2
- 5. ENTRYPOINT
Set the operation to be performed when the Container is started. You can set the operation multiple times, but only the last one takes effect.
Format: ENTRYPOINT command param1 Param2
Scenario 1:
When used alone, if you also use CMD and CMD is a complete executable command, then CMD and ENTRYPOINT overwrite each other and only the last CMD or ENTRYPOINT is valid.
Example: Only ENTRYPOINT will execute at this time
CMD ls -l ENTRYPOINT ls ‐ LCopy the code
Scenario 2:
Used in conjunction with CMD directives to specify the default parameters of ENTRYPOINT, CMD directives are not a complete executable command, only the parameters part. The ENTRYPOINT directive can only use JSON to specify execution commands, not parameters.
Ex. :
CMD ENTRYPOINT [‐ "l"] ["/usr/bin/ls "]Copy the code
- 6. USER
Set the user of container. The default user is root
Format:
#Specify the user to run memcached‐u: ENTRYPOINT ["memcached"] ENTRYPOINT ["memcached"]Copy the code
- 7. EXPOSE
Specifies the port that the container needs to map to the host machine. When you need to access a container, you can use the host machine’s IP address and mapped port instead of the container’s IP address. To do this there are two steps: first set the container port to map using EXPOSE in the Dockerfile, then specify the ‐ P option plus the EXPOSE port when running the container, so that the EXPOSE port number is randomly mapped to a port number on the host machine. You can also specify which port you want to map to the host machine, making sure that the port number on the host machine is not in use. The EXPOSE directive allows you to set up multiple port numbers at once, allowing you to use the ‐ P option multiple times when running the corresponding container.
Format:
EXPOSE <port> [<port>...]
#Mapping a Port
EXPOSE port1
#Corresponding command used by the run containerDocker Run ‐ P port1 image#Mapping Multiple Ports
EXPOSE port1 port2 port3
#Corresponding command used by the run containerDocker run ‐ P port1 ‐ P port2 ‐ P port3 image#You can also specify a port number that you want to map to the host machineDocker run ‐ P host_port1:port1 ‐ P host_port2:port2 ‐ P host_port3:port3 imageCopy the code
- 8. ENV
Used to set environment variables. After container is set, subsequent RUN commands can be used. After container is started, docker inspect can be used to check the environment variable, and docker RUN ‐env key=value can be set or modified.
Format:
ENV <key> <value>
#If you have JAVA installed and need to set JAVA_HOME, you can write this in your Dockerfile:
ENV JAVA_HOME /path/java/jdk
Copy the code
- 9. ADD
Copy the file from SRC to the dest path of the container. It is used to add files from the host machine to the image.
Format:
#< SRC > is the path relative to the source directory being built. This can be a file or directory path, or a remote file URL.
is the absolute path in the Container
ADD <src> <dest>
Copy the code
- 10. VOLUMN
Specify a mount point. Enables a directory in a container to persistently store data. The directory can be used by the container itself or shared with other containers.
Format:
VOLUME ["<mountpoint>"]
#Example: VOLUME ["/tmp/data"]
Copy the code
/ TMP /data; / TMP /data; / TMP /data; / TMP /data
- 11. WORKDIR
This command is equivalent to the CD command. It takes effect for RUN, CMD, and ENTRYPOINT.
Format:
WORKDIR /path/to/workdir
#Execute vim a.txt under /p1, /p2
WORKDIR /p1 WORKDIR p2 RUN vim a.txt
Copy the code
- 12. ONBUILD
Execute in a child mirror
Format:
#The command specified is not executed when the image is built, but in its child mirrorONBUILD <Dockerfile keyword >Copy the code
2)Create a mirror image
After editing the Dockerfile file, enter the command in the directory where the Dockerfile resides:
Docker build cbucImage:v1.0.0 ‐rm=true.Copy the code
Note:
-
‐ T indicates the user name, repository name, and tag selected to specify the image generation
-
‐‐rm=true specifies that temporary containers generated in the process of image generation are deleted.
-
Don’t leave out the. Symbol at the end of the build command above, indicating that the image will be built using the Dockerfile in the current directory
3. Run the image
Once we have created the image, we can run it with the following command:
Docker run ‐ ITD ‐name CentoS1 ‐p 8888:80 cbucImage /bin/bashCopy the code
Use the following command to enter the container:
docker exec -it centos1 /bin/bash
Copy the code
[END]
The above is the general knowledge of Docker, after watching to try to package their own project into Docker image practice! The road is long, small dishes with you to seek ~
Today you work harder, tomorrow you will be able to say less words!
I am xiao CAI, a man who studies with you. 💋
Wechat public number has been opened, xiao CAI Liang, did not pay attention to the students remember to pay attention to oh!