你可以在一个叫做 docker-compose.yml的文件中,配置你所有的容器。我在下方将贴上我为这个服务编写的docker-compose.yml文件(完整内容),因为我觉得它真的很简洁、直接!
version: "3.3"services: db: image: postgres volumes: - ./tmp/db:/var/lib/postgresql/data environment: POSTGRES_PASSWORD: password # yes I set the password to 'password' go_server: # todo: use a smaller image at some point, we don't need all of ubuntu to run a static go binary image: ubuntu command: /app/go_proxy/server volumes: - .:/app rails_server: build: docker/rails command: bash -c "rm -f tmp/pids/server.pid && source secrets.sh && bundle exec rails s -p 3000 -b '0.0.0.0'" volumes: - .:/app web: build: docker/nginx ports: - "8777:80" # this exposes port 8777 on my laptop
这个配置包含了两种容器。对于前面两个容器,我直接使用了现有的镜像(image: postgres和image: ubuntu)。对于后面两个容器,我不得不构建一个自定义容器镜像,其中,build: docker/rails的作用就是告诉 Docker Compose,它应该使用docker/rails/Dockerfile来构建一个自定义容器。
我需要允许我的 Rails 服务访问一些 API 密钥和其他东西,因此,我使用了 source secrets.sh,它的作用就是在环境变量中预设一组密钥。
(使用 Docker Compose)搭建好这个开发环境后,如果我需要访问 Rails 控制台console(一个交互式环境,加载了所有我的 Rails 代码),我只需要运行一行代码即可:
$ docker-compose exec rails_server rails consoleRunning via Spring preloader in process 597Loading development environment (Rails 6.0.3.4)irb(main):001:0>
好耶!
当你有很多微服务的时候(还是自己搭建比较好)当你尝试从一个很大的数据库中导入数据时(就像把几百 G 的数据存到每个人的笔记本电脑里一样)当你在 Mac 电脑上运行 Docker 时。我听说 Docker 在 macOS 上比在 Linux 上要慢很多(我猜想是因为它需要做额外的虚拟化)。我没有 Mac 电脑,所以我还没有碰到这个问题。