简体   繁体   English

使用docker-compose为AWS CodeBuild创建自定义构建映像

[英]Creating custom build image for AWS CodeBuild with docker-compose

I'm trying to create a custom docker image in order to use it as build image with AWS CodeBuild. 我正在尝试创建一个自定义Docker映像,以将其用作AWS CodeBuild的构建映像。 It works fine if I just do docker build against Dockerfile with set up environment. 如果我只是使用设置环境对Dockerfile进行docker docker build ,则效果很好。 But now I need to add a postgres instance to run the tests against. 但是现在我需要添加一个postgres实例来运行测试。 So I thought using docker-compose would do the trick. 所以我认为使用docker-compose可以解决问题。 However I'm failing to figure out how to make it work. 但是,我没有弄清楚如何使其工作。 It seems like the static part of the composition (the image from Dockerfile) just stops right away when I try docker-compose up , since there is no entry point. 当我尝试docker docker-compose up ,似乎组成的静态部分(来自Dockerfile的图像)立即停止,因为没有入口点。 At this point I can connect to db instance by running docker-compose run db psql -h db -U testdb -d testdb . 此时,我可以通过运行docker-compose run db psql -h db -U testdb -d testdb连接到数据库实例。 But when I build and feed it to the script provided by AWS, it runs fine until my tests try to reach the DB-server. 但是,当我将其构建并提供给AWS提供的脚本时,它可以正常运行,直到我的测试尝试到达数据库服务器为止。 This is where it fail with timeout, as if there was no db instance. 这是超时失败的地方,就像没有数据库实例一样。

Configs look like this: 配置看起来像这样:

version: '3.7'

services:
  api-build:
    tty: true
    build: ./api_build
    image: api-build
    depends_on:
      - db

  db:
    image: postgres:10-alpine
    restart: always
    environment:
      POSTGRES_USER: testdb
      POSTGRES_PASSWORD: testdb

And Dockerfile under ./api_build : 和./api_build下的./api_build

FROM alpine:3.8

FROM ruby:2.3-alpine as rb

RUN apk update && apk upgrade && \
      echo @edge http://nl.alpinelinux.org/alpine/edge/community >> /etc/apk/repositories && \
      echo @edge http://nl.alpinelinux.org/alpine/edge/main >> /etc/apk/repositories

RUN apk add --no-cache \
      alpine-sdk \
      tzdata \
      libxml2-dev \
      libxslt-dev \
      libpq \
      postgresql-dev \
      elixir \
      erlang

UPDATE: I just realized that docker-compose build just builds parts of composition if it's needed (eg Docker file updated), so does that mean there's no way to create an image using docker compose? 更新:我刚刚意识到,如果需要, docker-compose build只会构建部分组成部分(例如,更新Docker文件),这是否意味着没有办法使用docker compose创建映像? Or am I doing something very wrong? 还是我做错了什么?

Since there are no answers I'll try to answer it myself. 由于没有答案,因此我将尝试自己回答。 I'm not sure if it's gonna be useful, but I found out that I had some misconceptions concerning Docker, which prevented me from seeing a solution or the lack of. 我不确定它是否会有用,但是我发现我对Docker有一些误解,这使我看不到解决方案或缺乏解决方案。

1) What I didn't realize is that docker-compose is used for orchestration of container compositions, it cannot be built into a single image that contains all services that you need. 1)我没有意识到的是docker-compose用于编排容器组合,它无法构建到包含所需所有服务的单个映像中。

2) Multi-stage builds sounded exciting and a bit magical until I figured out that every next stage starts image from scratch. 2)多阶段构建听起来令人兴奋并且有些神奇,直到我发现每个下一阶段都从头开始创建图像。 The only thing you can do is copy some files from previous stages (if aliased with AS ). 您唯一可以做的就是复制以前阶段的某些文件(如果使用AS别名)。 It's still cool, but copying manually an installation with hundreds of files might (and will) become a nightmare. 它仍然很酷,但是手动复制包含数百个文件的安装可能(并且将会)成为噩梦。

3) Docker is designed to have only one process running inside of the container, but it doesn't mean it can't run multiple processes. 3)Docker被设计为仅在容器内部运行一个进程,但这并不意味着它不能运行多个进程。 So the solution for my problem was using a supervisor. 因此,解决我的问题的方法是使用主管。 S6 in particular, which is said to be lightweight, which is exactly what I needed with tiny Alpine images. 特别是S6,据说重量很轻,这正是我使用微型Alpine图像所需要的。

I ended up deploying s6-overlay from just-containers : 我最终从just-containers部署了s6-overlay

RUN curl -L -s https://github.com/just-containers/s6-overlay/releases/download/v1.21.4.0/s6-overlay-amd64.tar.gz \
      | tar xvzf - -C /

ENTRYPOINT [ "/init" ]

It provides /etc/services.d directory where service scripts go. 它提供服务脚本所在的/etc/services.d目录。 For example for postgresql, the minimal example would be (in /etc/services.d/postgres/run): 例如对于postgresql,最小的示例是(在/etc/services.d/postgres/run中):

#!/usr/bin/execlineb -P
s6-setuidgid postgres
postgres -D /usr/local/pgsql/data

Pretty much that's it. 就是这样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM