preface
When I first saw others using Docker, I was puzzled. Why and how to use Docker? Why is Docker configuration so difficult? Why is network access blocked? And other factors hinder the author from learning Docker? In fact, the author is also very stupid. There are many points where I don't think clearly. By the way, share it.
Class hour doubt
- Q1: why don't I put a set of services in a container (Java, mysql, nginx redis, etc.)
- A1: because it is necessary to maintain the network in the container, ports and other things, Docker is born to quickly build the environment, and Docker should also be a service and a container, which is easy to take care of
- Q2: can docker be put into production environment?
- A2: when the company does not have a special operation and maintenance team, it is not recommended to use the environment deployed by Docker as the production environment, because it is not only necessary to maintain the project and middleware, but also to solve these problems after problems occur in Docker or K8s, that is, to solve the problems of Docker. If you don't have a special operation and maintenance team, you'd better use some cloud services, such as RDS, SLB, etc. at least others will help you maintain your database and services
Basic command
Download Image
# Take redis as an example docker pull redis
Run mirror
docker run \ -d \ # Background operation --name redis6 \ # Custom name -p 6379:6379 \ # Port mapping redis # Image name docker run -d --name redis6 -p 6379:6379 redis redis-server --appendonly yes --requirepass "123456" # Full command
Enter container
The first one (not recommended. When you exit the container and use the exit command, the container will be stopped)
docker attach container id
Second
docker exec -it container id /bin/bash
Pause container
docker stop container id
Start container
docker start container id
Query container list
docker ps -a # View all containers docker ps # View containers in operation
The difference between run and start:
- run is to create a new container
- Start is to start the container that has been created
View container information
docker inspect container id
mount
Mount introduction
The files in the container are inside the container, which has nothing to do with your current computer. What if you delete the container? But I want to save the data, just like mysql. I just need to copy the whole container to another computer. It's too troublesome! Therefore, you need to map the container file to the current host file
Command tutorial
- Parameter -v host path: container path
# Take mysql as an example docker run -d --name mysql8 -p 3306:3306 -v /data/mysql8/config:/etc/mysql/conf.d -v /data/mysql8/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 mysql # The above commands can be referred to https://hub.docker.com/_/mysql There is a detailed introduction
Why do people know so many paths or parameters
- For each middleware or database container, it may need to have many configurations, such as password, path of persistent file, etc. How do we know what the path is
- You can enter the hub docker. Com find the container you need and look at the document
- Find it in the container (this method is a little stupid. That's how I found it at the beginning.)
network
How do containers communicate
Although the containers can communicate with each other, the ip address of each restart of the container is different, so the communication will be very complex
demonstration
# Pull a centos image first docker pull centos # Create a container docker run -d -it --name centos1 centos docker run -d -it --name centos2 centos docker inspect centos1_id
- Intercept some container information
[ { "NetworkSettings": { "Networks": { "bridge": { "IPAMConfig": null, "Links": null, "Aliases": null, "NetworkID": "9e7ed6d29ca3474de04409833e39b7c7965c7c63d3a1f509886a7a998e4825f8", "EndpointID": "41230bf523fac8fa4933989d98baaaa7655fba5c5dadd14e63839ffe868ed3f8", "Gateway": "172.17.0.1", "IPAddress": "172.17.0.4", "IPPrefixLen": 16, "IPv6Gateway": "", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "MacAddress": "02:42:ac:11:00:04", "DriverOpts": null } } } } ]
docker inspect centos2_id
[ { "NetworkSettings": { "Networks": { "bridge": { "IPAMConfig": null, "Links": null, "Aliases": null, "NetworkID": "9e7ed6d29ca3474de04409833e39b7c7965c7c63d3a1f509886a7a998e4825f8", "EndpointID": "8ae77d46887c795983ee7a8fb96951d05e236b4ca4b4caa5d5964f892e18a476", "Gateway": "172.17.0.1", "IPAddress": "172.17.0.5", "IPPrefixLen": 16, "IPv6Gateway": "", "GlobalIPv6Address": "", "GlobalIPv6PrefixLen": 0, "MacAddress": "02:42:ac:11:00:05", "DriverOpts": null } } } } ]
- The centos1 ip above is 172.17.0.4
- The centos2 ip above is 172.17.0.5
solve the problem
docker network create centos-network docker run -d -it --network centos-network --name centos3 centos docker run -d -it --network centos-network --name centos4 centos docker exec -it centos3_id /bin/bash ping centos4 # Therefore, when a network is created, containers can be added to the network, which is very convenient
- This can solve the problem of ip inconsistency after each container restart
Summary
In fact, it will be very troublesome when you first use the container
- I don't know the command of docker and the parameters required by the container
- Every time you go to the official website to find out what parameters or Baidu has, but compared with downloading files every time, it saves a lot of effort to go in and configure, and save the used commands once, which can be used in the future. There is no need to repeat to change a lot of things
- In fact, in some small companies, if there is no professional operation and maintenance, try not to use docker in the production environment, otherwise there will be a headache if there are problems, not only to maintain the project, but also to maintain docker
last
Recently, I compiled a complete set of "summary of Java core knowledge points". To be honest, as a java programmer, you should take a good look at this material whether you need an interview or not. Kwai won't lose my hand. Many of my fans got the Offer of Tencent's byte express.
Enter[ Java advanced road group ], find the administrator to get Oh -!