15 Docker 应用部署
部署 MySQL
需求
在 Docker 容器中部署 MySQL,并通过外部 mysql 客户端操作 MySQL Server。
实现步骤
端口映射
- 容器内的网络服务和外部机器不能直接通信
- 外部机器和宿主机可以直接通信
- 宿主机和容器可以直接通信
- 当容器中的网络服务需要被外部机器访问时,可以将容器中提供服务的端口映射到宿主机的端口上。外部机器访问宿主机的该端口,从而间接访问容器的服务。
搜索 mysql 镜像
bash❯ dk search mysql NAME DESCRIPTION STARS OFFICIAL AUTOMATED mysql MySQL is a widely used, open-source relation… 14637 [OK] mariadb MariaDB Server is a high performing open sou… 5582 [OK] percona Percona Server is a fork of the MySQL relati… 622 [OK] phpmyadmin phpMyAdmin - A web interface for MySQL and M… 901 [OK] bitnami/mysql Bitnami MySQL Docker Image 103 [OK] circleci/mysql MySQL is a widely used, open-source relation… 29 bitnami/mysqld-exporter 5 cimg/mysql 2 ubuntu/mysql MySQL open source fast, stable, multi-thread… 54 rapidfort/mysql RapidFort optimized, hardened image for MySQL 25 rapidfort/mysql8-ib RapidFort optimized, hardened image for MySQ… 9 google/mysql MySQL server for Google Compute Engine 25 [OK] rapidfort/mysql-official RapidFort optimized, hardened image for MySQ… 9 hashicorp/mysql-portworx-demo 0 elestio/mysql Mysql, verified and packaged by Elestio 0 newrelic/mysql-plugin New Relic Plugin for monitoring MySQL databa… 1 [OK] bitnamicharts/mysql 0 databack/mysql-backup Back up mysql databases to... anywhere! 104 linuxserver/mysql A Mysql container, brought to you by LinuxSe… 41 mirantis/mysql 0 docksal/mysql MySQL service images for Docksal - https://d… 0 linuxserver/mysql-workbench 52 vitess/mysqlctld vitess/mysqlctld 1 [OK] eclipse/mysql Mysql 5.7, curl, rsync 1 [OK] drupalci/mysql-5.5 https://www.drupal.org/project/drupalci 3 [OK] ❯ dksefo mysql NAME DESCRIPTION STARS OFFICIAL AUTOMATED mysql MySQL is a widely used, open-source relation… 14637 [OK] mariadb MariaDB Server is a high performing open sou… 5582 [OK] percona Percona Server is a fork of the MySQL relati… 622 [OK] phpmyadmin phpMyAdmin - A web interface for MySQL and M… 901 [OK]拉取 mysql 镜像
bash❯ curl -L -s 'https://registry.hub.docker.com/v2/repositories/library/mysql/tags?page_size=25' | jq -r '."results"[]["name"]' latest 8.0.35-debian 8.0-debian 5.7.44-oracle 5.7.44 5.7-oracle 5.7 5-oracle 5 oracle innovation-oracle innovation 8.2.0-oracle 8.2.0 8.2-oracle 8.2 8.0.35-oracle 8.0.35 8.0-oracle 8.0 8-oracle 8 8.1.0-oracle 8.1.0 8.1-oracle ❯ dkpl mysql:8.0-debian 8.0-debian: Pulling from library/mysql b7f91549542c: Pull complete 1c04f50b14af: Pull complete 7fb2b8bfab5d: Pull complete 06848c6e0c84: Pull complete b98691176bf0: Pull complete 7a46cc0d0031: Pull complete e2b33a1d9de8: Pull complete 38e8fe85abdf: Pull complete 4ad0e42be1e0: Pull complete 1f991fc99fab: Pull complete 0f16fe0caddd: Pull complete bbbe64f95283: Pull complete Digest: sha256:b8468d29f56498197888c573b7fa976cba3419233d4c9e405bc4d5c11a6b41c5 Status: Downloaded newer image for mysql:8.0-debian docker.io/library/mysql:8.0-debian ❯ dkIls REPOSITORY TAG IMAGE ID CREATED SIZE mysql 8.0-debian 3fd3c2ee4c5a 4 weeks ago 600MB创建容器,设置端口映射,目录挂载
yamlversion: "3.8" services: mysql: image: mysql:8.0-debian container_name: db-mysql command: --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci --max_connections=1000 restart: always ports: - 3307:3306 volumes: - ./mysql/conf:/etc/mysql/conf.d - ./mysql/logs:/logs - ./mysql/data:/var/lib/mysql environment: MYSQL_ROOT_PASSWORD: 123 TZ: Asia/Shanghai networks: - docker-compose-demobash❯ docker run -d \ --name db-mysql \ --restart=always \ -p 3307:3306 \ -v ./mysql/conf:/etc/mysql/conf.d \ -v ./mysql/logs:/logs \ -v ./mysql/data:/var/lib/mysql \ -e MYSQL_ROOT_PASSWORD=123 \ -e TZ=Asia/Shanghai \ --network=docker-compose-demo \ mysql:8.0-debian --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci --max_connections=1000 bcdac1001c56d5df1a3879d14cad105e04e54fdda8adcc22cc815746101f0817操作 mysql 数据库
bash❯ docker exec -it db-mysql mysql -u root -p123 -e 'SHOW DATABASES;' # 查看所有数据库 mysql: [Warning] Using a password on the command line interface can be insecure. +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ ❯ dex db-mysql mysql -u root -p123 -e "SHOW VARIABLES LIKE 'character_set_%'; SHOW VARIABLES LIKE 'collation_%';" # 查看字符集和排序规则 mysql: [Warning] Using a password on the command line interface can be insecure. +--------------------------+--------------------------------+ | Variable_name | Value | +--------------------------+--------------------------------+ | character_set_client | latin1 | | character_set_connection | latin1 | | character_set_database | utf8mb4 | | character_set_filesystem | binary | | character_set_results | latin1 | | character_set_server | utf8mb4 | | character_set_system | utf8mb3 | | character_sets_dir | /usr/share/mysql-8.0/charsets/ | +--------------------------+--------------------------------+ +----------------------+--------------------+ | Variable_name | Value | +----------------------+--------------------+ | collation_connection | latin1_swedish_ci | | collation_database | utf8mb4_unicode_ci | | collation_server | utf8mb4_unicode_ci | +----------------------+--------------------+ ❯ dex db-mysql mysql -u root -p123 -e "SHOW VARIABLES LIKE 'innodb_buffer_pool_size';" # 查看 innodb_buffer_pool_size 参数 mysql: [Warning] Using a password on the command line interface can be insecure. +-------------------------+-----------+ | Variable_name | Value | +-------------------------+-----------+ | innodb_buffer_pool_size | 134217728 | +-------------------------+-----------+ ❯ dex db-mysql mysql -u root -p123 -e "SELECT (@@innodb_buffer_pool_size / 1024 / 1024) AS innodb_buffer_pool_size_in_mb;" # 查看 innodb_buffer_pool_size 参数并将结果换算成 MB mysql: [Warning] Using a password on the command line interface can be insecure. +-------------------------------+ | innodb_buffer_pool_size_in_mb | +-------------------------------+ | 128.00000000 | +-------------------------------+停止容器,删除容器和本地挂载目录
bash❯ docker-compose down ❯ sudo rm -rf mysql
部署 Tomcat
需求
在 Docker 容器中部署 Tomcat,并通过外部机器访问 Tomcat 部署的项目。
实现步骤
搜索 tomcat 镜像
bash❯ dksefo tomcat NAME DESCRIPTION STARS OFFICIAL AUTOMATED tomcat Apache Tomcat is an open source implementati… 3607 [OK] tomee Apache TomEE is an all-Apache Java EE certif… 113 [OK]拉取 tomcat 镜像
bash❯ curl -L -s 'https://registry.hub.docker.com/v2/repositories/library/tomcat/tags?page_size=100' | jq -r '."results"[]["name"]' | grep 8.5 8.5.96-jdk8-corretto-al2 8.5.96-jdk8-corretto 8.5.96-jdk21-corretto-al2 8.5.96-jdk21-corretto 8.5.96-jdk17-corretto-al2 8.5.96-jdk17-corretto 8.5.96-jdk11-corretto-al2 8.5.96-jdk11-corretto 8.5-jdk8-corretto-al2 8.5-jdk8-corretto 8.5-jdk21-corretto-al2 8.5-jdk21-corretto 8.5-jdk17-corretto-al2 8.5-jdk17-corretto 8.5-jdk11-corretto-al2 8.5-jdk11-corretto ❯ dkpl tomcat:8.5-jdk11-corretto 8.5-jdk11-corretto: Pulling from library/tomcat 0b4a6f011995: Pull complete 2d619674e889: Pull complete 454186275587: Pull complete a97501b03fb7: Pull complete 86aa7e5da5f2: Pull complete Digest: sha256:1e375ed130bf213aa1a7a73f49f2ba47718f780abadfae1567253d19802fd295 Status: Downloaded newer image for tomcat:8.5-jdk11-corretto docker.io/library/tomcat:8.5-jdk11-corretto ❯ dkIls REPOSITORY TAG IMAGE ID CREATED SIZE tomcat 8.5-jdk11-corretto a79fefa24f8f 3 days ago 482MB mysql 8.0-debian 3fd3c2ee4c5a 4 weeks ago 600MB创建容器,设置端口映射,目录挂载
yamlversion: "3.8" services: tomcat: image: tomcat:8.5-jdk11-corretto container_name: tomcat_container restart: always ports: - 8080:8080 volumes: - ./tomcat/webapps:/usr/local/tomcat/webappsbashdocker run -d \ --name tomcat_container \ --restart=always \ -p 8080:8080 \ -v ./tomcat/webapps:/usr/local/tomcat/webapps \ tomcat:8.5-jdk11-corretto访问 tomcat
bash❯ curl localhost:8080 <!doctype html><html lang="en"><head><title>HTTP Status 404 – Not Found</title><style type="text/css">body {font-family:Tahoma,Arial,sans-serif;} h1, h2, h3, b {color:white;background-color:#525D76;} h1 {font-size:22px;} h2 {font-size:16px;} h3 {font-size:14px;} p {font-size:12px;} a {color:black;} .line {height:1px;background-color:#525D76;border:none;}</style></head><body><h1>HTTP Status 404 – Not Found</h1><hr class="line" /><p><b>Type</b> Status Report</p><p><b>Description</b> The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.</p><hr class="line" /><h3>Apache Tomcat/8.5.96</h3></body></html>%停止容器,删除容器和本地挂载目录
bash❯ dkcd # docker-compose down [+] Running 2/2 ✔ Container tomcat_container Removed 0.3s ✔ Network demo_default Removed 0.3s ❯ dkci CONTAINER REPOSITORY TAG IMAGE ID SIZE ❯ sudo rm -rf tomcat
部署 Nginx
需求
在 Docker 容器中部署 Nginx,并通过外部机器访问 Nginx。
实现步骤
搜索 nginx 镜像
bash❯ dksefo nginx NAME DESCRIPTION STARS OFFICIAL AUTOMATED nginx Official build of Nginx. 19271 [OK] unit Official build of NGINX Unit: Universal Web … 17 [OK]拉取 nginx 镜像
bash❯ curl -L -s 'https://registry.hub.docker.com/v2/repositories/library/nginx/tags?page_size=100' | jq -r '."results"[]["name"]' | grep alpine mainline-alpine3.18-slim mainline-alpine3.18-perl mainline-alpine3.18 mainline-alpine-slim mainline-alpine-perl mainline-alpine alpine3.18-slim alpine3.18-perl alpine3.18 alpine-slim alpine-perl alpine 1.25.3-alpine3.18-slim 1.25.3-alpine3.18-perl 1.25.3-alpine3.18 1.25.3-alpine-slim 1.25.3-alpine-perl 1.25.3-alpine 1.25-alpine3.18-slim 1.25-alpine3.18-perl 1.25-alpine3.18 1.25-alpine-slim 1.25-alpine-perl 1.25-alpine 1-alpine3.18-slim 1-alpine3.18-perl 1-alpine3.18 1-alpine-slim 1-alpine-perl 1-alpine stable-alpine3.17-slim stable-alpine3.17-perl stable-alpine3.17 stable-alpine-slim stable-alpine-perl stable-alpine 1.25.2-alpine3.18-slim 1.25.2-alpine3.18-perl 1.25.2-alpine3.18 1.25.2-alpine-slim 1.25.2-alpine-perl 1.25.2-alpine 1.24.0-alpine3.17-slim 1.24.0-alpine3.17-perl 1.24.0-alpine3.17 1.24.0-alpine-slim 1.24.0-alpine-perl 1.24.0-alpine 1.24-alpine3.17-slim 1.24-alpine3.17-perl 1.24-alpine3.17 1.24-alpine-slim 1.24-alpine-perl 1.24-alpine ❯ dkpl nginx:1.25-alpine 1.25-alpine: Pulling from library/nginx 96526aa774ef: Already exists 740091335c74: Already exists da9c2e764c5b: Already exists ade17ad21ef4: Already exists 4e6f462c8a69: Already exists 1324d9977cd2: Already exists 1b9b96da2c74: Already exists 153aef7ca07f: Already exists Digest: sha256:db353d0f0c479c91bd15e01fc68ed0f33d9c4c52f3415e63332c3d0bf7a4bb77 Status: Downloaded newer image for nginx:1.25-alpine docker.io/library/nginx:1.25-alpine ❯ dkIls REPOSITORY TAG IMAGE ID CREATED SIZE tomcat 8.5-jdk11-corretto a79fefa24f8f 3 days ago 482MB nginx 1.25-alpine b135667c9898 4 weeks ago 47.7MB mysql 8.0-debian 3fd3c2ee4c5a 4 weeks ago 600MB创建 nginx.conf 文件
bashmkdir conf tee conf/nginx.conf <<-'EOF' user nginx; # 用户 worker_processes 1; # 工作进程数量 error_log /var/log/nginx/error.log warn; # 错误日志路径 pid /var/run/nginx.pid; # PID 文件路径 events { worker_connections 1024; # 每个工作进程的最大连接数 } http { include /etc/nginx/mime.types; # 包含 MIME 类型定义文件 default_type application/octet-stream; # 默认 MIME 类型 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; # 开启 sendfile #tcp_nopush on; # Nginx 会尽可能地将多个小的响应包合并成一个大的 TCP 包来发送 keepalive_timeout 65; # keepalive 超时时间 #gzip on; # gzip 压缩 include /etc/nginx/conf.d/*.conf; # 包含其他配置文件 } EOF创建容器,设置端口映射,目录挂载
yamlversion: "3.8" services: web: image: nginx:1.25-alpine container_name: nginx_container restart: always ports: - "80:80" volumes: - ./conf/nginx.conf:/etc/nginx/nginx.conf - ./logs:/var/log/nginx - ./html:/usr/share/nginx/htmlbashdocker run -d \ --name nginx_container \ --restart always \ -p 80:80 \ -v $pwd/conf/nginx.conf:/etc/nginx/nginx.conf \ -v $pwd/logs:/var/log/nginx \ -v $pwd/html:/usr/share/nginx/html \ nginx:1.25-alpine访问 nginx
bash❯ dkcU [+] Building 0.0s (0/0) docker:default [+] Running 2/2 ✔ Network demo_default Created 0.1s ✔ Container nginx_container Started 0.1s ❯ curl localhost <html> <head><title>403 Forbidden</title></head> <body> <center><h1>403 Forbidden</h1></center> <hr><center>nginx/1.25.3</center> </body> </html>停止容器,删除容器和本地挂载目录
bash❯ dkcd [+] Running 2/2 ✔ Container nginx_container Removed 0.3s ✔ Network demo_default Removed 0.3s ❯ sudo rm -rf conf html logs
部署 Redis
需求
在 Docker 容器中部署 Redis,并通过外部机器访问 Redis。
实现步骤
搜索 redis 镜像
bash❯ dksefo redis NAME DESCRIPTION STARS OFFICIAL AUTOMATED redis Redis is an open source key-value store that… 12498 [OK]拉取 redis 镜像
bash❯ curl -L -s 'https://registry.hub.docker.com/v2/repositories/library/redis/tags?page_size=100' | jq -r '."results"[]["name"]' | grep alpine alpine3.18 alpine 7.2.3-alpine3.18 7.2.3-alpine 7.2-alpine3.18 7.2-alpine 7.0.14-alpine3.18 7.0.14-alpine 7.0-alpine3.18 7.0-alpine 7-alpine3.18 7-alpine 6.2.14-alpine3.18 6.2.14-alpine 6.2-alpine3.18 6.2-alpine 6.0.20-alpine3.18 6.0.20-alpine 6.0-alpine3.18 6.0-alpine 6-alpine3.18 6-alpine ❯ dkpl redis:7-alpine 7-alpine: Pulling from library/redis 96526aa774ef: Already exists f150590280ce: Pull complete f28642122f09: Pull complete 3b02f0386730: Pull complete 42c996777dea: Pull complete 4f4fb700ef54: Pull complete dce1e561adcd: Pull complete Digest: sha256:6a7b3c6e3a6854424d96953172cac1ca97f0fc90094bcc479f3949e29bb053af Status: Downloaded newer image for redis:7-alpine docker.io/library/redis:7-alpine ❯ dkIls REPOSITORY TAG IMAGE ID CREATED SIZE tomcat 8.5-jdk11-corretto a79fefa24f8f 3 days ago 482MB redis 7-alpine 246a9110fd9e 2 weeks ago 43.4MB nginx 1.25-alpine b135667c9898 4 weeks ago 47.7MB mysql 8.0-debian 3fd3c2ee4c5a 4 weeks ago 600MB创建容器,设置端口映射,目录挂载
yamlversion: "3.8" services: redis: image: redis:7-alpine container_name: redis_container restart: always command: redis-server --requirepass 123456 ports: - "6379:6379" volumes: - redis_data:/data volumes: redis_data:bashdocker run \ --name redis_container \ --restart always \ -p 6379:6379 \ -v redis_data:/data \ redis:7-alpine redis-server --requirepass 123456访问 redis
bash❯ dkcU [+] Building 0.0s (0/0) docker:default [+] Running 3/3 ✔ Network demo_default Created 0.1s ✔ Volume "demo_redis_data" Created 0.0s ✔ Container redis_container Started 0.1s ❯ dex redis_container sh -c 'echo -e "AUTH 123456\nSET foo bar\nGET foo" | redis-cli -h localhost -p 6379' OK OK "bar"停止容器,删除容器
bash❯ dkcd [+] Running 2/2 ✔ Container redis_container Removed 0.3s ✔ Network demo_default Removed 0.2s查看 Docker 卷中的 Redis 数据。
bash❯ docker volume inspect demo_redis_data [ { "CreatedAt": "2023-11-25T12:40:44+08:00", "Driver": "local", "Labels": { "com.docker.compose.project": "demo", "com.docker.compose.version": "2.23.1", "com.docker.compose.volume": "redis_data" }, "Mountpoint": "/var/lib/docker/volumes/demo_redis_data/_data", "Name": "demo_redis_data", "Options": null, "Scope": "local" } ] ❯ sudo ls /var/lib/docker/volumes/demo_redis_data/_data dump.rdb