|
...
|
...
|
@@ -9,12 +9,14 @@ Linking Redis
|
|
9
|
9
|
|
|
10
|
10
|
.. include:: example_header.inc
|
|
11
|
11
|
|
|
12
|
|
-Building a redis container to link as a child of our web application.
|
|
|
12
|
+Building a Redis container to link as a child of our web application.
|
|
13
|
13
|
|
|
14
|
|
-Building the redis container
|
|
|
14
|
+Building the Redis container
|
|
15
|
15
|
----------------------------
|
|
16
|
16
|
|
|
17
|
|
-Lets build a redis image with the following Dockerfile.
|
|
|
17
|
+Lets build a Redis image with the following Dockerfile.
|
|
|
18
|
+
|
|
|
19
|
+First checkout the Redis source code.
|
|
18
|
20
|
|
|
19
|
21
|
.. code-block:: bash
|
|
20
|
22
|
|
|
...
|
...
|
@@ -22,7 +24,10 @@ Lets build a redis image with the following Dockerfile.
|
|
22
|
22
|
cd redis
|
|
23
|
23
|
git checkout 2.6
|
|
24
|
24
|
|
|
25
|
|
- # Save this Dockerfile to the root of the redis repository.
|
|
|
25
|
+
|
|
|
26
|
+Now let's create a Dockerfile in the root of the Redis repository.
|
|
|
27
|
+
|
|
|
28
|
+.. code-block:: bash
|
|
26
|
29
|
|
|
27
|
30
|
# Build redis from source
|
|
28
|
31
|
# Make sure you have the redis source code checked out in
|
|
...
|
...
|
@@ -51,37 +56,37 @@ Lets build a redis image with the following Dockerfile.
|
|
51
|
51
|
|
|
52
|
52
|
|
|
53
|
53
|
We need to ``EXPOSE`` the default port of 6379 so that our link knows what ports
|
|
54
|
|
-to connect to our redis container on. If you do not expose any ports for the
|
|
|
54
|
+to connect to our Redis container on. If you do not expose any ports for the
|
|
55
|
55
|
image then docker will not be able to establish the link between containers.
|
|
56
|
56
|
|
|
57
|
57
|
|
|
58
|
|
-Run the redis container
|
|
|
58
|
+Run the Redis container
|
|
59
|
59
|
-----------------------
|
|
60
|
60
|
|
|
61
|
61
|
.. code-block:: bash
|
|
62
|
|
-
|
|
63
|
|
- docker run -d -e PASSWORD=docker -name redis redis-2.6 --requirepass docker
|
|
64
|
|
-
|
|
65
|
|
-This will run our redis container with the password docker
|
|
|
62
|
+
|
|
|
63
|
+ sudo docker run -d -e PASSWORD=docker -name redis redis-2.6 --requirepass docker
|
|
|
64
|
+
|
|
|
65
|
+This will run our Redis container with the password docker
|
|
66
|
66
|
to secure our service. By specifying the ``-name`` flag on run
|
|
67
|
|
-we will assign the name ``redis`` to this container. If we do not specify a name for
|
|
|
67
|
+we will assign the name ``redis`` to this container. If we do not specify a name for
|
|
68
|
68
|
our container via the ``-name`` flag docker will automatically generate a name for us.
|
|
69
|
69
|
We can issue all the commands that you would expect; start, stop, attach, using the name for our container.
|
|
70
|
70
|
The name also allows us to link other containers into this one.
|
|
71
|
71
|
|
|
72
|
|
-Linking redis as a child
|
|
|
72
|
+Linking Redis as a child
|
|
73
|
73
|
------------------------
|
|
74
|
74
|
|
|
75
|
|
-Next we can start a new web application that has a dependency on redis and apply a link
|
|
76
|
|
-to connect both containers. If you noticed when running our redis server we did not use
|
|
77
|
|
-the ``-p`` flag to publish the redis port to the host system. Redis exposed port 6379 via the Dockerfile
|
|
|
75
|
+Next we can start a new web application that has a dependency on Redis and apply a link
|
|
|
76
|
+to connect both containers. If you noticed when running our Redis server we did not use
|
|
|
77
|
+the ``-p`` flag to publish the Redis port to the host system. Redis exposed port 6379 via the Dockerfile
|
|
78
|
78
|
and this is all we need to establish a link.
|
|
79
|
79
|
|
|
80
|
|
-Now lets start our web application with a link into redis.
|
|
|
80
|
+Now let's start our web application with a link into Redis.
|
|
81
|
81
|
|
|
82
|
82
|
.. code-block:: bash
|
|
83
|
|
-
|
|
84
|
|
- docker run -t -i -link redis:db -name webapp ubuntu bash
|
|
|
83
|
+
|
|
|
84
|
+ sudo docker run -t -i -link redis:db -name webapp ubuntu bash
|
|
85
|
85
|
|
|
86
|
86
|
root@4c01db0b339c:/# env
|
|
87
|
87
|
|
|
...
|
...
|
@@ -105,7 +110,7 @@ Now lets start our web application with a link into redis.
|
|
105
|
105
|
|
|
106
|
106
|
When we inspect the environment of the linked container we can see a few extra environment
|
|
107
|
107
|
variables have been added. When you specified ``-link redis:db`` you are telling docker
|
|
108
|
|
-to link the container named ``redis`` into this new container with the alias ``db``.
|
|
|
108
|
+to link the container named ``redis`` into this new container with the alias ``db``.
|
|
109
|
109
|
Environment variables are prefixed with the alias so that the parent container can access
|
|
110
|
110
|
network and environment information from the containers that are linked into it.
|
|
111
|
111
|
|
|
...
|
...
|
@@ -128,5 +133,5 @@ network and environment information from the containers that are linked into it.
|
|
128
|
128
|
|
|
129
|
129
|
|
|
130
|
130
|
Accessing the network information along with the environment of the child container allows
|
|
131
|
|
-us to easily connect to the redis service on the specific ip and port and use the password
|
|
|
131
|
+us to easily connect to the Redis service on the specific IP and port and use the password
|
|
132
|
132
|
specified in the environment.
|