我的docker随笔17:使用docker-compose启动MySQL、Redis和Mongo

一、背景

有网友咨询用docker-compose启动几个存储服务,这些服务包括了MySQL、Redis、Mongo。恰好笔记没做过这一方面,趁机会学习一下。

二、要求

需要启动的docker如下:

docker run -d --name myredis -v $PWD/data:/data -p 6378:6379 redis --appendonly yes

docker run --name=mysql -d -p 3306:3306 -e TZ=Asia/Shanghai \
--mount type=bind,source="$(PWD)"/data,target=/var/lib/mysql mysql/mysql-server:5.6 \
--character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci \
--explicit_defaults_for_timestamp=false

docker run -d --name mongo -p 27017:27017 mongo

分析如下:
除了常用的docker启动操作外,Redis需要添加持久化的配置,MySQL复杂很多,有时区,有挂载目录,有字符集编码配置。

三、实验

3.1 配置文件

先给出docker-compose文件,如下:

version: '2'
services:
    # 第一个容器 mysql
    foomysql:
        # 镜像名,如果不存在,则从网络下载,如果网络不存在,则会出错
        image: mysql:5.6
        # 容器名,在运行名称是唯一的
        container_name: foomysql
        restart: always # 如果容器运行出错,则重启容器
        # 挂载数据卷
        volumes:
            - $PWD/mysqldata:/var/lib/mysql
        environment:
            - TZ=Asia/Shanghai
            - MYSQL_ROOT_PASSWORD=root
            - MYSQL_USER=latelee
            - MYSQL_PASSWORD=123456
        # 端口映射
        ports:
            - "3306:3306"
        # 启动时执行的命令
        command: ['mysqld', '--character-set-server=utf8mb4', '--collation-server=utf8mb4_unicode_ci', '--explicit_defaults_for_timestamp=false']
        # 自定义的网络(见下[网络配置])
        networks:
            - foo-net

    redis:
        image: redis
        container_name: fooredis
        restart: always
        volumes:
            - $PWD/redisdata:/data
        ports:
            - "6379:6379"
        command: ["redis-server", "--appendonly", "yes"]
        networks:
            - foo-net

    mongo:
        image: mongo
        container_name: foomongo
        restart: always
        environment:
            MONGO_INITDB_ROOT_USERNAME: root
            MONGO_INITDB_ROOT_PASSWORD: root
        networks:
            - foo-net

    mongo-express:
        image: mongo-express
        container_name: foomongo-express
        restart: always
        ports:
            - 8081:8081
        environment:
            MONGO_INITDB_ROOT_USERNAME: root
            MONGO_INITDB_ROOT_PASSWORD: root
        networks:
            - foo-net


# 网络配置
networks:
    foo-net: # foo-net是自定义的网络名称
        driver: bridge  # 桥接方式

挑几个要点说明如下:
1、挂载目录可以使用$PWD/代替当前目录,也可以直接使用./。当存在多个容器服务时,目录名称最好要有辨识度。
2、如果希望容器在启动的同时带有参数运行,则使用command来指定。如command: ["redis-server", "--appendonly", "yes"],即等于redis-server --appendonly=yes,对应到docker命令,是docker run -d --name myredis -v $PWD/data:/data -p 6378:6379 redis --appendonly yes,注意,这句命令应该如此阅读:docker run -d --name myredis-v $PWD/data:/data-p 6378:6379redis--appendonly yes不能认为是redis --appendonly yes
3、mongo相关的环境变量,不能带前导的-,即正确的是:

environment:
  MONGO_INITDB_ROOT_USERNAME: root
  MONGO_INITDB_ROOT_PASSWORD: root

不能是:

environment:
  - MONGO_INITDB_ROOT_USERNAME: root
  - MONGO_INITDB_ROOT_PASSWORD: root

否则会提示:

ERROR: The Compose file './docker-compose.yml' is invalid because:
services.mongo.environment contains {"MONGO_INITDB_ROOT_USERNAME": "root"}, which is an invalid type, it should be a string

3.2 运行

使用docker-compose up命令可以直接启动。如果不希望有输出信息,则使用docker-compose up -d来启动。启动后,使用docker-compose ps命令查看运行状态:

docker-compose ps
      Name                    Command               State           Ports          
----------------------------------------------------------------------------------
foomongo           docker-entrypoint.sh mongod      Up      27017/tcp              
foomongo-express   tini -- /docker-entrypoint ...   Up      0.0.0.0:8081->8081/tcp 
foomysql           docker-entrypoint.sh mysql ...   Up      0.0.0.0:3306->3306/tcp 
fooredis           docker-entrypoint.sh redis ...   Up      0.0.0.0:6379->6379/tcp 

从信息上看,已经可以正常运行了。

3.3 分析

这里重要关注一下MySQL的字符集设置是否正确。操作如下,注释见中文:

root@latelee:/home/latelee/docker/composefile/mysql_test# docker exec -it foomysql bash # 进入容器
root@61858ee65fd1:/# mysql -uroot -proot # 使用root进入MySQL
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.45 MySQL Community Server (GPL)

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

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> show variables like '%character%'; # 查看字符集
+--------------------------+----------------------------+
| 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     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.00 sec)

mysql> show variables like 'collation%';
+----------------------+--------------------+
| Variable_name        | Value              |
+----------------------+--------------------+
| collation_connection | latin1_swedish_ci  |
| collation_database   | utf8mb4_unicode_ci |
| collation_server     | utf8mb4_unicode_ci |    ## !!!! 与配置的一致
+----------------------+--------------------+
3 rows in set (0.00 sec)

mysql> select user from mysql.user; ## 查询用户
+---------+
| user    |
+---------+
| latelee |
| root    |
| root    |
+---------+
3 rows in set (0.00 sec)

mysql> exit # 退出MySQL
Bye
root@61858ee65fd1:/# exit # 退出容器
exit

四、小结

针对网友的提问及本文的知识点,总结一下:
1、有些服务在启动时需要带参数,则可以在docker-compose文件中使用command来指定参数。
2、如果这样做还不能达到目的,则要考虑自行重新构建docker镜像,在启动文件中添加参数。
3、不同的服务,其运行的进程名称不同,如MySQL服务名为mysqld,Redis服务名为redis-server,这些知识只能在日常积累,需要参考官方的说明文档,没有规律可遵——其实接触使用多了,也就熟悉了。如果一定要找规律,Linux系统,守护进程(或服务进程)名称是以d结尾,d表示daemon
4、官方镜像都带有介绍详细的说明。包括如何用docker启动,如果配置docker-compose等等。请自行参阅。
5、项目需要同时使用多个docker容器时,强烈建议使用docker-compose来管理。
6、实际项目不同,功能不同,参数亦不同,本文内容仅作为参考。

五、扩展

1、本文所述“docker-compose文件”,——包括作者日常交流,都是指“docker-compose.yml”这个文件。而“docker-compose”则是指docker-compose这个工具本身。
2、默认情况下,直接使用docker-compose up -d命令启动即可。此时,docker-compose工具会在当前目录查找docker-compose.yml文件,并以目录名称作为工程名称。如果需要更改,则使用-f来指定docker-compose文件,-p指定工程名称。
3、docker-compose也有类似的

本文仅关注docker-compose的使用,对于MySQL、Redis、Mongo容器化未有深入研究,不作为实践的指导。如后续有机会,再进行研究。

附录

这是MySQL官方镜像及说明。
这是Redis官方镜像及说明。
这是Mongo官方镜像及说明。
这是mongo-express官方镜像及说明。

已标记关键词 清除标记
相关推荐
<div><p>after run docker-compose -f docker/docker-compose.yml up , I got this error info <img alt="image" src="https://user-images.githubusercontent.com/20986998/86879753-34f6f180-c11e-11ea-977b-2806d7e35a5f.png" /></p> <pre><code> BASH [root docker]# docker version Client: Docker Engine - Community Version: 19.03.5 API version: 1.40 Go version: go1.12.12 Git commit: 633a0ea Built: Wed Nov 13 07:25:41 2019 OS/Arch: linux/amd64 Experimental: false Server: Docker Engine - Community Engine: Version: 19.03.5 API version: 1.40 (minimum version 1.12) Go version: go1.12.12 Git commit: 633a0ea Built: Wed Nov 13 07:24:18 2019 OS/Arch: linux/amd64 Experimental: false containerd: Version: 1.2.10 GitCommit: b34a5c8af56e510852c35414db4c1f4fa6172339 runc: Version: 1.0.0-rc8+dev GitCommit: 3e425f80a8c931f88e6d94a8c831b9d5aa481657 docker-init: Version: 0.18.0 GitCommit: fec3683 </code></pre> <pre><code> BASH [root docker]# docker-compose version docker-compose version 1.26.2, build unknown docker-py version: 4.2.2 CPython version: 2.7.5 OpenSSL version: OpenSSL 1.0.2k-fips 26 Jan 2017 [root docker]# [root docker]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE mainflux/authn latest 572a7a451a4b 4 days ago 16.1MB mainflux/things latest 617b6eb4ea76 4 days ago 17.3MB mainflux/coap latest d3a6ddd5603f 4 days ago 14.9MB mainflux/users latest 9a04fd575588 10 days ago 16MB mainflux/http latest d8d8d06d3205 10 days ago 14.9MB redis 5.0-alpine 58084f18c7ec 4 weeks ago 29.7MB mainflux/ui latest 64a873e1f91e 2 months ago 44.5MB mainflux/ws latest e44b7d690062 2 months ago 13.7MB jaegertracing/all-in-one 1.13 7c596de19db2 12 months ago 48.7MB postgres 10.8-alpine b97fc2e43226 13 months ago 70.8MB nginx 1.16.0-alpine ef04b00b089d 14 months ago 20.4MB nats 1.3.0 40a6f5ca1594 22 months ago 7.76MB [root docker]# [root docker]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 150a684a6826 nginx:1.16.0-alpine "/entrypoint.sh" 5 hours ago Restarting (1) 44 seconds ago mainflux-nginx 71347a75fdfd mainflux/ws:latest "/exe" 5 hours ago Up 5 hours 0.0.0.0:8186->8186/tcp mainflux-ws a9d1f293e04d mainflux/coap:latest "/exe" 5 hours ago Up 5 hours 0.0.0.0:5683->5683/tcp, 0.0.0.0:5683->5683/udp mainflux-coap ed2876a6274c mainflux/http:latest "/exe" 5 hours ago Up 5 hours 0.0.0.0:8185->8185/tcp mainflux-http 11eb8723301b mainflux/users:latest "/exe" 5 hours ago Up 5 hours 0.0.0.0:8180->8180/tcp mainflux-users 6b16a450b3b0 mainflux/things:latest "/exe" 5 hours ago Up 5 hours 0.0.0.0:8182-8183->8182-8183/tcp, 0.0.0.0:8989->8989/tcp mainflux-things 72f58ebf7887 mainflux/authn:latest "/exe" 5 hours ago Up 5 hours 0.0.0.0:8181->8181/tcp, 0.0.0.0:8189->8189/tcp mainflux-authn a242f4b510ef postgres:10.8-alpine "docker-entrypoint.s…" 5 hours ago Up 5 hours 5432/tcp mainflux-authn-db 292ec00a9fbf postgres:10.8-alpine "docker-entrypoint.s…" 5 hours ago Up 5 hours 5432/tcp mainflux-users-db 91576bfc6c04 redis:5.0-alpine "docker-entrypoint.s…" 5 hours ago Up 5 hours 6379/tcp mainflux-es-redis 9c88cbf5708f redis:5.0-alpine "docker-entrypoint.s…" 5 hours ago Up 5 hours 6379/tcp mainflux-things-redis 8805ba940a7d nats:1.3.0 "/gnatsd -c gnatsd.c…" 5 hours ago Up 5 hours 4222/tcp, 6222/tcp, 8222/tcp mainflux-nats cb3adb0858b3 jaegertracing/all-in-one:1.13 "/go/bin/all-in-one-…" 5 hours ago Up 5 hours 5775/udp, 0.0.0.0:5778->5778/tcp, 0.0.0.0:14268->14268/tcp, 6832/udp, 0.0.0.0:16686->16686/tcp, 0.0.0.0:6831->6831/udp, 14250/tcp mainflux-jaeger 93d6ae4487b5 postgres:10.8-alpine "docker-entrypoint.s…" 5 hours ago Up 5 hours 5432/tcp mainflux-things-db 47a082c14249 mainflux/ui:latest "nginx -g 'daemon of…" 5 hours ago Up 5 hours 80/tcp, 0.0.0.0:3000->3000/tcp mainflux-ui [root docker]# [root docker]# docker-compose ps ERROR: The Compose file './docker-compose.yml' is invalid because: services.nginx.ports value [u':', u':', u':', u':'] has non-unique elements services.things.ports value [u':', u':', u':'] has non-unique elements services.authn.ports value [u':', u':'] has non-unique elements services.jaeger.ports value [u':/udp', u':', u':', u':'] has non-unique elements services.authn.expose is invalid: should be of the format 'PORT[/PROTOCOL]' services.authn.ports contains an invalid type, it should be a number, or an object services.authn.ports contains an invalid type, it should be a number, or an object services.http-adapter.ports contains an invalid type, it should be a number, or an object services.jaeger.ports contains an invalid type, it should be a number, or an object services.jaeger.ports contains an invalid type, it should be a number, or an object services.jaeger.ports contains an invalid type, it should be a number, or an object services.nginx.ports contains an invalid type, it should be a number, or an object services.nginx.ports contains an invalid type, it should be a number, or an object services.nginx.ports contains an invalid type, it should be a number, or an object services.nginx.ports contains an invalid type, it should be a number, or an object services.things.ports contains an invalid type, it should be a number, or an object services.things.ports contains an invalid type, it should be a number, or an object services.things.ports contains an invalid type, it should be a number, or an object services.ui.ports contains an invalid type, it should be a number, or an object services.users.ports contains an invalid type, it should be a number, or an object services.ws-adapter.ports contains an invalid type, it should be a number, or an object services.coap-adapter.ports contains an invalid type, it should be a number, or an object services.coap-adapter.ports contains an invalid type, it should be a number, or an object services.jaeger.ports contains an invalid type, it should be a number, or an object [root mainflux-0.10.0]# docker-compose -f docker/docker-compose.yml ps Name Command State Ports -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- mainflux-authn /exe Up 0.0.0.0:8181->8181/tcp, 0.0.0.0:8189->8189/tcp mainflux-authn-db docker-entrypoint.sh postgres Up 5432/tcp mainflux-coap /exe Up 0.0.0.0:5683->5683/tcp, 0.0.0.0:5683->5683/udp mainflux-es-redis docker-entrypoint.sh redis ... Up 6379/tcp mainflux-http /exe Up 0.0.0.0:8185->8185/tcp mainflux-jaeger /go/bin/all-in-one-linux - ... Up 14250/tcp, 0.0.0.0:14268->14268/tcp, 0.0.0.0:16686->16686/tcp, 5775/udp, 0.0.0.0:5778->5778/tcp, 0.0.0.0:6831->6831/udp, 6832/udp mainflux-nats /gnatsd -c gnatsd.conf Up 4222/tcp, 6222/tcp, 8222/tcp mainflux-nginx /entrypoint.sh Restarting mainflux-things /exe Up 0.0.0.0:8182->8182/tcp, 0.0.0.0:8183->8183/tcp, 0.0.0.0:8989->8989/tcp mainflux-things-db docker-entrypoint.sh postgres Up 5432/tcp mainflux-things-redis docker-entrypoint.sh redis ... Up 6379/tcp mainflux-ui nginx -g daemon off; Up 0.0.0.0:3000->3000/tcp, 80/tcp mainflux-users /exe Up 0.0.0.0:8180->8180/tcp mainflux-users-db docker-entrypoint.sh postgres Up 5432/tcp mainflux-ws /exe Up 0.0.0.0:8186->8186/tcp </code></pre><p>该提问来源于开源项目:mainflux/mainflux</p></div>
©️2020 CSDN 皮肤主题: 技术工厂 设计师:CSDN官方博客 返回首页