Docker:如何使用Redis Docker镜像

 首先,从库中取下Redis。

$ docker pull redis

 接着,启动redis server,启动时设置端口映射,使其可以被外部访问。

$ docker run --name test-redis -p 6379:6379 -d redis
775e30ad251042fab3b397ff68a603796b50ccf3fe592f0256c78ae38919c218

 然后,启动redis client,将IP设置为127.0.0.1,网络设置为host,此时容器继承了宿主机的IP地址。命令执行完成后,就可以执行redis命令了。

$ docker run --name test-redis-cli -it --network host --rm redis redis-cli  -h 127.0.0.1
127.0.0.1:6379>

或者,先查看test-redis在容器上的IP。

$ docker network inspect bridge
[
    {
        "Name": "bridge",
        "Id": "fef17235611317db053a20bd182fc7c690b2751a048c2b0f989b509ba94aa4bd",
        "Created": "2019-04-24T06:35:15.645286472Z",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": null,
            "Config": [
                {
                    "Subnet": "172.17.0.0/16",
                    "Gateway": "172.17.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "1c68074a453f71d2fbb0b641272f2e16298a704250aeb02bfbb5d685fd9ff17f": {
                "Name": "test-redis",
                "EndpointID": "b53ffa56e8111152c19d36cbdbec805697d66eac11c02d0411bb22770c5e7295",
                "MacAddress": "02:42:ac:11:00:02",
                "IPv4Address": "172.17.0.2/16",
                "IPv6Address": ""
            }
        },
        "Options": {
            "com.docker.network.bridge.default_bridge": "true",
            "com.docker.network.bridge.enable_icc": "true",
            "com.docker.network.bridge.enable_ip_masquerade": "true",
            "com.docker.network.bridge.host_binding_ipv4": "0.0.0.0",
            "com.docker.network.bridge.name": "docker0",
            "com.docker.network.driver.mtu": "1500"
        },
        "Labels": {}
    }
]

 再将网络设置为bridge(bridge为默认值,可以不设置),IP设置为172.17.0.2。

$ docker run --name test-redis-cli --network bridge -it --rm redis redis-cli -h 172.17.0.2
172.17.0.2:6379>

最后,可以查看到此时有两个客户实例在执行。

$ docker ps -a
CONTAINER ID   IMAGE COMMAND                    CREATED         STATUS            PORTS                             NAMES
f6ba17249d4a      redis    "docker-entrypoint.s…"   5 seconds ago   Up 5 seconds                                            test-redis-cli
775e30ad2510     redis    "docker-entrypoint.s…"   2 minutes ago    Up 2 minutes     0.0.0.0:6379->6379/tcp   test-redis

 

参考文档

redis Docker Official Images
Networking with standalone containers
virtualbox桥接网卡设置
桥接、NAT、Host-only上网方式的区别
VMware三种模式(桥接/NAT/Host-Only)区别及上网

 

 

你可能感兴趣的:(docker,redis,Docker)