{"id":42177,"date":"2024-12-01T09:06:46","date_gmt":"2024-12-01T01:06:46","guid":{"rendered":"https:\/\/fwq.ai\/blog\/42177\/"},"modified":"2024-12-01T09:06:46","modified_gmt":"2024-12-01T01:06:46","slug":"web%e6%9c%8d%e5%8a%a1%e5%99%a8%e6%97%a0%e6%b3%95%e9%80%9a%e8%bf%87docker-compose%e8%bf%9e%e6%8e%a5%e5%88%b0redis","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/42177\/","title":{"rendered":"Web\u670d\u52a1\u5668\u65e0\u6cd5\u901a\u8fc7docker-compose\u8fde\u63a5\u5230Redis"},"content":{"rendered":"<p><b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span>  <span>&gt;<\/span>  <span>&gt;<\/span>  <span>&gt;<\/span> <span>Web\u670d\u52a1\u5668\u65e0\u6cd5\u901a\u8fc7docker-compose\u8fde\u63a5\u5230Redis<\/span><\/p>\n<h1>Web\u670d\u52a1\u5668\u65e0\u6cd5\u901a\u8fc7docker-compose\u8fde\u63a5\u5230Redis<\/h1>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-22 21:36:32<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u54c8\u55bd\uff01\u4eca\u5929\u5fc3\u8840\u6765\u6f6e\u7ed9\u5927\u5bb6\u5e26\u6765\u4e86<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aWeb\u670d\u52a1\u5668\u65e0\u6cd5\u901a\u8fc7docker-compose\u8fde\u63a5\u5230Redis\u300b<\/span>\uff0c\u60f3\u5fc5\u5927\u5bb6\u5e94\u8be5\u5bf9<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\u90fd\u4e0d\u964c\u751f\u5427\uff0c\u90a3\u4e48\u9605\u8bfb\u672c\u6587\u5c31\u90fd\u4e0d\u4f1a\u5f88\u56f0\u96be\uff0c\u4ee5\u4e0b\u5185\u5bb9\u4e3b\u8981\u6d89\u53ca\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\uff0c\u82e5\u662f\u4f60\u6b63\u5728\u5b66\u4e60<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">Golang<\/span>\uff0c\u5343\u4e07\u522b\u9519\u8fc7\u8fd9\u7bc7\u6587\u7ae0~\u5e0c\u671b\u80fd\u5e2e\u52a9\u5230\u4f60\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6211\u6b63\u5728\u5c1d\u8bd5\u901a\u8fc7\u540c\u4e00\u7f51\u7edc\u4e0a\u7684\u7f51\u7edc\u670d\u52a1\u5668\u8fde\u63a5\u5230 docker \u7f51\u7edc\u4e0a\u7684 redis \u5b9e\u4f8b\u3002\u6211\u7684\u4ee3\u7801\u5728 go \u4e2d\u5e76\u4e14\u6211\u6b63\u5728\u4f7f\u7528 \u6211\u7684 docker-compose.yml\uff1a<\/p>\n<pre>thor-redis:\n    image: redis:5.0.7\n    networks:\n      - thorcast\n    ports:\n      - \"6380:6379\"\n    volumes:\n      - .\/redis.conf:\/usr\/local\/etc\/redis.conf\n    entrypoint: redis-server \/usr\/local\/etc\/redis.conf\nthorcast-server:\n    build: ..\/..\/thorcast-server\n    volumes:\n      - ..\/..\/thorcast-server:\/app\/thorcast-server\n    networks:\n      - thorcast\n    ports:\n      - \"8000:8000\"\n    env_file:\n      - ..\/..\/thorcast-server\/env.list<\/pre>\n<p>env.list \u6587\u4ef6\u7684\u76f8\u5173\u90e8\u5206\uff1a<\/p>\n<pre>redis_host=thor-redis\nredis_port=6380\nredis_db=0\nredis_password=redis_pass<\/pre>\n<p>\u6211\u7684\u5e94\u7528\u7a0b\u5e8f\u4e2d\u8fde\u63a5\u5230 redis \u7684\u4ee3\u7801\u662f\uff1a<\/p>\n<pre>a.redis = redis.newclient(&amp;redis.options{\n    addr:       fmt.sprintf(\"%s:%s\", conf.redishost, conf.redisport),\n    password:   conf.redispassword,\n    db:         conf.redisdb,\n})<\/pre>\n<p>\u6211\u7684\u586b\u5145conf\u7684\u4ee3\u7801\u662f\uff1a<\/p>\n<pre>type config struct {\n    sqlusername     string\n    sqlpassword     string\n    sqlhost         string\n    sqlport         string\n    sqldbname       string\n    redispassword   string\n    redishost       string\n    redisport       string\n    redisdb         int\n}\n\/\/ method to initialize config struct from environment variables\nfunc (conf *config) configure() {\n    conf.sqlusername = os.getenv(\"thorcast_db_username\")\n    conf.sqlpassword = os.getenv(\"thorcast_db_password\")\n    conf.sqlhost = os.getenv(\"thorcast_db_host\")\n    conf.sqlport = os.getenv(\"thorcast_db_port\")\n    conf.sqldbname = os.getenv(\"thorcast_db_name\")\n    conf.redispassword = os.getenv(\"redis_password\")\n    conf.redishost = os.getenv(\"redis_host\")\n    conf.redisport = os.getenv(\"redis_port\")\n    conf.redisdb, _ = strconv.atoi(os.getenv(\"redis_db\"))\n}\nvar conf = config{}\n\ntype app struct {\n    router *mux.router\n    logger http.handler\n    db     *sql.db\n    redis  *redis.client\n}<\/pre>\n<p>\u5728redis.conf\u6587\u4ef6\u4e2d\uff0c\u6ca1\u6709\u6307\u5b9a\u7ed1\u5b9aip\uff0c\u5e76\u4e14requirepass\u8bbe\u7f6e\u4e3a\u4e0eredis_pass\u76f8\u540c\u7684\u503c\u3002\u6b64\u5916\uff0c\u6211\u5728\u670d\u52a1\u5668\u4e0a\u770b\u5230\u7684\u9519\u8bef\u662f\uff1a<\/p>\n<pre>{\"error\":\"dial tcp 172.21.0.5:6380: connect: connection refused\"}<\/pre>\n<p>\u5f53\u6211\u68c0\u67e5 redis \u6620\u50cf\u65f6\uff0c\u6211\u53ef\u4ee5\u786e\u8ba4\u5b83\u5177\u6709\u4ee5\u4e0b ip \u5730\u5740\uff1a<\/p>\n<pre>docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' dev_thor- \nredis_1\n172.21.0.5<\/pre>\n<p>\u5f53\u6211\u5c1d\u8bd5\u901a\u8fc7 redis-cli \u8fde\u63a5\u672c\u5730\u8ba1\u7b97\u673a\u65f6\uff0c\u6211\u53ef\u4ee5\u6beb\u65e0\u95ee\u9898\u5730\u8bbf\u95ee\u670d\u52a1\u5668\u3002\u6211\u5728\u8c37\u6b4c\u4e0a\u641c\u7d22\u4e86\u4e00\u4e0b\uff0c\u8bd5\u56fe\u627e\u5230\u7c7b\u4f3c\u7684\u95ee\u9898\uff0c\u4f46\u5b83\u4eec\u4f3c\u4e4e\u90fd\u4e0e\u8bd5\u56fe\u901a\u8fc7\u672c\u5730\u4e3b\u673a\u8bbf\u95ee redis \u7684\u4eba\u6709\u5173\uff0c\u800c\u6211\u5e76\u4e0d\u60f3\u8fd9\u6837\u505a\u3002\u975e\u5e38\u611f\u8c22\u4efb\u4f55\u5e2e\u52a9\u3002<\/p>\n<p> <\/p>\n<h2>\u89e3\u51b3\u65b9\u6848<\/h2>\n<p> <\/p>\n<p>\u60a8\u53ef\u80fd\u5fd8\u8bb0\u4ece\u7f51\u7edc\u89d2\u5ea6\u5c06\u60a8\u7684\u5e94\u7528\u7a0b\u5e8f\u4e0e redis \u94fe\u63a5<\/p>\n<pre>thor-redis:\n        image: redis:5.0.7\n        networks:\n          - thorcast\n        ports:\n          - \"6380:6379\"\n        volumes:\n          - .\/redis.conf:\/usr\/local\/etc\/redis.conf\n        entrypoint: redis-server \/usr\/local\/etc\/redis.conf\n    thorcast-server:\n        build: ..\/..\/thorcast-server\n        volumes:\n          - ..\/..\/thorcast-server:\/app\/thorcast-server\n        networks:\n          - thorcast\n        ports:\n          - \"8000:8000\"\n        env_file:\n          - ..\/..\/thorcast-server\/env.list\n        links:          # &lt;-- Add this\n          - thor-redis  # &lt;-- Add this<\/pre>\n<p>\u597d\u4e86\uff0c\u672c\u6587\u5230\u6b64\u7ed3\u675f\uff0c\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u300aWeb\u670d\u52a1\u5668\u65e0\u6cd5\u901a\u8fc7docker-compose\u8fde\u63a5\u5230Redis\u300b\uff0c\u5e0c\u671b\u672c\u6587\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff01\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff0c\u7ed9\u5927\u5bb6\u5206\u4eab\u66f4\u591aGolang\u77e5\u8bc6\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &gt; &gt; Web\u670d\u52a1\u5668\u65e0\u6cd5\u901a\u8fc7docker-compose\u8fde\u63a5\u5230Redis Web\u670d\u52a1\u5668\u65e0\u6cd5\u901a\u8fc7docker-compose\u8fde\u63a5\u5230Redis \u6765\u6e90\uff1astackoverflow 2024-04-22 21:36:32 0\u6d4f\u89c8 \u6536\u85cf \u54c8\u55bd\uff01\u4eca\u5929\u5fc3\u8840\u6765\u6f6e\u7ed9\u5927\u5bb6\u5e26\u6765\u4e86\u300aWeb\u670d\u52a1\u5668\u65e0\u6cd5\u901a\u8fc7docker-compose\u8fde\u63a5\u5230Redis\u300b\uff0c\u60f3\u5fc5\u5927\u5bb6\u5e94\u8be5\u5bf9Golang\u90fd\u4e0d\u964c\u751f\u5427\uff0c\u90a3\u4e48\u9605\u8bfb\u672c\u6587\u5c31\u90fd\u4e0d\u4f1a\u5f88\u56f0\u96be\uff0c\u4ee5\u4e0b\u5185\u5bb9\u4e3b\u8981\u6d89\u53ca\u5230\uff0c\u82e5\u662f\u4f60\u6b63\u5728\u5b66\u4e60Golang\uff0c\u5343\u4e07\u522b\u9519\u8fc7\u8fd9\u7bc7\u6587\u7ae0~\u5e0c\u671b\u80fd\u5e2e\u52a9\u5230\u4f60\uff01 \u95ee\u9898\u5185\u5bb9 \u6211\u6b63\u5728\u5c1d\u8bd5\u901a\u8fc7\u540c\u4e00\u7f51\u7edc\u4e0a\u7684\u7f51\u7edc\u670d\u52a1\u5668\u8fde\u63a5\u5230 docker \u7f51\u7edc\u4e0a\u7684 redis \u5b9e\u4f8b\u3002\u6211\u7684\u4ee3\u7801\u5728 go \u4e2d\u5e76\u4e14\u6211\u6b63\u5728\u4f7f\u7528 \u6211\u7684 docker-compose.yml\uff1a thor-redis: image: redis:5.0.7 networks: &#8211; thorcast ports: &#8211; &#8220;6380:6379&#8221; volumes: &#8211; .\/redis.conf:\/usr\/local\/etc\/redis.conf entrypoint: redis-server \/usr\/local\/etc\/redis.conf thorcast-server: build: ..\/..\/thorcast-server volumes: &#8211; ..\/..\/thorcast-server:\/app\/thorcast-server networks: &#8211; thorcast ports: &#8211; &#8220;8000:8000&#8221; env_file: &#8211; ..\/..\/thorcast-server\/env.list env.list \u6587\u4ef6\u7684\u76f8\u5173\u90e8\u5206\uff1a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[],"class_list":["post-42177","post","type-post","status-publish","format-standard","hentry","category-docker"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/42177","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=42177"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/42177\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=42177"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=42177"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=42177"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}