Before learning Docker, I was confused and always felt difficult. Later, I tried it and found that it was relatively simple. After using the command, I could probably understand it.
The introduction
Why Docker?
The emergence of Docker is to solve the problem of deployment environment, and often encountered in the usual development of those, in my computer can be used on your side can not use the situation.
The most important issue is the environment deployment of the cluster. MySQL, Redis, RabbitMQ, ZK, ES and so on can be very troublesome to deploy on one computer. And sometimes the environment goes wrong and you have to start all over again. This is more time-consuming and laborious. So Docker is online.
What is a Docker?
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.
What are the advantages of Docker?
Docker is an open platform for developing, delivering and running applications. Docker enables you to separate your applications from your infrastructure, allowing you to deliver software quickly. With Docker, you can manage your infrastructure the same way you manage your applications. By leveraging Docker’s approach to quickly deliver, test, and deploy code, you can greatly reduce the latency between writing code and running it in a production environment. Fast and consistent delivery of your applications 2 responsive deployment and scaling 3 Running more workloads on the same hardware 4 light
Docker thought
Similar to the nature of a container, each container is isolated, containerized. Next, containerization involves some virtualization.
Virtualization technology
Inside the kernel, lib, APP and other memory is very large. Containerization technique
The container runs directly on top of the kernel, which saves the server the most resources. Each container is isolated from the other. Each container has its own file system. Each other
Docker architecture composition
Images:An image is like a template from which container services can be created. A container service infinite clone deployed for use on other computers. Simplify deployment environment issues.Container:Docker uses containerization to run a single application or a group of applications independently, created through imagesRepository:Similar to a lot
Common Docker commands
Linux Installation (recommended)
curl -sSL https://get.daocloud.io/docker | sh
Copy the code
The docker version number
docker -v
Copy the code
Start Docker (from the system)
sudo systemctl start docker
sudo systemctl status docker
Copy the code
Docker detailed version
docker version
Copy the code
Docker details
docker info
Copy the code
Docker help information
docker --help
Copy the code
Image information
## Lists local images
docker images
## REPOSITORY: represents the REPOSITORY source of the image
## TAG: indicates the mirrored TAG
## IMAGE ID: indicates the IMAGE ID
## CREATED: time when a mirror is CREATED
## SIZE: mirror SIZE
## includes intermediate image layers
docker images -a
Only the mirror ID is displayed
docker images -q
## includes intermediate image layers
docker images -qa
# display the DIGEST column
docker images --digests
Display complete information about the image
docker images --no-trunc
Copy the code
Search the warehouse MySQL mirror
docker search mysql
## -- starts = 0 and starts = 0
docker search --filter=stars=600 mysql
## --no-trunc Display full DESCRIPTION of the image
docker search --no-trunc mysql
## --automated: Lists only images whose automated =OK
docker search --automated mysql
Copy the code
Image downloads (e.g. Redis)
Docker pull Redis :latest docker pull Redis :latest
docker pull redis
Download all Redis images from the repository
docker pull -a redis
## Download the private warehouse image
docker pull bitnami/redis
Copy the code
Mirror deletion (this will not be demonstrated)
Docker rmi redis:latest
docker rmi redis
## Force delete (for container processes running based on images)
docker rmi -f redis
## Delete multiple mirrors with Spaces between them
docker rmi -f redis tomcat nginx
## Delete all local mirrors
docker rmi -f $(docker images -q)
Copy the code
Image building
## (1) Write a dockerfile
cd /docker/dockerfile
vim mycentos
## (2) Build docker image
docker build -f /docker/dockerfile/mycentos -t mycentos:1.1
Copy the code
Container startup
If you use “docker ps -a”, you will find that the container has already exited. This is how Docker works: in order for a Docker container to run in the background, there must be a foreground process. Solution: Run the program you want to run as a console process.
## Create a container and start it with -i to run the container in interactive mode. -t reassigns a dummy input terminal to the container. --name Specifies a name for the container
docker run -i -t --name mycentos
## Start the container in daemon mode
docker run -d mycentos
Copy the code
Process of container
Docker top [OPTIONS] CONTAINER [ps OPTIONS]
## Lists the processes running in the Redis container
docker top redis
Check all processes running the container
for i in `docker ps |grep Up|awk '{print $1}'`;do echo \ &&docker top $i; done
Copy the code
Container log
## View redis container log, default parameter
docker logs redis
The redis container log is displayed with the -f command. -t Displays the timestamp. --tail lists only the latest N container logs;
docker logs -f -t --tail=20 redis
## Check the latest 10 logs of container Redis since May 21, 2019.
docker logs --since="2019-05-21" --tail=10 redis
Copy the code
Container entry and exit
## Use run to enter at create time
docker run -it centos /bin/bash
## Close the container and exit
exit
## Exit container only, not closeShortcut key: Ctrl + P + QSig -proxy=false Make sure ctrl-d or Ctrl-c does not close the container
docker attach --sig-proxy=false centos
The following command is used to open a new interactive mode terminal in centos: -t Assigns a dummy terminal
docker exec -i -t centos /bin/bash
Execute commands in the container in interactive mode, and the result is returned to the current terminal screen
docker exec -i -t centos ls -l /tmp
Execute commands in the container in separate mode, the program runs in the background, and the result will not be fed back to the current terminal
docker exec -d centos touch cache.txt
Copy the code
Check the container
## View the running container
docker ps
Check the ID of the running container
docker ps -q
## View containers that are running + historically run
docker ps -a
## Displays the total file size of the running container
docker ps -s
## displays recently created containers
docker ps -l
## Displays the three recently created containers
docker ps -n 3
## Do not truncate the output
docker ps --no-trunc
Get meta information about the image redis
docker inspect redis
Get the IP of the running container redis
docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' redis
Copy the code
Stop and delete containers
Stop a running container
docker stop redis
Kill a running container
docker kill redis
## Delete a stopped container
docker rm redis
Delete a running container
docker rm -f redis
## Delete multiple containers
docker rm -f $(docker ps -a -q)
docker ps -a -q | xargs docker rm
## -l Remove the network connection between containers named db
docker rm -l db
## -v Deletes the container and the data volumes mounted to the container
docker rm -v redis
Copy the code
Generate the mirror
Create a new image based on the current Redis container. Parameter: -a Submitted mirror author; -c use the Dockerfile command to create an image; -m: Description of submission; -p: Suspends the container at commit time
docker commit -a="DeepInThought" -m="my redis" [redisThe containerID] myredis:v1.1
Copy the code
Data copy between container and host
Copy the files in the RabbitMQ container to a local directory
docker cp rabbitmq:/[container_path] [local_path]
Copy the host file to the RabbitMQ container
docker cp [local_path] rabbitmq:/[container_path] /Copy the host files to the rabbitMQ container and rename the directory to [container_path].
docker cp [local_path] rabbitmq:/[container_path]
Copy the code
The underlying principle
Run Running process
How does Docker work?
Docker is a client-server structure of the system, Docker daemon running on the host. Access from client through Socket! DockerServer receives the docker-client directive and executes this command!
Why is Docker faster than VM?
- Docker has fewer layers of abstraction than virtual machines
- Docker uses the kernel of the host computer, and the VM needs to be a Guest OS.
So, when adding a container, Docker doesn’t need to reload an operating system kernel like a virtual machine, avoiding booting!