Services
Drone supports launching detached service containers as part of your pipeline. The typical use case for services is when your unit tests require a running redis server, for example:
1 kind: pipeline
2 type: docker
3 name: default
4
5 services:
6 - name: cache
7 image: redis
Service containers are reachable at a hostname identical to the container name. In our previous example, the redis container name is cache, and can be accessed from the pipeline at tcp://cache:6379
1 kind: pipeline
2 type: docker
3 name: default
4
5 steps:
6 - name: ping
7 image: redis
8 commands:
9 - redis-cli -h cache ping
10
11 services:
12 - name: cache
13 image: redis
It is important to note the service container exit code is ignored, and a non-zero exit code does not fail the overall pipeline. Drone expects service containers to exit with a non-zero exit code, since they often need to be killed after the pipeline completes.
Detached Steps
Services can also be defined directly in the pipeline, as detached pipeline steps. This can be useful when you need direct control over when the service is started, relative to other steps in your pipeline.
1 kind: pipeline
2 name: default
3
4 steps:
5 - name: cache
6 image: redis
7 detach: true
8
9 - name: ping
10 image: redis
11 commands:
12 - redis-cli -h cache ping
Common Problems
This section highlights some common problems that users encounter when configuring services. If you continue to experience issues please also check the faq. You might also want to compare your yaml to our example service configurations.
Incorrect Hostname
It is import to remember that you cannot use the localhost
or 127.0.0.1
address to connect to services from your pipeline. Service containers are assigned their own IP address and hostname. The hostname is based on the service container name.
1 kind: pipeline
2 type: docker
3 name: default
4
5 steps:
6 - name: ping
7 image: redis
8 commands:
9 - redis-cli -h cache ping
10
11 services:
12 - name: cache
13 image: redis
Initialization
It is important to remember that after a container is started, the software running inside the container (e.g. redis) takes time to initialize and begin accepting connections.
Be sure to give the service adequate time to initialize before attempting to connect. A naive solution is to use the sleep command.
1 kind: pipeline
2 type: docker
3 name: default
4
5 steps:
6 - name: ping
7 image: redis
8 commands:
9 - sleep 5
10 - redis-cli -h cache ping
11
12 services:
13 - name: cache
14 image: redis