Docker Compose – examples

In this section, we will take a look at some examples and break them to understand what we can do within the docker-compose.yml file. Remember, earlier we discussed that in the YAML file, there needs to be either an image section or a build section. Let's take a look at an example using each. Then, we will look at an example using as many of the options available for the Docker Compose YAML file as possible.

Here is a breakdown of an example docker-compose.yml file. We will break the contents into sections to help you understand each entry.

image

The image section tells Docker Compose that you are going to define the configuration of your containers and what settings each will have:

haproxy:#container name
  image: tutum/haproxy #image to use from the Docker Hub
  ports: #defining our port setup
    - "80:80" #port to map from Docker Host: to container
  links: #what containers to link to/with
    - varnish1
    - varnish2
varnish1:
  image: jacksoncage/varnish
  ports:
    - "82:80"
  links:
    - web1
    - web2
    - web3
    - web4
  environment: # you use environment to specify variable to pass to the container with values
    VARNISH_BACKEND_PORT: 80
    VARNISH_BACKEND_IP: web1
    VARNISH_BACKEND_PORT: 80
    VARNISH_BACKEND_IP: web2
    VARNISH_BACKEND_PORT: 80
    VARNISH_BACKEND_IP: web3
    VARNISH_BACKEND_PORT: 80
    VARNISH_BACKEND_IP: web4
    VARNISH_PORT: 80

varnish2:
  image: jacksoncage/varnish
  ports:
    - "81:80"
  links:
    - web1
    - web2
    - web3
    - web4
  environment:
    VARNISH_BACKEND_PORT: 80
    VARNISH_BACKEND_IP: web1
    VARNISH_BACKEND_PORT: 80
    VARNISH_BACKEND_IP: web2
    VARNISH_BACKEND_PORT: 80
    VARNISH_BACKEND_IP: web3
    VARNISH_BACKEND_PORT: 80
    VARNISH_BACKEND_IP: web4
    VARNISH_PORT: 80

web1:
   image: scottpgallagher/php5-mysql-apache2
   volumes: # you can specify volumes for the container to use. This will allow for multiple containers to share a volume
     - .:/var/www/html/ # specify the location of the volume
   links:
     - master
     - node1
     - node2
     - nfs1
     - mcrouter1
     - mcrouter2

web2:
   image: scottpgallagher/php5-mysql-apache2
   volumes:
     - .:/var/www/html/
   links:
     - master
     - node1
     - node2
     - nfs1
     - mcrouter1
     - mcrouter2
web3:
   image: scottpgallagher/php5-mysql-apache2
   volumes:
     - .:/var/www/html/
   links:
     - master
     - node1
     - node2
     - nfs1
     - mcrouter1
     - mcrouter2
web4:
   image: scottpgallagher/php5-mysql-apache2
   volumes:
     - .:/var/www/html/
   links:
     - master
     - node1
     - node2
     - nfs1
     - mcrouter1
     - mcrouter2
master:
  image:
    scottpgallagher/galeramaster
  hostname: # you can specify a hostname to assign to the container
    master #hostname to use
  environment:
    MARIADB_DATABASE: wordpressmu
    MARIADB_USER: replica
    MARIADB_PASSWORD: replica
node1:
  image:
    scottpgallagher/galeranode
  hostname:
    node1
  environment:
    MARIADB_DATABASE: wordpressmu
    MARIADB_USER: replica
    MARIADB_PASSWORD: replica
  links:
    - master
node2:
  image:
    scottpgallagher/galeranode
  hostname:
    node2
  environment:
    MARIADB_DATABASE: wordpressmu
    MARIADB_USER: replica
    MARIADB_PASSWORD: replica
  links:
    - master
nfs1:
  image: cpuguy83/nfs-server
  volumes:
   - /var/www/wp-content/uploads
mcrouter1:
  image: jmck/mcrouter-docker
  command: mcrouter --config-str='{"pools":{"A":{"servers":["memcached1:11211", "memcached2:11211"]}},"route":"PoolRoute|A"}' -p 5000 # here you can specify a command to run on the container when it's started
  links:
   - memcached1
   - memcached2
mcrouter2:
  image: jmck/mcrouter-docker
  command: mcrouter --config-str='{"pools":{"A":{"servers":["memcached1:11211", "memcached2:11211"]}},"route":"PoolRoute|A"}' -p 5000
  links:
   - memcached1
   - memcached2
memcached1:
  image: memcached
  links:
   - db0
memcached1:
  image: memcached
  links:
   - db0
memcached2:
  image: memcached
  links:
   - db0

In this very long example, you can see that we are specifying a name for each service as well as the image that is going to be used from the Docker Hub Registry. You can also see a lot of container linking being done in it. Remember that container linking removes the exposition off ports and keeps the communication secure between the said linked containers. We are specifying volumes as well as running some commands in the containers as well.

build

The easiest example of something that uses build is a wordpress instance:

web:
  build: .
  command: php -S 0.0.0.0:8080 -t /wordpress
  ports:
    - "80:8080"
  links:
    - database
  volumes:
    - .:/wordpress
database:
  image: mysql
  environment:
    MYSQL_DATABASE: wordpress
    MYSQL_ROOT_PASSWORD: password

Now, there are other files that are required for this setup; but we are just focusing on the docker-compose.yml file right now. In the earlier example, we are specifying two services: a web service and a database service. In the database service, we see that we are using the image option; but in the web service, we are doing something different. We are building based off the contents of the folder and then placing the files in the /wordpress directory inside the container.

The last example

Following is an example just for the sake of it. It's probably something that would not actually run, but you could use it for reference for the different options that you can set within your docker-compose.yml file:

node2:
  image:
    scottpgallagher/galeranode
  hostname:
    database
  environment:
    MARIADB_DATABASE: wordpressmu
    MARIADB_USER: replica
    MARIADB_PASSWORD: replica
nfs1:
  image: scottpgallagher/php5-mysql-apache2
  ports:
    - "2049"
  volumes:
    - .:/var/www/html/
web1:
  image: apache
  links:
    - node2
    - nfs1
  volumes_from:
    - nfs1
  expose:
    - "80"
  log_driver: "syslog"
  dns: 8.8.8.8
  restart: always
  hostname: webserver
  read_only: true

In the previous example, we specified a lot of things:

  • image: This specifies what image to use from Docker Hub
  • volumes: This specifies what paths to use for the volumes that live outside the container
  • volumes-from: This specifies what volume from another container to mount into the container
  • links: This links containers together, so the need to expose ports isn't there
  • log_driver: This selects what logging driver to use
  • dns: This specifies the ability to add additional DNS servers per container
  • restart: This states that the container needs to restart when or if it fails
  • hostname: This sets a hostname for the container
  • read_only: This allows you to specify that a container is read-only
  • ports: This specifies what ports can be attached to (from the Docker host to the Docker container)
  • expose: This specifies what ports are actually exposed externally
  • environment: This sets the values to the specified variables
The last example
The last example
The last example
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset