Service Container Networking

When you start a service container from your Wercker build, you can connect to it from the main box container, or from other containers that you have started using the container’s name and port, where the name that you use is specified using the (optional) name property of the service. If you do not set the name property, then the image name is used as default.

For example, when the name is specified, mon1 is used as the hostname for the first service, and mon2 is used as the hostname for the second service.

services:
  - name: mon1
    id: mongo:2.2.7
  - name: mon2
    id: mongo:2.2.7

When the name is not specified, mongo is used as the hostname. For example:

services:
  - id: mongo:2.2.7

When connecting to a service container, note that the port must have been exposed by that container.

Exposed environment variables

For connecting to the containers that are started using the docker-run command, see Container Networking.

Wercker creates environment variables which can be used to refer to the container and the ports exposed by it. This happens for every service listed in the services section. For each service, Docker will expose three distinct environment variables (where the container name is the name of the service container):

services:
  - name: mon1
    id: mongo:2.2.7
  - name: mon2
    id: mongo:2.2.7

For example, if you want to use Elasticsearch built from the default Elasticsearch Docker image on the Docker hub as a service in your application, the following environment variables would be injected:

ELASTICSEARCH_PORT_9200_TCP_PORT
ELASTICSEARCH_ENV_JAVA_DEBIAN_VERSION
ELASTICSEARCH_NAME
ELASTICSEARCH_PORT_9200_TCP_PROTO
ELASTICSEARCH_PORT_9300_TCP_PORT
ELASTICSEARCH_PORT_9300_TCP_ADDR
ELASTICSEARCH_ENV_CA_CERTIFICATES_JAVA_VERSION
ELASTICSEARCH_ENV_ELASTICSEARCH_VERSION
ELASTICSEARCH_ENV_JAVA_VERSION
ELASTICSEARCH_PORT
ELASTICSEARCH_PORT_9200_TCP
ELASTICSEARCH_PORT_9300_TCP
ELASTICSEARCH_PORT_9300_TCP_PROTO
ELASTICSEARCH_PORT_9200_TCP_ADDR

Getting the environment variables

If you want to view which variables are exposed in your container while it’s running, you can run the env command in a script step in your wercker.yml file:

# wercker.yml
box: tcnksm/gox:1.3.3
services:
   - elasticsearch
build:
   steps:
     - script:
         name: env
         code: env
(...)

Using the environment variables

To use these environment variables in your application you would simply query the environment for the variables you are looking for. In this example, you would want the IP and the port Elasticsearch is running on. In Go this would look something like this:

fmt.Println(“IP:", os.Getenv("ELASTICSEARCH_PORT_9300_TCP_ADDR”))
fmt.Println(“PORT:", os.Getenv("ELASTICSEARCH_PORT_9300_TCP_PORT”))

Waiting for services to come up

You can add this script step to your wercker.yml to ensure that your pipeline run will wait until the service has come up:

- script: 
name: "Wait for MariaDB connection" 
code: | 
while ! nc -q 1 $MARIADB_PORT_3306_TCP_ADDR $MARIADB_PORT_3306_TCP_PORT 
</dev/null; do sleep 3; done

Thanks to user ryanrapp for suggesting this!