replace use of docker links with docker networks

Docker links are marked as deprecated and has been superseded by
Docker networks.
This commit is contained in:
Sameer Naik 2016-12-09 18:25:47 +05:30
parent d9f39a95ec
commit 8de5e01548
1 changed files with 40 additions and 34 deletions

View File

@ -72,68 +72,74 @@ services:
- /path/to/redis-persistence:/bitnami/redis
```
# Linking
# Connecting to other containers
If you want to connect to your Redis server inside another container, you can use the linking system provided by Docker.
Using [Docker container networking](https://docs.docker.com/engine/userguide/networking/), a Redis server running inside a container can easily be accessed by your application containers.
## Connecting a Redis client container to the Redis server container
Containers attached to the same network can communicate with each other using the container name as the hostname.
### Step 1: Run the Redis image with a specific name
## Using the Command Line
The first step is to start our Redis server.
In this example, we will create a Redis client instance that will connect to the server instance that is running on the same docker network as the client.
Docker's linking system uses container ids or names to reference containers. We can explicitly specify a name for our Redis server to make it easier to connect to other containers.
### Step 1: Create a network
```bash
docker run --name redis bitnami/redis:latest
$ docker network create app-tier --driver bridge
```
### Step 2: Run Redis as a client and link to our server
### Step 2: Launch the Redis server instance
Now that we have our Redis server running, we can create another container that links to it by giving Docker the `--link` option. This option takes the id or name of the container we want to link it to as well as a hostname to use inside the container, separated by a colon. For example, to have our Redis server accessible in another container with `server` as it's hostname we would pass `--link redis:server` to the Docker run command.
The Bitnami Redis Docker Image also ships with a Redis client, but by default it will start a server. To start the client instead, we can override the default command Docker runs by stating a different command to run after the image name.
Use the `--network app-tier` argument to the `docker run` command to attach the Redis container to the `app-tier` network.
```bash
docker run --rm -it --link redis:server bitnami/redis:latest redis-cli -h server
$ docker run -d --name redis-server \
--network app-tier \
bitnami/redis:latest
```
We started the Redis client passing in the `-h` option that allows us to specify the hostname of the server, which we set to the hostname we created in the link.
### Step 3: Launch your Redis client instance
**Note!**
You can also run the Redis client in the same container the server is running in using the Docker [exec](https://docs.docker.com/reference/commandline/cli/#exec) command.
Finally we create a new container instance to launch the Redis client and connect to the server created in the previous step:
```bash
docker exec -it redis redis-cli
$ docker run -it --rm \
--network app-tier \
bitnami/redis:latest redis-cli -h redis-server
```
## Linking with Docker Compose
## Using Docker Compose
### Step 1: Add a Redis entry in your `docker-compose.yml`
Copy the snippet below into your `docker-compose.yml` to add Redis to your application.
```yaml
services:
redis:
image: 'bitnami/redis:latest'
```
### Step 2: Link it to another container in your application
Update the definitions for containers you want to access your Redis server from to include a link to the `redis` entry you added in Step 1.
When not specified, Docker Compose automatically sets up a new network and attaches all deployed services to that network. However, we will explicitly define a new `bridge` network named `app-tier`. In this example we assume that you want to connect to the Redis server from your own custom application image which is identified in the following snippet by the service name `myapp`.
```yaml
version: '2'
networks:
app-tier:
driver: bridge
services:
redis:
image: 'bitnami/redis:latest'
networks:
- app-tier
myapp:
image: myapp
depends_on:
- redis
image: 'YOUR_APPLICATION_IMAGE'
networks:
- app-tier
```
Inside `myapp`, use `redis` as the hostname for the Redis server.
> **IMPORTANT**:
>
> 1. Please update the **YOUR_APPLICATION_IMAGE_** placeholder in the above snippet with your application image
> 2. In your application container, use the hostname `redis` to connect to the Redis server
Launch the containers using:
```bash
$ docker-compose up -d
```
# Configuration