目录搜索
ComposeAbout 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 bundledocker-compose configdocker-compose createdocker-compose downdocker-compose eventsdocker-compose execdocker-compose helpdocker-compose imagesdocker-compose killdocker-compose logsdocker-compose pausedocker-compose portdocker-compose psdocker-compose pulldocker-compose pushdocker-compose restartdocker-compose rmdocker-compose rundocker-compose scaledocker-compose startdocker-compose stopdocker-compose topdocker-compose unpausedocker-compose upEnvironment file (Compose)Environment variables in ComposeExtend services in ComposeFrequently asked questions (Compose)Getting started (Compose)Install ComposeLink environment variables (deprecated) (Compose)Networking in ComposeOverview of Docker ComposeOverview of docker-compose CLIQuickstart: Compose and WordPressRails and PostgreSQL (Compose)Sample apps with ComposeUsing Compose in productionUsing Compose with SwarmEngine.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-ngBest 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 networkDelete the service (Engine)Deploy a service (Engine)Deploy services to a swarm (Engine)Deprecated Engine featuresDocker container networking (Engine)Docker overview (Engine)Docker run reference (Engine)Dockerfile reference (Engine)Dockerize an applicationDrain a node (Engine)EngineFAQ (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.10Optional Linux post-installation steps (Engine)Overview (Engine)PostgreSQL (Engine)Raft consensus in swarm mode (Engine)Riak (Engine)Run Docker Engine in swarm modeScale 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 GuideAmazon 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 mountsTroubleshoot 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: CLIDaemon CLI reference (dockerd) (Engine)dockerdocker attachdocker builddocker checkpointdocker checkpoint createdocker checkpoint lsdocker checkpoint rmdocker commitdocker configdocker config createdocker config inspectdocker config lsdocker config rmdocker containerdocker container attachdocker container commitdocker container cpdocker container createdocker container diffdocker container execdocker container exportdocker container inspectdocker container killdocker container logsdocker container lsdocker container pausedocker container portdocker container prunedocker container renamedocker container restartdocker container rmdocker container rundocker container startdocker container statsdocker container stopdocker container topdocker container unpausedocker container updatedocker container waitdocker cpdocker createdocker deploydocker diffdocker eventsdocker execdocker exportdocker historydocker imagedocker image builddocker image historydocker image importdocker image inspectdocker image loaddocker image lsdocker image prunedocker image pulldocker image pushdocker image rmdocker image savedocker image tagdocker imagesdocker importdocker infodocker inspectdocker killdocker loaddocker logindocker logoutdocker logsdocker networkdocker network connectdocker network createdocker network disconnectdocker network inspectdocker network lsdocker network prunedocker network rmdocker nodedocker node demotedocker node inspectdocker node lsdocker node promotedocker node psdocker node rmdocker node updatedocker pausedocker plugindocker plugin createdocker plugin disabledocker plugin enabledocker plugin inspectdocker plugin installdocker plugin lsdocker plugin pushdocker plugin rmdocker plugin setdocker plugin upgradedocker portdocker psdocker pulldocker pushdocker renamedocker restartdocker rmdocker rmidocker rundocker savedocker searchdocker secretdocker secret createdocker secret inspectdocker secret lsdocker secret rmdocker servicedocker service createdocker service inspectdocker service logsdocker service lsdocker service psdocker service rmdocker service scaledocker service updatedocker stackdocker stack deploydocker stack lsdocker stack psdocker stack rmdocker stack servicesdocker startdocker statsdocker stopdocker swarmdocker swarm cadocker swarm initdocker swarm joindocker swarm join-tokendocker swarm leavedocker swarm unlockdocker swarm unlock-keydocker swarm updatedocker systemdocker system dfdocker system eventsdocker system infodocker system prunedocker tagdocker topdocker unpausedocker updatedocker versiondocker volumedocker volume createdocker volume inspectdocker volume lsdocker volume prunedocker volume rmdocker waitUse the Docker command line (Engine)Engine: ExtendAccess authorization plugin (Engine)Docker log driver pluginsDocker network driver plugins (Engine)Extending Engine with pluginsManaged plugin system (Engine)Plugin configuration (Engine)Plugins API (Engine)Volume plugins (Engine)Engine: SecurityAppArmor 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 EngineUse trusted imagesUsing certificates for repository client verification (Engine)Engine: TutorialsEngine tutorialsNetwork containers (Engine)Get StartedPart 1: OrientationPart 2: ContainersPart 3: ServicesPart 4: SwarmsPart 5: StacksPart 6: Deploy your appMachineAmazon Web Services (Machine)Digital Ocean (Machine)docker-machine activedocker-machine configdocker-machine createdocker-machine envdocker-machine helpdocker-machine inspectdocker-machine ipdocker-machine killdocker-machine lsdocker-machine provisiondocker-machine regenerate-certsdocker-machine restartdocker-machine rmdocker-machine scpdocker-machine sshdocker-machine startdocker-machine statusdocker-machine stopdocker-machine upgradedocker-machine urlDriver 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 MachineMachineMachine CLI overviewMachine command-line completionMachine concepts and helpMachine overviewMicrosoft Azure (Machine)Microsoft Hyper-V (Machine)Migrate from Boot2Docker to MachineOpenStack (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)NotaryClient configuration (Notary)Common Server and signer configurations (Notary)Getting started with NotaryNotary changelogNotary configuration filesRunning a Notary serviceServer configuration (Notary)Signer configuration (Notary)Understand the service architecture (Notary)Use the Notary client
文字

这个快速启动指南将向您展示如何使用 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 开始

  • 命令行引用

  • 合成文件引用

上一篇:下一篇: