使用Redis作为Docker容器之间的链接

I have a docker-compose file with several containers in, two of which are supposed to communicate via a Redis DB. Both containers have connections to Reids and I can read/write from both. However I'd like a container to be triggered every time the something is added from the other container. I thought I could accomplish this via Redis Sub/Pub but it when I run the code it never triggers anything, even when I can see I've added new items to the Redis queue.

From this I have two questions: 1. Is what I'm looking to do even possible? Can I do publish/subscribe in in two separate docker containers and expect it work as described above? 2. If it is possible, can someone please point me below where I"m going wrong with this tools?

This is my function that I add new data to the Redis queue and then publish the data in Docker container 1.

func redisShare(key string, value string) {
jobsQueue.Set(key, value, 0) //setting in the queue
jobsQueue.Publish(key, value) //publishing for the other docker container to notice
fmt.Println("added ", key, "with a value of ", value, "to the redis queue")
}

I'm using this line in my other docker container to subscribe to the Redis queue and listen for changes: redisdb.Subscribe()

I would expect if something was added to the redis queue it would share the data to the other container and I'd see the message received, but right now Docker Container 2 just runs and then closes.

Thanks!

As from docs

receiver.go

package main

import (
    "fmt"

    "github.com/go-redis/redis"
)

func main() {
    c := redis.NewClient(&redis.Options{
        Addr: ":6379",
    })

    pubsub := c.Subscribe("mychannel1")

    // Wait for confirmation that subscription is created before publishing anything.
    _, err := pubsub.Receive()
    if err != nil {
        panic(err)
    }

    // Go channel which receives messages.
    ch := pubsub.Channel()

    // Consume messages.
    for msg := range ch {
        fmt.Println(msg.Channel, msg.Payload)
    }
}

sender.go

package main

import (
    "time"

    "github.com/go-redis/redis"
)

func main() {
    c := redis.NewClient(&redis.Options{
        Addr: ":6379",
    })

    // Publish a message.
    for range time.Tick(time.Second) {
        err := c.Publish("mychannel1", "hello").Err()
        if err != nil {
            panic(err)
        }
    }
}

Just in case anyone else wonders about the answer: I ended up using a combination of both Aleksandrs and sui's answers. In my first Docker container I published the results to a specific channel:

publishData := redisdb.Subscribe("CHANNELNAME")

And then in my second Docker container that was subscribing to the channel, thanks to sui for the assistance on this part, I subscribed to the channel and pulled the both the UID and the IP information like so:

ch := pubsub.Channel()
    for msg := range ch {
        fmt.Println(msg.Payload)
        s := strings.Split(msg.Payload, ":")
        UID, IP := s[0], s[1]
        fmt.Println(UID, IP)
    }

This is working great for me so far - thanks so much to sui and Aleksandrs for the assistance!