directory search
Compose About versions and upgrading (Compose) ASP.NET Core + SQL Server on Linux (Compose) CLI environment variables (Compose) Command-line completion (Compose) Compose(组成) Compose command-line reference(组合命令行参考) Control startup order (Compose) Django and PostgreSQL (Compose) Docker stacks and distributed application bundles (Compose) docker-compose build(docker-compose构建) docker-compose bundle docker-compose config docker-compose create docker-compose down docker-compose events docker-compose exec docker-compose help docker-compose images docker-compose kill docker-compose logs docker-compose pause docker-compose port docker-compose ps docker-compose pull docker-compose push docker-compose restart docker-compose rm docker-compose run docker-compose scale docker-compose start docker-compose stop docker-compose top docker-compose unpause docker-compose up Environment file (Compose) Environment variables in Compose Extend services in Compose Frequently asked questions (Compose) Getting started (Compose) Install Compose Link environment variables (deprecated) (Compose) Networking in Compose Overview of Docker Compose Overview of docker-compose CLI Quickstart: Compose and WordPress Rails and PostgreSQL (Compose) Sample apps with Compose Using Compose in production Using Compose with Swarm Engine .NET Core application (Engine) About images, containers, and storage drivers (Engine) Add nodes to the swarm (Engine) Apply custom metadata (Engine) Apply rolling updates (Engine) apt-cacher-ng Best practices for writing Dockerfiles (Engine) Binaries (Engine) Bind container ports to the host (Engine) Breaking changes (Engine) Build your own bridge (Engine) Configure container DNS (Engine) Configure container DNS in user-defined networks (Engine) CouchDB (Engine) Create a base image (Engine) Create a swarm (Engine) Customize the docker0 bridge (Engine) Debian (Engine) Default bridge network Delete the service (Engine) Deploy a service (Engine) Deploy services to a swarm (Engine) Deprecated Engine features Docker container networking (Engine) Docker overview (Engine) Docker run reference (Engine) Dockerfile reference (Engine) Dockerize an application Drain a node (Engine) Engine FAQ (Engine) Fedora (Engine) Get started (Engine) Get started with macvlan network driver (Engine) Get started with multi-host networking (Engine) How nodes work (Engine) How services work (Engine) Image management (Engine) Inspect the service (Engine) Install Docker (Engine) IPv6 with Docker (Engine) Join nodes to a swarm (Engine) Legacy container links (Engine) Lock your swarm (Engine) Manage nodes in a swarm (Engine) Manage sensitive data with Docker secrets (Engine) Manage swarm security with PKI (Engine) Manage swarm service networks (Engine) Migrate to Engine 1.10 Optional Linux post-installation steps (Engine) Overview (Engine) PostgreSQL (Engine) Raft consensus in swarm mode (Engine) Riak (Engine) Run Docker Engine in swarm mode Scale the service (Engine) SDKs (Engine) Select a storage driver (Engine) Set up for the tutorial (Engine) SSHd (Engine) Storage driver overview (Engine) Store service configuration data (Engine) Swarm administration guide (Engine) Swarm mode key concepts (Engine) Swarm mode overlay network security model (Engine) Swarm mode overview (Engine) Understand container communication (Engine) Use multi-stage builds (Engine) Use swarm mode routing mesh (Engine) Use the AUFS storage driver (Engine) Use the Btrfs storage driver (Engine) Use the Device mapper storage driver (Engine) Use the OverlayFS storage driver (Engine) Use the VFS storage driver (Engine) Use the ZFS storage driver (Engine) Engine: Admin Guide Amazon CloudWatch logs logging driver (Engine) Bind mounts (Engine) Collect Docker metrics with Prometheus (Engine) Configuring and running Docker (Engine) Configuring logging drivers (Engine) Control and configure Docker with systemd (Engine) ETW logging driver (Engine) Fluentd logging driver (Engine) Format command and log output (Engine) Google Cloud logging driver (Engine) Graylog Extended Format (GELF) logging driver (Engine) Journald logging driver (Engine) JSON File logging driver (Engine) Keep containers alive during daemon downtime (Engine) Limit a container's resources (Engine) Link via an ambassador container (Engine) Log tags for logging driver (Engine) Logentries logging driver (Engine) PowerShell DSC usage (Engine) Prune unused Docker objects (Engine) Run multiple services in a container (Engine) Runtime metrics (Engine) Splunk logging driver (Engine) Start containers automatically (Engine) Storage overview (Engine) Syslog logging driver (Engine) tmpfs mounts Troubleshoot volume problems (Engine) Use a logging driver plugin (Engine) Using Ansible (Engine) Using Chef (Engine) Using Puppet (Engine) View a container's logs (Engine) Volumes (Engine) Engine: CLI Daemon CLI reference (dockerd) (Engine) docker docker attach docker build docker checkpoint docker checkpoint create docker checkpoint ls docker checkpoint rm docker commit docker config docker config create docker config inspect docker config ls docker config rm docker container docker container attach docker container commit docker container cp docker container create docker container diff docker container exec docker container export docker container inspect docker container kill docker container logs docker container ls docker container pause docker container port docker container prune docker container rename docker container restart docker container rm docker container run docker container start docker container stats docker container stop docker container top docker container unpause docker container update docker container wait docker cp docker create docker deploy docker diff docker events docker exec docker export docker history docker image docker image build docker image history docker image import docker image inspect docker image load docker image ls docker image prune docker image pull docker image push docker image rm docker image save docker image tag docker images docker import docker info docker inspect docker kill docker load docker login docker logout docker logs docker network docker network connect docker network create docker network disconnect docker network inspect docker network ls docker network prune docker network rm docker node docker node demote docker node inspect docker node ls docker node promote docker node ps docker node rm docker node update docker pause docker plugin docker plugin create docker plugin disable docker plugin enable docker plugin inspect docker plugin install docker plugin ls docker plugin push docker plugin rm docker plugin set docker plugin upgrade docker port docker ps docker pull docker push docker rename docker restart docker rm docker rmi docker run docker save docker search docker secret docker secret create docker secret inspect docker secret ls docker secret rm docker service docker service create docker service inspect docker service logs docker service ls docker service ps docker service rm docker service scale docker service update docker stack docker stack deploy docker stack ls docker stack ps docker stack rm docker stack services docker start docker stats docker stop docker swarm docker swarm ca docker swarm init docker swarm join docker swarm join-token docker swarm leave docker swarm unlock docker swarm unlock-key docker swarm update docker system docker system df docker system events docker system info docker system prune docker tag docker top docker unpause docker update docker version docker volume docker volume create docker volume inspect docker volume ls docker volume prune docker volume rm docker wait Use the Docker command line (Engine) Engine: Extend Access authorization plugin (Engine) Docker log driver plugins Docker network driver plugins (Engine) Extending Engine with plugins Managed plugin system (Engine) Plugin configuration (Engine) Plugins API (Engine) Volume plugins (Engine) Engine: Security AppArmor security profiles for Docker (Engine) Automation with content trust (Engine) Content trust in Docker (Engine) Delegations for content trust (Engine) Deploying Notary (Engine) Docker security (Engine) Docker security non-events (Engine) Isolate containers with a user namespace (Engine) Manage keys for content trust (Engine) Play in a content trust sandbox (Engine) Protect the Docker daemon socket (Engine) Seccomp security profiles for Docker (Engine) Secure Engine Use trusted images Using certificates for repository client verification (Engine) Engine: Tutorials Engine tutorials Network containers (Engine) Get Started Part 1: Orientation Part 2: Containers Part 3: Services Part 4: Swarms Part 5: Stacks Part 6: Deploy your app Machine Amazon Web Services (Machine) Digital Ocean (Machine) docker-machine active docker-machine config docker-machine create docker-machine env docker-machine help docker-machine inspect docker-machine ip docker-machine kill docker-machine ls docker-machine provision docker-machine regenerate-certs docker-machine restart docker-machine rm docker-machine scp docker-machine ssh docker-machine start docker-machine status docker-machine stop docker-machine upgrade docker-machine url Driver options and operating system defaults (Machine) Drivers overview (Machine) Exoscale (Machine) Generic (Machine) Get started with a local VM (Machine) Google Compute Engine (Machine) IBM Softlayer (Machine) Install Machine Machine Machine CLI overview Machine command-line completion Machine concepts and help Machine overview Microsoft Azure (Machine) Microsoft Hyper-V (Machine) Migrate from Boot2Docker to Machine OpenStack (Machine) Oracle VirtualBox (Machine) Provision AWS EC2 instances (Machine) Provision Digital Ocean Droplets (Machine) Provision hosts in the cloud (Machine) Rackspace (Machine) VMware Fusion (Machine) VMware vCloud Air (Machine) VMware vSphere (Machine) Notary Client configuration (Notary) Common Server and signer configurations (Notary) Getting started with Notary Notary changelog Notary configuration files Running a Notary service Server configuration (Notary) Signer configuration (Notary) Understand the service architecture (Notary) Use the Notary client
characters

这个快速启动指南将向您展示如何使用 DockerCompose 来设置和运行 Rails/PostgreSQL 应用程序。在开始之前,你需要组合安装...

定义项目

首先设置构建应用程序所需的四个文件。首先,由于您的应用程序将在包含所有依赖关系的 Docker 容器中运行,因此您需要准确定义容器中需要包含的内容。这是通过使用名为的文件完成的Dockerfile。首先,Dockerfile 由以下部分组成:

FROM ruby:2.3.3RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs
RUN mkdir /myapp
WORKDIR /myapp
ADD Gemfile /myapp/Gemfile
ADD Gemfile.lock /myapp/Gemfile.lock
RUN bundle install
ADD . /myapp

这会将您的应用程序代码放入一个图像中,该图像将与 Ruby,Bundler 以及其内的所有依赖关系构建一个容器。有关如何编写 Dockerfiles 的更多信息,请参阅 Docker 用户指南和 Dockerfile 参考。

接下来,创建一个Gemfile只加载 Rails 的引导程序。它会在一瞬间被覆盖rails new

source 'https://rubygems.org'gem 'rails', '5.0.0.1'

你需要一个空的Gemfile.lock为了建造我们的Dockerfile

touch Gemfile.lock

最后,docker-compose.yml是魔术发生的地方。该文件描述了组成应用程序(数据库和 Web 应用程序)的服务,如何获取每个人的 Docker 镜像(数据库仅在预制 PostgreSQL 镜像上运行,Web 应用程序是从当前目录构建的),以及将它们链接在一起并公开 Web 应用程序的端口所需的配置。

version: '3'services:
  db:
    image: postgres
  web:
    build: .
    command: bundle exec rails s -p 3000 -b '0.0.0.0'
    volumes:      - .:/myapp
    ports:      - "3000:3000"
    depends_on:      - db

针尖::您可以使用.yml.yaml此文件的扩展名。

构建项目

有了这四个文件之后,现在可以使用以下方法生成 Rails 框架应用程序 docker-compose 运行。

docker-compose run web rails new . --force --database=postgresql

首先,撰写将为web使用Dockerfile.然后它会运行rails new在一个新的容器中,使用该图像。一旦完成,你就应该生成一个新的应用程序。

列出文件。

$ ls -l
total 64-rw-r--r--   1 vmb  staff   222 Jun  7 12:05 Dockerfile-rw-r--r--   1 vmb  staff  1738 Jun  7 12:09 Gemfile-rw-r--r--   1 vmb  staff  4297 Jun  7 12:09 Gemfile.lock-rw-r--r--   1 vmb  staff   374 Jun  7 12:09 README.md-rw-r--r--   1 vmb  staff   227 Jun  7 12:09 Rakefile
drwxr-xr-x  10 vmb  staff   340 Jun  7 12:09 app
drwxr-xr-x   8 vmb  staff   272 Jun  7 12:09 bin
drwxr-xr-x  14 vmb  staff   476 Jun  7 12:09 config-rw-r--r--   1 vmb  staff   130 Jun  7 12:09 config.ru
drwxr-xr-x   3 vmb  staff   102 Jun  7 12:09 db-rw-r--r--   1 vmb  staff   211 Jun  7 12:06 docker-compose.yml
drwxr-xr-x   4 vmb  staff   136 Jun  7 12:09 lib
drwxr-xr-x   3 vmb  staff   102 Jun  7 12:09 log
drwxr-xr-x   9 vmb  staff   306 Jun  7 12:09 publicdrwxr-xr-x   9 vmb  staff   306 Jun  7 12:09 test
drwxr-xr-x   4 vmb  staff   136 Jun  7 12:09 tmp
drwxr-xr-x   3 vmb  staff   102 Jun  7 12:09 vendor

如果您在 Linux 上运行 Docker,则文件rails new所创建的是根用户拥有的。这是因为容器以根用户的身份运行。如果是这样,请更改新文件的所有权。

sudo chown -R $USER:$USER .

如果您在 Mac 或 Windows 上运行 Docker,您应该已经拥有所有文件的所有权,包括rails new

现在你已经有了一个新的 Gemfile,你需要再次构建图像。(这和Gemfile对 Dockerfile 的更改应该是唯一需要重建的时间。)

docker-compose build

连接数据库

应用程序现在可以启动,但你还没有完全到位。默认情况下,Rails期望数据库在其上运行。localhost-所以你需要把它指向db而是集装箱。您还需要更改数据库和用户名,使其与postgres图像。

替换config/database.yml的内容有以下几点:

default: &default
  adapter: postgresql
  encoding: unicode
  host: db
  username: postgres
  password:
  pool: 5development:  <<: *default
  database: myapp_development


test:  <<: *default
  database: myapp_test

您现在可以使用 docker 启动应用程序 - 撰写:

docker-compose up

如果一切顺利,您应该会看到一些 PostgreSQL 输出,然后几秒钟后是8212;您应该看到熟悉的重音:

Starting rails_db_1 ...Starting rails_db_1 ... done
Recreating rails_web_1 ...Recreating rails_web_1 ... done
Attaching to rails_db_1, rails_web_1
db_1   | LOG:  database system was shut down at 2017-06-07 19:12:02 UTC
db_1   | LOG:  MultiXact member wraparound protections are now enabled
db_1   | LOG:  database system is ready to accept connections
db_1   | LOG:  autovacuum launcher started
web_1  | => Booting Puma
web_1  | => Rails 5.0.0.1 application starting in development on http://0.0.0.0:3000web_1  | => Run `rails server -h` for more startup options
web_1  | Puma starting in single mode...web_1  | * Version 3.9.1 (ruby 2.3.3-p222), codename: Private Caller
web_1  | * Min threads: 5, max threads: 5web_1  | * Environment: development
web_1  | * Listening on tcp://0.0.0.0:3000web_1  | Use Ctrl-C to stop

最后,您需要创建数据库。在另一个终端,运行:

docker-compose run web rake db:create

下面是该命令的输出示例:

vmb at snapair in ~/sandbox/rails
$ docker-compose run web rake db:create
Starting rails_db_1 ... done
Created database 'myapp_development'Created database 'myapp_test'

查看 Rails 欢迎页面!

就这样,您的应用程序现在应该运行在您的 Docker 守护进程的端口3000上。

在 Docker for Mac 和 Docker for Windows 上,转到http://localhost:3000Web 浏览器以查看 Rails 欢迎。

如果您使用 Docker Machine,则docker-machine ip MACHINE_VM返回可添加端口(<Docker-Host-IP>:3000)的 Docker 主机 IP 地址。

二次


停止应用程序

若要停止应用程序,请运行码头工人在您的项目目录中。您可以使用启动数据库的同一终端窗口,也可以使用访问命令提示符的另一个终端窗口。这是停止应用程序的一种干净的方法。

vmb at snapair in ~/sandbox/rails
$ docker-compose down
Stopping rails_web_1 ... done
Stopping rails_db_1 ... done
Removing rails_web_run_1 ... done
Removing rails_web_1 ... done
Removing rails_db_1 ... done
Removing network rails_default

您还可以用Ctrl-C在执行docker-compose up如果您以这种方式停止应用程序,并试图重新启动它,您可能会收到以下错误:

web_1 | A server is already
running. Check /myapp/tmp/pids/server.pid.

若要解决此问题,请删除该文件。tmp/pids/server.pid,然后用docker-compose up

重新启动应用程序

要重新启动应用程序:

  1. 运行docker-compose up在项目目录中。

  1. 在另一个终端中运行此命令以重新启动数据库:docker-compose run web rake db:create

重建应用程序

如果对 Gemfile 或 Composed 文件进行更改以尝试一些不同的配置,则需要重新构建。有些更改只需要docker-compose up --build,但是完全重建需要重新运行docker-compose run web bundle install同步Gemfile.lock给主机,然后是docker-compose up --build

下面是第一个例子,在这种情况下,完全重建是不必要的。假设您只想将本地主机上的公开端口更改为3000在我们的第一个例子中3001更改撰写文件以公开端口。3000在新港口的集装箱上,3001,并保存更改:

ports: - "3001:3000"

现在,用docker-compose up --build,然后重新启动数据库:docker-compose run web rake db:create...

在容器内部,您的应用程序像以前一样运行在相同的端口上3000,但现在http://localhost:3001您的本地主机上仍可使用 Rails Welcome 。

更多撰写文档

  • 用户指南

  • 安装组合

  • 开始

  • 从 Django 开始

  • 从 WordPress 开始

  • 命令行引用

  • 合成文件引用

Previous article: Next article: