Skip to content

Latest commit

 

History

History
125 lines (96 loc) · 4.93 KB

README.md

File metadata and controls

125 lines (96 loc) · 4.93 KB

Working with Related Containers

Another usage pattern for Docker is to package not only the application, but also its dependencies, in to Docker containers. In this section you'll run the Liberty MongoDB sample in one container with MongoDB running in another and then use Docker Compose to manage them both.

Resolving dependencies via linking

You'll begin by using Docker linking to allow the Liberty application to resolve the MongoDB instance.

  1. Create an image with WebSphere Liberty and the MongoDB feature as follows.

  2. Switch to the compose directory containing this README by typing:

    $ cd compose
  3. View the contents of the Dockerfile by typing:

    $ cat Dockerfile

The file adds the Liberty mongodb-2.0 feature from the Liberty online repository using the installUtility command and then copies in the server configuration. 2. View the server.xml by running:

```bash
$ cat mongoDBSample/server.xml
```

The important part to note is the stanza that specifies that the hostname should be db. 3. Build the application with the command:

```bash
$ docker build -t app .
```
  1. Pull down MongoDB from Docker Hub to your local cache using the command:

    $ docker pull mongo
  2. Now start the MongoDB server using the following command:

    $ docker run -d --name mongodb mongo
  3. Now start an instance of the app container using the following command:

    $ docker run -d --link mongodb:db -p 80:9080 --name app app

The --link option indicates that the mongodb container should be made available to this container under the alias db. 7. Use the following command to wait for the server to start:

```bash
$ docker logs --tail=all -f app
```
  1. Run the following Docker Machine command to identify the IP address allocated to the current active virtual machine:

    $ docker-machine ip $(docker-machine active)
  2. View the running application by opening a web browser at this IP address followed by /mongoDBApp. Each time the page is refreshed it writes more users in to the MongoDB base.

  3. Clean up the containers and images with the following commands:

    $ docker kill $(docker ps -q)
    $ docker rm $(docker ps -aq)
    $ docker rmi app

Note: By default, container linking only works between containers on a single host. Across multiple hosts there are a number of options:

  • Pass the address of the remote container as an environment variable to the container. The Liberty ``$(env.XXX)` syntax makes it easy to substitute variables in to server.xml.
  • Use some form of service registry to locate dependencies (e.g. etcd or consul).
  • As of Docker 1.10, use a Docker overlay network to create a software defined network spanning the multiple hosts.

Docker Compose for container orchestration

Docker Compose provides a simple file format to define the relationship between containers that make up a single application.

  1. View the docker-compose.yml in the current directory that defines the application using:

    $ cat docker-compose.yml

Note that it defines two services: web and db and links them together. 2. Start the application with the following command (as with the docker command, the -d option starts the containers in the background):

```bash
$ docker-compose up -d
```
  1. The following command will then show an aggregated view of the logs from the two services:

    $ docker-compose logs
  2. Ctrl-C to exit the logs view.

  3. You can scale up the number of containers for the web service using the command:

    $ docker-compose scale web=2
  4. Use the following command to list all of the containers that make up the application:

    $ docker-compose ps
  5. Execute the following commands to demonstrate that the two Liberty containers are using the same MongoDB instance:

    $ curl $(docker-compose port --index=1 web 9080)/mongoDBApp
    $ curl $(docker-compose port --index=2 web 9080)/mongoDBApp

You should see that the two application instances are serving data from the same MongoDB database with the second command returning the users created by the first command plus ten more.

```bash
$ docker-compose ps
```
  1. Clean up the containers with the following commands:

    $ docker-compose kill
    $ docker-compose rm -f

Note: Docker Compose can only communicate with a single Docker API endpoint and therefore, by default, is restricted to creating containers on a single node. However, when used in conjunction with Docker Swarm, which exposes a single API endpoint for managing a cluster of Docker nodes, it is able to schedule servers over multiple nodes.

Congratulations

Congratulations on completing this tutorial. You may now like to look at the tutorial on Deploying to IBM Containers on Bluemix.