简体   繁体   English

设置简单的docker-compose配置进行测试的正确方法是什么?

[英]What is the proper way to setup a simple docker-compose configuration for testing?

My current docker-compose.yml file: 我当前docker-compose.yml文件:

version: '2'
services:
  app:
    restart: always
    build: ./web
    ports:
      - "8000:8000"
    volumes:
      - ./web:/app/web
    command: /usr/local/bin/gunicorn -w 3 -b :8000 project:create_app()
    environment:
      FLASK_APP: project/__init__.py
    depends_on:
      - db
    working_dir: /app/web

  db:
    image: postgres:9.6-alpine
    restart: always
    volumes:
      - dbvolume:/var/lib/postgresql/data
    environment:
      POSTGRES_DB: app
      POSTGRES_USER: app
      POSTGRES_PASSWORD: app

volumes:
  dbvolume:

I'm now trying to create a docker-compose-test.yml file that overrides the previous file for testing. 我现在正在尝试创建一个docker-compose-test.yml文件,该文件将覆盖先前的文件进行测试。 What came to my mind was to use this: 我想到的是使用这个:

version: '2'
services:
  app:
    command: pytest

  db:
    volumes:
      - dbtestvolume:/var/lib/postgresql/data

volumes:
  dbtestvolume:

And then run the tests with the command: 然后使用以下命令运行测试:

docker-compose -f docker-compose.yml -f docker-compose-test.yml run --rm app

that as far as I understand should override only the different aspects compared to the docker-file used for development, that is the command used and the data volume where the data is stored. 据我了解,与用于开发的docker-file相比,它仅应覆盖不同的方面,即使用的命令和存储数据的数据量。

The command is successfully overridden, while unfortunately the data volume stays the same and so the data of my application get overwritten if I run my tests. 该命令已成功被覆盖,但不幸的是数据量保持不变,因此如果运行测试,应用程序的数据将被覆盖。

Is this the correct way to set up a docker configuration for the tests? 这是为测试设置docker配置的正确方法吗? Any suggestion about what is going wrong? 关于出什么问题有什么建议吗?

If this is not the correct way, what is the proper way to setup a docker-compose configuration for testing? 如果这不是正确的方法,那么设置用于测试的docker-compose配置的正确方法是什么?

Alternative test 替代测试

I tried to change my docker-compose-test.yml file to use a different service ( db-test ) for testing: 我试图将docker-compose-test.yml文件更改为使用其他服务( db-test )进行测试:

version: '2'
services:
  app:
    command: pytest
    depends_on:
      - db-test

  db-test:
    image: postgres:9.6-alpine
    restart: always
    environment:
      POSTGRES_DB: app
      POSTGRES_USER: app
      POSTGRES_PASSWORD: app

What happens now is that I have data is not overwritten (so, in a way, it works, hurray!) when a run my tests, but if I try to run the command: 现在发生的是,在运行测试时,我的数据没有被覆盖(因此,以某种方式,它可以工作,欢呼!),但是如果我尝试运行以下命令,则:

docker-compose down

I get this ouput: 我得到这个输出:

Stopping app_app_1 ... done
Stopping app_db_1  ... done
Found orphan containers (app_db-test_1) for this project. If you removed or renamed this service in your compose file, you can run this command with the --remove-orphans flag to clean it up.

and then the docker-compose down fails. 然后docker-compose down失败。 So something is not configured properly. 因此某些东西配置不正确。

Any idea? 任何想法?

If you don't want to persist the DB data, don't use volumes, so you will have a fresh database everytime you start the container. 如果您不想持久化数据库数据,请不要使用卷,因此每次启动容器时都会有一个新的数据库。

I guess you need some prepopulated data in your tables, so just build a new DB image copying the data you need. 我猜想您的表中需要一些预填充的数据,因此只需构建一个新的DB映像来复制所需的数据即可。 The Docker file could be something like: Docker文件可能类似于:

FROM postgres:9.6-alpine

COPY db-data/ /var/lib/postgresql/data

In case you need to update the data, mount the db-data/ using -v , change it and rebuild the image. 如果您需要更新数据,请使用-v挂载db-data/ ,对其进行更改并重建映像。

BTW, it would be better to use an automated pipeline to test your builds, using Jenkins, GitLab CI, Travis or whatever solution that suits you. 顺便说一句,最好使用自动管道,使用Jenkins,GitLab CI,Travis或任何适合您的解决方案来测试您的构建。 Anyway, you can use docker-compose in your pipeline as well to keep it consistent with your local development environment. 无论如何,您也可以在管道中使用docker-compose,使其与本地开发环境保持一致。

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

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