Docker basic use commands

preface

When I first saw others using Docker, I had many questions. Why do I use Docker? How do I use Docker? Why is Docker configuration so difficult? Why is the network inaccessible? And other factors hinder the author from learning Docker? In fact, the author is also very stupid, there are many points of unclear thinking. And by the way, share.

Hours wondering

  • Q1: Why don’t I put a set of services in a container (Java Mysql Nginx Redis etc.)
  • A1: Because it needs to maintain the network in the container, maintain ports and other things, Docker is born for the rapid establishment of the environment, and Docker had better serve a container, so that it is easy to manage
  • Q2: Can Docker be put into production environment?
  • A2: If the company does not have a dedicated operation and maintenance team, it is not recommended to use the Docker deployment environment as the production environment, because it not only needs to maintain the project and middleware, but also needs to solve the problems of Docker or K8s, that is, the problems of Docker should be solved. If you do not have a dedicated operation and maintenance team, it is best to use a cloud service, such as RDS, SLB, etc., at least others will help you maintain your database and services
  • Q3:

Basic commands

Download mirror

#Take Redis for example
docker pull redis
Copy the code

Run the mirror

Docker run -d name redis6 -p 6379:6379 docker run -d --name redis6-p 6379:6379 redis redis-server --appendonly yes --requirepass "123456Copy the code

Into the container

The first (not recommended, the container will be stopped when exiting the container using the exit command)

Docker Attach Container IDCopy the code

The second,

Docker exec -it Container ID /bin/bashCopy the code

Suspension of the container

Docker stop Container IDCopy the code

Start the container

Docker start Container IDCopy the code

Querying a list of containers

Docker ps -a # check all containers docker ps # check all containersCopy the code

The difference between run and start:

  • Run creates a new container
  • Start starts the created container

Viewing Container Information

Docker inspect Container IDCopy the code

mount

Mount the introduction

The files in the container are inside the container and have 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 on another computer, too much trouble! Therefore, the container file needs to be mapped to the current host file

Command tutorial

  • Parameter -v Host path: indicates the container path
#Take mysql as an exampledocker 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#You can refer to https://hub.docker.com/_/mysql for details about the above commands
Copy the code

Why do you know so many paths or parameters

  • Each middleware or database container may require many configurations, such as passwords, persistence file paths, and so on. So how do we know what the path is
  1. You can go to hub.docker.com and find the container you need and look at the documentation
  2. Into the container (this method is a bit stupid. That’s how I found it in the first place.)

network

How do containers communicate with each other

Containers can communicate with each other, but the IP address of each container restart is different from the last one, so communication is complicated

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
Copy the code
  • Grab 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}}}}]Copy the code
docker inspect centos2_id
Copy the code
[{"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}}}}]Copy the code
  • The centos1 IP address is 172.17.0.4
  • The IP address of centos2 is 172.17.0.5

To 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
Copy the code
  • In this way, the problem of inconsistent IP addresses after each container restart is resolved

summary

In fact, it can be quite troublesome when you first start using containers

  1. Docker commands and container parameters are not known
  2. Every time to go to the official website to find what parameters or Baidu

However, compared with downloading files every time, it saves a lot of effort to configure, and the configuration of a saved command can be used in the future, there is no need to repeat to change a lot of things 3. In fact, in some small companies, if there is no professional operation and maintenance, try not to use Docker in the production environment, or it will be a headache if there is a real problem, not only to maintain the project, but also to maintain the Docker