天天看點

Docker常用知識點總結(持續更新)Docker常用知識點總結(持續更新)

Docker常用知識點總結(持續更新)

目錄

  • MySQL相關
  • Nginx相關

MySQL相關

  1. 建立MySQL容器,并使用Navigator連接配接
localhost:~ # docker run --name mysql -e MYSQL_ROOT_PASSWORD=changeme -d -p 3306:3306 mysql:latest
101ef9ff09cdb9c0312d4fb980256065072d795a48f7598fbb30a3f1b871aca3
localhost:~ # docker ps
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                               NAMES
101ef9ff09cd        mysql:latest        "docker-entrypoint.s…"   2 seconds ago       Up 1 second         0.0.0.0:3306->3306/tcp, 33060/tcp   mysql
localhost:~ # docker exec -it 101ef9ff09cd /bin/bash
[email protected]:/# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.0.24 MySQL Community Server - GPL

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> select Host,User,Plugin from mysql.user;
+-----------+------------------+-----------------------+
| Host      | User             | Plugin                |
+-----------+------------------+-----------------------+
| %         | root             | caching_sha2_password |
| localhost | mysql.infoschema | caching_sha2_password |
| localhost | mysql.session    | caching_sha2_password |
| localhost | mysql.sys        | caching_sha2_password |
| localhost | root             | caching_sha2_password |
+-----------+------------------+-----------------------+
5 rows in set (0.01 sec)

mysql> ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'changeme';
Query OK, 0 rows affected (0.03 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

mysql> exit
Bye
[email protected]:/# exit
exit
           

注意:預設的加密方式是caching_sha2_password,需要改為mysql_native_password,否則用戶端可能連接配接不上。

Nginx相關

Docker中啟動Nginx容器,Nginx容器提供檔案下載下傳功能,

準備nginx.conf,

user root;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
include /usr/share/nginx/modules/*.conf;
events {
    worker_connections 1024;
}
http {
    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;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;
	
    default_type        application/octet-stream;
    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
	
    include /etc/nginx/conf.d/*.conf;
    server {
        listen 80;
        server_name  _;
        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;
        # limit_rate 50k;
        location / {
            root    /usr/share/nginx/html/download;
			autoindex on;
			autoindex_exact_size off;
			#autoindex_localtime on;
			charset utf-8;
        }
        error_page 404 /404.html;
            location = /40x.html {
        }
        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }
}
           

關鍵的配置是下面這個,需要打開autoindex功能,

location / {
            root    /usr/share/nginx/html/download;
			autoindex on;
			autoindex_exact_size off;
			#autoindex_localtime on;
			charset utf-8;
        }
           

啟動容器:

docker run -d --name nginx -p 80:80 -v /root/test_docker/nginx/nginx.conf:/etc/nginx/nginx.conf -v /root/test_docker/nginx:/usr/share/nginx/html/download -v /data/nginx/conf.d:/etc/nginx/conf.d nginx
           

注意,上面指令中覆寫了預設的conf.d,否則還是不生效,

-v /data/nginx/conf.d:/etc/nginx/conf.d