concept
Docker Images (Images)
Docker images are templates used to create Docker containers
Docker Container
A container is one or a group of applications that run independently and is called a container after an image is started
Docker Client
Clients use Docker from the command line or other tools
Docker Host
A physical or virtual machine used to execute the Docker daemon and container
Docker Repository (Registry)
Docker repository is used to store images, which can be understood as a code-controlled code repository. Docker Hub provides a huge set of images for use
Using the process, the machine installs the Docker application, and then goes to the repository to download the image. After the image is downloaded, the image is started and the container is generated
Common operations
Mirror operation:
operation | The command | instructions |
---|---|---|
retrieve | Docker search keyword, such as docker search redis | Go to docker Hub to retrieve the image details (but directly visit docker Hub website to query, so this command is not commonly used) |
pull | Docker pull image name :tag | : Tag Is optional. Tag indicates the software version. The default value is Latest |
The list of | docker images | View all local mirrors |
delete | docker rmi image-id | Example Delete a local mirror |
Container operation:
operation | The command | instructions |
---|---|---|
run | Docker run –name Specifies the container name. -d Specifies the image name, for example:docker run -name myredis -d redis |
–name: user-defined container name; -d: background running. Image-name: specifies an image template |
The list of | Docker PS (View containers in action) | add-a You can view all containers |
stop | Docker stop Container name/container ID | Stops the currently running container |
Start the | Docker start Container name/container ID | Start the container |
delete | Docker RM Container ID | Deleting a specified container |
Port mapping | 3306-3306 – p, such as:docker run -d -p 3306:3306 --name mysql mysql |
-p Host port (mapped to) internal container port |
Container log | Docker logs Container name/container ID |
docker run [OPTIONS]
The OPTIONS:
-a stdin
: Specifies the standard input and output types. The options are STDIN, STDOUT, or STDERR.-d
: Runs the container in the background and returns the container ID.-i
: Runs the container in interactive mode, usually with-t
Simultaneous use;-t
: Reassigns a pseudo-input terminal to the container, usually with-i
Simultaneous use;-P
: random port mapping. Ports in a container are randomly mapped to ports on the host-p
: Specifies the port mapping. The format is: Host port: container port--name="nginx-lb"
: Specifies a name for the container.--dns 8.8.8.8
: Specifies the DNS server used by the container. The default DNS server is the same as the host server.--dns-search example.com
: Specifies the container DNS search domain name, which is the same as the host by default.-h "mars"
: Specifies the hostname of the container.-e username="ritchie"
: Sets environment variables.--env-file=[]
: reads environment variables from the specified file;--cpuset="0-2" or --cpuset="0,1,2"
: binds the container to a specified CPU to run.-m
: Sets the maximum memory used by the container.--net="bridge"
Specifies the network connection type, container support bridge/host/none/container: four types;--link=[]
: Adds a link to another container--expose=[]
: Open a port or group of ports;--volume
.-v
: Binds a volume--net=host
Port mapping is not required when starting with host-p
)--restart=always
: Starts with docker--privileged=true
: The container root user has the rights of the root user on the host
Build Docker environment from 0
Docker installation
- Check the centos version Docker requires a centos kernel version later than 3.10
uname -r
Command to see - (Optional) If the kernel is smaller than 3.10, use
yum update
upgrade - Install the docker:
yum install docker
- Start the docker:
systemctl start docker
- Check:
docker -v
Checking the Version number - Docker is set to boot automatically after startup:
systemctl enable docker
- Stop the docker:
systemctl stop docker
Docker installation image
Search for Docker images
Take MySql as an example:
Docker hub search docker image:
docker search mysql
Copy the code
The installation image
Docker pull Image name: version number
Docker pull mysql: 5.7.31Copy the code
Start the MySql
Docker run -d --net=host --restart=always --privileged=true --name mysql -e MYSQL_ROOT_PASSWORD=123456 mysql:5.7.31 --lower_case_table_names=1Copy the code
Parameter Description:
- -d: Background startup
- –net=host: does not need to do port mapping
- –restart=always: start with docker
- — Privileged =true: The container root user has the privileges of the host root user
- -e MYSQL_ROOT_PASSWORD=root: Sets the password of user root
- –lower_case_table_names=1: Sets the table name and parameter names to ignore case
Start the Redis
Ordinary start
docker run -d --net=host --restart=always --name redis redis --requirepass "123456" --tcp-keepalive 10
Copy the code
Parameter Description:
- -d: Background startup
- –net=host: does not need to do port mapping
- –restart=always: start with docker
- –requirepass “123456” : specifies the login password
- — TCP-Keepalive 10: resolves the reIDS delay error
Start from the configuration file
Configuration file download: download from the official website
The configuration file needs to be modified:
bind 127.0.0.1 # comment out this section, this restricts redis to local access only
protected-mode No # Default yes, enable protected mode, restrict local access
daemonize No # Default no, change to yes means to start in daemon mode, can run in background, except kill process, change to yes will cause configuration file mode to fail to start Redis
databases 16 # Number of databases (optional), I changed this just to see if it works.
dir / # Enter the local redis database folder (optional).
appendonly Yes #redis persistence (optional)
Copy the code
Start command:
docker run -d --net=host --restart=always --name redis redis --requirepass "123456" -v /redis/conf/redis.conf:/etc/redis/redis.conf -v /redis/data:/data --appendonly yes
Copy the code
Parameter Description:
-
– v/redis/conf/redis. Conf: / etc/redis/redis conf: mount persistence configuration
-
/redis/conf/redis
- /etc/redis/redis.conf: path to the redis.conf file in the container, which is automatically mapped to the redis.conf file of the previous host when the container is started. This way you don’t have to go inside the container to modify the redis.conf file
-
-v /redis/data:/data: mounts persistent files
-
/redis/data: is the location of persistent files in the host machine
-
/data: is the location of the persistent file in the container
-
–appendonly yes: Enable persistence
Start the Nginx
The configuration file
Create a mount directory on the host
mkdir -p /nginx/{conf,html,logs}
#-p is short for --parents which creates the entire directory tree up to a given directory.
#{conf, HTML,logs} three parallel folders
Copy the code
Copy of the containernginx.conf
和 default.conf
To the host
Docker ps check the container ID
docker cp ef:/etc/nginx/nginx.conf /nginx/conf
dokcer cp ef:/etc/nginx/conf.d/default.conf /nginx/conf/conf.d
Copy the code
Parameter Description:
- Cp: indicates replication
- Ef: is the ID of our nginx container
- /etc/nginx/nginx.conf: is the internal nginx.conf path of the container
Modify the nginx.conf file
Include /etc/nginx/conf.d/*.conf;
Example:
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
Check to see if the property exists and if the path is correct
include /etc/nginx/conf.d/*.conf;
}
Copy the code
Modify the default.conf file
upstream xxxserver{
#ip_hash;
server 192.168.1.101:8182 backup;
server 192.168.1.102:8182;
}
server {
listen 80;
server_name 192.168.1.101;
charset utf-8;
location / {
alias /home/ec2-user/hibate/ROOT/;
}
location /service/ {
proxy_pass http://xxxserver/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 100m;
}
location /admin {
alias /home/ec2-user/hibate/admin/;
try_files $uri $uri/ /admin/index.html;
}
location /merchant {
alias /home/ec2-user/hibate/merchant/;
try_files $uri $uri/ /merchant/index.html;
}
location /source {
alias /home/ec2-user/hibate/source/;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root/usr/share/nginx/html; }}Copy the code
Start the command
docker run -d --name mynginx --net=host --rm --privileged=true -v /nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /nginx/conf/conf.d:/etc/nginx/conf.d -v /nginx/logs:/var/log/nginx -v /home/ec2-user/hibate/ROOT/:/home/ec2-user/hibate/ROOT/ -v /home/ec2-user/hibate/admin/:/home/ec2-user/hibate/admin/ -v /home/ec2-user/hibate/merchant/:/home/ec2-user/hibate/merchant/ -v /home/ec2-user/hibate/source/:/home/ec2-user/hibate/source/ -v /home/ec2-user/hibate/web/:/home/ec2-user/hibate/web/ nginx
Copy the code
Parameter Description:
- -d: background running
- – name: name
- –net=host: The container can directly access all network information on the host
- –restart=always: restarts with the system
- –rm: deletes containers automatically after stop. This container is mutually exclusive with –restart-always. Only one container can exist
- — Privileged =true: The container root user has the privileges of the host root user
- -v: mounts files (folders). Format: Local path: container path Each path involved in the default.conf file needs to be mapped to -v
Start the RabbitMQ
docker run -d --name rabbitmq --net=host rabbitmq
#Get docker idDocker ps docker exec -it Container ID/container name sh#Start the plugin
#Only browser 15672 can access username and password are guest
rabbitmq-plugins enable rabbitmq_management
Copy the code