github.com/slava-ustovytski/docker@v1.8.2-rc1/docs/examples/running_redis_service.md (about)

     1  <!--[metadata]>
     2  +++
     3  title = "Dockerizing a Redis service"
     4  description = "Installing and running an redis service"
     5  keywords = ["docker, example, package installation, networking,  redis"]
     6  [menu.main]
     7  parent = "smn_applied"
     8  +++
     9  <![end-metadata]-->
    10  
    11  # Dockerizing a Redis service
    12  
    13  Very simple, no frills, Redis service attached to a web application
    14  using a link.
    15  
    16  ## Create a Docker container for Redis
    17  
    18  Firstly, we create a `Dockerfile` for our new Redis
    19  image.
    20  
    21      FROM        ubuntu:14.04
    22      RUN         apt-get update && apt-get install -y redis-server
    23      EXPOSE      6379
    24      ENTRYPOINT  ["/usr/bin/redis-server"]
    25  
    26  Next we build an image from our `Dockerfile`.
    27  Replace `<your username>` with your own user name.
    28  
    29      $ docker build -t <your username>/redis .
    30  
    31  ## Run the service
    32  
    33  Use the image we've just created and name your container `redis`.
    34  
    35  Running the service with `-d` runs the container in detached mode, leaving
    36  the container running in the background.
    37  
    38  Importantly, we're not exposing any ports on our container. Instead
    39  we're going to use a container link to provide access to our Redis
    40  database.
    41  
    42      $ docker run --name redis -d <your username>/redis
    43  
    44  ## Create your web application container
    45  
    46  Next we can create a container for our application. We're going to use
    47  the `-link` flag to create a link to the `redis` container we've just
    48  created with an alias of `db`. This will create a secure tunnel to the
    49  `redis` container and expose the Redis instance running inside that
    50  container to only this container.
    51  
    52      $ docker run --link redis:db -i -t ubuntu:14.04 /bin/bash
    53  
    54  Once inside our freshly created container we need to install Redis to
    55  get the `redis-cli` binary to test our connection.
    56  
    57      $ sudo apt-get update
    58      $ sudo apt-get install redis-server
    59      $ sudo service redis-server stop
    60  
    61  As we've used the `--link redis:db` option, Docker
    62  has created some environment variables in our web application container.
    63  
    64      $ env | grep DB_
    65  
    66      # Should return something similar to this with your values
    67      DB_NAME=/violet_wolf/db
    68      DB_PORT_6379_TCP_PORT=6379
    69      DB_PORT=tcp://172.17.0.33:6379
    70      DB_PORT_6379_TCP=tcp://172.17.0.33:6379
    71      DB_PORT_6379_TCP_ADDR=172.17.0.33
    72      DB_PORT_6379_TCP_PROTO=tcp
    73  
    74  We can see that we've got a small list of environment variables prefixed
    75  with `DB`. The `DB` comes from the link alias specified when we launched
    76  the container. Let's use the `DB_PORT_6379_TCP_ADDR` variable to connect to
    77  our Redis container.
    78  
    79      $ redis-cli -h $DB_PORT_6379_TCP_ADDR
    80      $ redis 172.17.0.33:6379>
    81      $ redis 172.17.0.33:6379> set docker awesome
    82      OK
    83      $ redis 172.17.0.33:6379> get docker
    84      "awesome"
    85      $ redis 172.17.0.33:6379> exit
    86  
    87  We could easily use this or other environment variables in our web
    88  application to make a connection to our `redis`
    89  container.