github.com/feiyang21687/docker@v1.5.0/docs/sources/examples/running_redis_service.md (about)

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