[note] Using the Go language Redigo the way to connect a Redis container within a docker container

Source: Internet
Author: User

The connection between Docker containers can be a lot of convenience, and the following records how the Redis container connected to it is connected through environment variables within its own container.

Start with a Redis docker container named Redis, and then start your own Docker container, using "--link Redis:redis" to connect to the Redis container.

Within their own container, there will be a corresponding environment variable, which is used here is Redis_port = tcp://172.17.0.89:6379.

It can be split into TCP and 172.17.0.89:6379 as a parameter passed into the Redigo dial function, so you do not have to explicitly write IP and port addresses.

By the way, Redigo is a Redis client implementation of the go language.

The reference code is as follows:

Package Mainimport ("FMT"    "Github.com/garyburd/redigo/redis"    "OS"    "Strings"    " Time") func Testredis ()BOOL {    //e.g. Redis_port = tcp://172.17.0.89:6379Fmt. Println ("Redis_port", OS. Getenv ("Redis_port")) Addr:= Strings. Split (OS. Getenv ("Redis_port"),"://") conn, err:= Redis. Dialtimeout (addr[0], addr[1],0,1*time. Second,1*Time . Second)ifErr! =Nil {fmt. PRINTLN (ERR)return false} defer Conn. Close () Size, err:= Conn. Do ("dbsize")    ifErr! =Nil {fmt. PRINTLN (ERR)return false} fmt. Printf ("DB size is%d \ n", size) _, Err= Conn. Do ("SET","User:user0",123) _, Err= Conn. Do ("SET","User:user1",456) _, Err= Conn. Do ("APPEND","User:user0", the) User0, err:= Redis. INT (Conn. Do ("GET","User:user0") ) User1, err:= Redis. INT (Conn. Do ("GET","User:user1") FMT. Printf ("User0 is%d, User1 is%d \ n", User0, user1)return true}func Main () {FMT. Println (time. Now (),"[Redis Test start]")    ifTestredis () {fmt. Println ("[Redis test OK]")    } Else{fmt. Println ("[Redis Test failed]")    }}

If the connection succeeds, the result is as follows:

£ º-44.796537101 +0000  UTC [Redis Test Start]redis_port TCP://172.17.0.89:6379is2is  12387is456  [Redis test OK]

[note] Using the Go language Redigo the way to connect a Redis container within a docker container

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.