diff --git a/docs/sources/examples/index.rst b/docs/sources/examples/index.rst index 6167e35652..ea3294d2d3 100644 --- a/docs/sources/examples/index.rst +++ b/docs/sources/examples/index.rst @@ -24,4 +24,3 @@ to more substantial services like those which you might find in production. postgresql_service mongodb running_riak_service - linking_into_redis diff --git a/docs/sources/examples/linking_into_redis.rst b/docs/sources/examples/linking_into_redis.rst deleted file mode 100644 index 1b91b8451d..0000000000 --- a/docs/sources/examples/linking_into_redis.rst +++ /dev/null @@ -1,137 +0,0 @@ -:title: Linking to an Redis container -:description: Running redis linked into your web app -:keywords: docker, example, networking, redis, link - -.. _linking_redis: - -Linking Redis -============= - -.. include:: example_header.inc - -Building a Redis container to link as a child of our web application. - -Building the Redis container ----------------------------- - -Lets build a Redis image with the following Dockerfile. - -First checkout the Redis source code. - -.. code-block:: bash - - git clone https://github.com/antirez/redis.git - cd redis - git checkout 2.6 - - -Now let's create a Dockerfile in the root of the Redis repository. - -.. code-block:: bash - - # Build redis from source - # Make sure you have the redis source code checked out in - # the same directory as this Dockerfile - FROM ubuntu - - RUN echo "deb http://archive.ubuntu.com/ubuntu precise main universe" > /etc/apt/sources.list - RUN apt-get update - RUN apt-get upgrade -y - - RUN apt-get install -y gcc make g++ build-essential libc6-dev tcl - - ADD . /redis - - RUN (cd /redis && make) - - RUN mkdir -p /redis-data - VOLUME ["/redis-data"] - EXPOSE 6379 - - ENTRYPOINT ["/redis/src/redis-server"] - CMD ["--dir", "/redis-data"] - - # docker build our new redis image from source - docker build -t redis-2.6 . - - -We need to ``EXPOSE`` the default port of 6379 so that our link knows what ports -to connect to our Redis container on. If you do not expose any ports for the -image then docker will not be able to establish the link between containers. - - -Run the Redis container ------------------------ - -.. code-block:: bash - - sudo docker run -d -e PASSWORD=docker -name redis redis-2.6 --requirepass docker - -This will run our Redis container with the password docker -to secure our service. By specifying the ``-name`` flag on run -we will assign the name ``redis`` to this container. If we do not specify a name for -our container via the ``-name`` flag docker will automatically generate a name for us. -We can issue all the commands that you would expect; start, stop, attach, using the name for our container. -The name also allows us to link other containers into this one. - -Linking Redis as a child ------------------------- - -Next we can start a new web application that has a dependency on Redis and apply a link -to connect both containers. If you noticed when running our Redis server we did not use -the ``-p`` flag to publish the Redis port to the host system. Redis exposed port 6379 via the Dockerfile -and this is all we need to establish a link. - -Now let's start our web application with a link into Redis. - -.. code-block:: bash - - sudo docker run -t -i -link redis:db -name webapp ubuntu bash - - root@4c01db0b339c:/# env - - HOSTNAME=4c01db0b339c - DB_NAME=/webapp/db - TERM=xterm - DB_PORT=tcp://172.17.0.8:6379 - DB_PORT_6379_TCP=tcp://172.17.0.8:6379 - DB_PORT_6379_TCP_PROTO=tcp - DB_PORT_6379_TCP_ADDR=172.17.0.8 - DB_PORT_6379_TCP_PORT=6379 - PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin - PWD=/ - DB_ENV_PASSWORD=docker - SHLVL=1 - HOME=/ - container=lxc - _=/usr/bin/env - root@4c01db0b339c:/# - - -When we inspect the environment of the linked container we can see a few extra environment -variables have been added. When you specified ``-link redis:db`` you are telling docker -to link the container named ``redis`` into this new container with the alias ``db``. -Environment variables are prefixed with the alias so that the parent container can access -network and environment information from the containers that are linked into it. - -.. code-block:: bash - - # The name of the child container - DB_NAME=/webapp/db - - # The default protocol, ip, and port of the service running in the container - DB_PORT=tcp://172.17.0.8:6379 - - # A specific protocol, ip, and port of various services - DB_PORT_6379_TCP=tcp://172.17.0.8:6379 - DB_PORT_6379_TCP_PROTO=tcp - DB_PORT_6379_TCP_ADDR=172.17.0.8 - DB_PORT_6379_TCP_PORT=6379 - - # Get environment variables of the container - DB_ENV_PASSWORD=docker - - -Accessing the network information along with the environment of the child container allows -us to easily connect to the Redis service on the specific IP and port and use the password -specified in the environment. diff --git a/docs/sources/examples/running_redis_service.rst b/docs/sources/examples/running_redis_service.rst index ac869501b0..edcf73e657 100644 --- a/docs/sources/examples/running_redis_service.rst +++ b/docs/sources/examples/running_redis_service.rst @@ -9,74 +9,93 @@ Redis Service .. include:: example_header.inc -Very simple, no frills, Redis service. +Very simple, no frills, Redis service attached to a web application using a link. -Open a docker container ------------------------ +Create a docker container for Redis +----------------------------------- + +Firstly, we create a ``Dockerfile`` for our new Redis image. .. code-block:: bash - sudo docker run -i -t ubuntu /bin/bash + FROM ubuntu:12.10 + RUN apt-get update + RUN apt-get -y install redis-server + EXPOSE 6379 + ENTRYPOINT ["/usr/bin/redis-server"] -Building your image -------------------- - -Update your Docker container, install the Redis server. Once -installed, exit out of the Docker container. +Next we build an image from our ``Dockerfile``. Replace ```` +with your own user name. .. code-block:: bash - apt-get update - apt-get install redis-server - exit - -Snapshot the installation -------------------------- - -.. code-block:: bash - - sudo docker ps -a # grab the container id (this will be the first one in the list) - sudo docker commit /redis + sudo docker build -t /redis Run the service --------------- +Use the image we've just created and name your container ``redis``. + Running the service with ``-d`` runs the container in detached mode, leaving the -container running in the background. Use your snapshot. +container running in the background. + +Importantly, we're not exposing any ports on our container. Instead we're going to +use a container link to provide access to our Redis database. .. code-block:: bash - sudo docker run -d -p 6379 /redis /usr/bin/redis-server + sudo docker run -name redis -d /redis -Test 1 -++++++ +Create your web application container +------------------------------------- -Connect to the container with the ``redis-cli`` binary. +Next we can create a container for our application. We're going to use the ``-link`` +flag to create a link to the ``redis`` container we've just created with an alias of +``db``. This will create a secure tunnel to the ``redis`` container and expose the +Redis instance running inside that container to only this container. .. code-block:: bash - sudo docker ps # grab the new container id - sudo docker inspect # grab the ipaddress of the container - redis-cli -h -p 6379 - redis 10.0.3.32:6379> set docker awesome + sudo docker run -link redis:db -i -t ubuntu:12.10 /bin/bash + +Once inside our freshly created container we need to install Redis to get the +``redis-cli`` binary to test our connection. + +.. code-block:: bash + + apt-get update + apt-get -y install redis-server + service redis-server stop + +Now we can test the connection. Firstly, let's look at the available environmental +variables in our web application container. We can use these to get the IP and port +of our ``redis`` container. + +.. code-block:: bash + + env + . . . + DB_NAME=/violet_wolf/db + DB_PORT_6379_TCP_PORT=6379 + DB_PORT=tcp://172.17.0.33:6379 + DB_PORT_6379_TCP=tcp://172.17.0.33:6379 + DB_PORT_6379_TCP_ADDR=172.17.0.33 + DB_PORT_6379_TCP_PROTO=tcp + +We can see that we've got a small list of environmental varaibles prefixed with ``DB``. +The ``DB`` comes from the link alias specified when we launched the container. Let's use +the ``DB_PORT_6379_TCP_ADDR`` variable to connect to our Redis container. + +.. code-block:: bash + + redis-cli -h $DB_PORT_6379_TCP_ADDR + redis 172.17.0.33:6379> + redis 172.17.0.33:6379> set docker awesome OK - redis 10.0.3.32:6379> get docker + redis 172.17.0.33:6379> get docker "awesome" - redis 10.0.3.32:6379> exit + redis 172.17.0.33:6379> exit -Test 2 -++++++ +We could easily use this or other environment variables in our web application to make a +connection to our ``redis`` container. -Connect to the host os with the ``redis-cli`` binary. - -.. code-block:: bash - - sudo docker ps # grab the new container id - sudo docker port 6379 # grab the external port - ip addr show # grab the host ip address - redis-cli -h -p - redis 192.168.0.1:49153> set docker awesome - OK - redis 192.168.0.1:49153> get docker - "awesome" - redis 192.168.0.1:49153> exit