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