Docker containers on travis-ci

时间:2015-09-01 22:14:21

标签: ruby rspec docker travis-ci

I have a Dockerfile that I am attempting to test using RSpec, serverspec and docker-api. Locally (using boot2docker as I am on OS X) this works great and all my test pass, but on travis-ci none of the tests pass. My .travis.yml file is as such:

language: ruby
rvm:
  - "2.2.0"

sudo: required
cache: bundler

services:
  - docker

before_install:
  - docker build -t tomasbasham/nginx .
  - docker run -d -p 80:80 -p 443:443 --name nginx -v $(pwd)/nginx.conf:/etc/nginx/nginx.conf tomasbasham/nginx

script: bundle exec rspec

Am I doing something noticeably wrong here? The repository I have created and is run on travis-ci is on GitHub. There may be something else amiss that I am unaware of

1 个答案:

答案 0 :(得分:1)

TL;博士

容器必须在前台运行它的程序。

您的Dockerfile是个问题。从您提供的github repository开始,Dockerfile内容为:

# Dockerfile for nginx with configurable persistent volumes

# Select nginx as the base image
FROM nginx
MAINTAINER Tomas Basham <me@tomasbasham.co.uk>

# Install net-tools
RUN apt-get update -q && \
    apt-get install -qy net-tools && \
    apt-get clean

# Mount configurable persistent volumes
VOLUME ["/etc/nginx/sites-enabled", "/etc/nginx/certs", "/etc/nginx/conf.d", "/var/log/nginx", "/var/www/html"]

# Expose both HTTP and HTTPS ports
EXPOSE 80 443

# ENTRYPOINT
ENTRYPOINT ["service", "nginx", "start"]

在调试RSpec / serverspec测试之前,应确保docker镜像能够运行容器。

从名为Dockerfile的目录中键入这些命令:

docker build -t tmp .
docker run --rm -it tmp

如果您收到shell提示,则表示您的容器已停止运行。如果您的容器没有启动,那么您的测试套件将失败。

dockerfile

有什么问题

您定义的入口点ENTRYPOINT ["service", "nginx", "start"]将执行一个命令,该命令将在后台中启动 nginx 程序。这意味着最初由docker(/bin/service)运行的进程将终止,docker将检测到并停止您的容器。

要在前景中运行nginx,必须运行nginx -g daemon off;,因为您可以在Dockerfile for the official nginx image中找到。但是,由于您将daemon off;放在 nginx.conf 文件中,因此您应该只使用nginx

我建议您从Dockerfile 中删除入口点(并从nginx配置中删除daemon off;,它应该可以正常工作。

serverspec

一旦你得到一个运行的容器,你将不得不专注于我没有经验的serverspec部分。