| 28 | 27 |
deleted file mode 100644 |
| ... | ... |
@@ -1,137 +0,0 @@ |
| 1 |
-:title: Linking to an Redis container |
|
| 2 |
-:description: Running redis linked into your web app |
|
| 3 |
-:keywords: docker, example, networking, redis, link |
|
| 4 |
- |
|
| 5 |
-.. _linking_redis: |
|
| 6 |
- |
|
| 7 |
-Linking Redis |
|
| 8 |
-============= |
|
| 9 |
- |
|
| 10 |
-.. include:: example_header.inc |
|
| 11 |
- |
|
| 12 |
-Building a Redis container to link as a child of our web application. |
|
| 13 |
- |
|
| 14 |
-Building the Redis container |
|
| 15 |
- |
|
| 16 |
-Lets build a Redis image with the following Dockerfile. |
|
| 17 |
- |
|
| 18 |
-First checkout the Redis source code. |
|
| 19 |
- |
|
| 20 |
-.. code-block:: bash |
|
| 21 |
- |
|
| 22 |
- git clone https://github.com/antirez/redis.git |
|
| 23 |
- cd redis |
|
| 24 |
- git checkout 2.6 |
|
| 25 |
- |
|
| 26 |
- |
|
| 27 |
-Now let's create a Dockerfile in the root of the Redis repository. |
|
| 28 |
- |
|
| 29 |
-.. code-block:: bash |
|
| 30 |
- |
|
| 31 |
- # Build redis from source |
|
| 32 |
- # Make sure you have the redis source code checked out in |
|
| 33 |
- # the same directory as this Dockerfile |
|
| 34 |
- FROM ubuntu |
|
| 35 |
- |
|
| 36 |
- RUN echo "deb http://archive.ubuntu.com/ubuntu precise main universe" > /etc/apt/sources.list |
|
| 37 |
- RUN apt-get update |
|
| 38 |
- RUN apt-get upgrade -y |
|
| 39 |
- |
|
| 40 |
- RUN apt-get install -y gcc make g++ build-essential libc6-dev tcl |
|
| 41 |
- |
|
| 42 |
- ADD . /redis |
|
| 43 |
- |
|
| 44 |
- RUN (cd /redis && make) |
|
| 45 |
- |
|
| 46 |
- RUN mkdir -p /redis-data |
|
| 47 |
- VOLUME ["/redis-data"] |
|
| 48 |
- EXPOSE 6379 |
|
| 49 |
- |
|
| 50 |
- ENTRYPOINT ["/redis/src/redis-server"] |
|
| 51 |
- CMD ["--dir", "/redis-data"] |
|
| 52 |
- |
|
| 53 |
- # docker build our new redis image from source |
|
| 54 |
- docker build -t redis-2.6 . |
|
| 55 |
- |
|
| 56 |
- |
|
| 57 |
-We need to ``EXPOSE`` the default port of 6379 so that our link knows what ports |
|
| 58 |
-to connect to our Redis container on. If you do not expose any ports for the |
|
| 59 |
-image then docker will not be able to establish the link between containers. |
|
| 60 |
- |
|
| 61 |
- |
|
| 62 |
-Run the Redis container |
|
| 63 |
- |
|
| 64 |
-.. code-block:: bash |
|
| 65 |
- |
|
| 66 |
- sudo docker run -d -e PASSWORD=docker -name redis redis-2.6 --requirepass docker |
|
| 67 |
- |
|
| 68 |
-This will run our Redis container with the password docker |
|
| 69 |
-to secure our service. By specifying the ``-name`` flag on run |
|
| 70 |
-we will assign the name ``redis`` to this container. If we do not specify a name for |
|
| 71 |
-our container via the ``-name`` flag docker will automatically generate a name for us. |
|
| 72 |
-We can issue all the commands that you would expect; start, stop, attach, using the name for our container. |
|
| 73 |
-The name also allows us to link other containers into this one. |
|
| 74 |
- |
|
| 75 |
-Linking Redis as a child |
|
| 76 |
- |
|
| 77 |
-Next we can start a new web application that has a dependency on Redis and apply a link |
|
| 78 |
-to connect both containers. If you noticed when running our Redis server we did not use |
|
| 79 |
-the ``-p`` flag to publish the Redis port to the host system. Redis exposed port 6379 via the Dockerfile |
|
| 80 |
-and this is all we need to establish a link. |
|
| 81 |
- |
|
| 82 |
-Now let's start our web application with a link into Redis. |
|
| 83 |
- |
|
| 84 |
-.. code-block:: bash |
|
| 85 |
- |
|
| 86 |
- sudo docker run -t -i -link redis:db -name webapp ubuntu bash |
|
| 87 |
- |
|
| 88 |
- root@4c01db0b339c:/# env |
|
| 89 |
- |
|
| 90 |
- HOSTNAME=4c01db0b339c |
|
| 91 |
- DB_NAME=/webapp/db |
|
| 92 |
- TERM=xterm |
|
| 93 |
- DB_PORT=tcp://172.17.0.8:6379 |
|
| 94 |
- DB_PORT_6379_TCP=tcp://172.17.0.8:6379 |
|
| 95 |
- DB_PORT_6379_TCP_PROTO=tcp |
|
| 96 |
- DB_PORT_6379_TCP_ADDR=172.17.0.8 |
|
| 97 |
- DB_PORT_6379_TCP_PORT=6379 |
|
| 98 |
- PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin |
|
| 99 |
- PWD=/ |
|
| 100 |
- DB_ENV_PASSWORD=docker |
|
| 101 |
- SHLVL=1 |
|
| 102 |
- HOME=/ |
|
| 103 |
- container=lxc |
|
| 104 |
- _=/usr/bin/env |
|
| 105 |
- root@4c01db0b339c:/# |
|
| 106 |
- |
|
| 107 |
- |
|
| 108 |
-When we inspect the environment of the linked container we can see a few extra environment |
|
| 109 |
-variables have been added. When you specified ``-link redis:db`` you are telling docker |
|
| 110 |
-to link the container named ``redis`` into this new container with the alias ``db``. |
|
| 111 |
-Environment variables are prefixed with the alias so that the parent container can access |
|
| 112 |
-network and environment information from the containers that are linked into it. |
|
| 113 |
- |
|
| 114 |
-.. code-block:: bash |
|
| 115 |
- |
|
| 116 |
- # The name of the child container |
|
| 117 |
- DB_NAME=/webapp/db |
|
| 118 |
- |
|
| 119 |
- # The default protocol, ip, and port of the service running in the container |
|
| 120 |
- DB_PORT=tcp://172.17.0.8:6379 |
|
| 121 |
- |
|
| 122 |
- # A specific protocol, ip, and port of various services |
|
| 123 |
- DB_PORT_6379_TCP=tcp://172.17.0.8:6379 |
|
| 124 |
- DB_PORT_6379_TCP_PROTO=tcp |
|
| 125 |
- DB_PORT_6379_TCP_ADDR=172.17.0.8 |
|
| 126 |
- DB_PORT_6379_TCP_PORT=6379 |
|
| 127 |
- |
|
| 128 |
- # Get environment variables of the container |
|
| 129 |
- DB_ENV_PASSWORD=docker |
|
| 130 |
- |
|
| 131 |
- |
|
| 132 |
-Accessing the network information along with the environment of the child container allows |
|
| 133 |
-us to easily connect to the Redis service on the specific IP and port and use the password |
|
| 134 |
-specified in the environment. |