0

I am working on a CloudBuild script that needs to instantiate a local DB for testing of a backend service. I am using docker/compose:1.25.3 for this purpose. The script works locally but when running on CloudBuild the service is unable to connect to the database. The relevant scripts can be observed below:

docker-compose.test.yml

version: '3.4'

services:
  app:
    image: gcr.io/${PROJECT_ID}/<REDACTED>/test:${SHORT_SHA}
    command: ["bundle", "exec", "rails", "t"]
    ports:
      - 3000:3000
    depends_on:
      - db      
  db:
    image: mysql:5.7.24
    environment:
      - MYSQL_ALLOW_EMPTY_PASSWORD=true
      - MYSQL_DATABASE=sales_test
      - MYSQL_HOST=0.0.0.0
      - MYSQL_TCP_PORT=3306

networks:
  default:
      external:
          name: cloudbuild

cloudbuild.yaml

steps:
  # Run Tests
  - name: 'docker/compose:1.25.3'
    id: test
    args: ['-f', 'docker-compose.test.yml', 'up', '--exit-code-from', 'app']
    env:
      - 'PROJECT_ID=$PROJECT_ID'
      - 'SHORT_SHA=$SHORT_SHA'

The error log (with minor redactions) can be seen below:

Pulling image: docker/compose:1.25.3
1.25.3: Pulling from docker/compose
89d9c30c1d48: Already exists
8c7adae20efb: Pulling fs layer
c8bcdc0a450b: Pulling fs layer
cee865d72b79: Pulling fs layer
8c7adae20efb: Verifying Checksum
8c7adae20efb: Download complete
8c7adae20efb: Pull complete
cee865d72b79: Verifying Checksum
cee865d72b79: Download complete
c8bcdc0a450b: Verifying Checksum
c8bcdc0a450b: Download complete
c8bcdc0a450b: Pull complete
cee865d72b79: Pull complete
Digest: sha256:3ab5ae464a8a428085729d6a0f2ab7a5ccc5524f6ca8649855c7d21fb86148cd
Status: Downloaded newer image for docker/compose:1.25.3
docker.io/docker/compose:1.25.3
using --exit-code-from implies --abort-on-container-exit
Pulling db (mysql:5.7.24)...
5.7.24: Pulling from library/mysql
Digest: sha256:bf17a7109057494c45fba5aab7fc805ca00ac1eef638dfdd42b38d5a7190c9bb
Status: Downloaded newer image for mysql:5.7.24
Pulling app (gcr.io/<REDACTED>)...
e98c184: Pulling from <REDACTED>
Digest: sha256:55f6679af82d7ca5ccac5a2d2aefe0b1c75e4d65b49cd6c6a768c074dfafc483
Status: Downloaded newer image for gcr.io/<REDACTED>
Creating workspace_db_1 ... 

Attaching to workspace_db_1, workspace_app_1
db_1   | Initializing database
db_1   | 2020-02-09T16:53:45.954586Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
db_1   | 2020-02-09T16:53:47.683244Z 0 [Warning] InnoDB: New log files created, LSN=45790
db_1   | 2020-02-09T16:53:47.892451Z 0 [Warning] InnoDB: Creating foreign key constraint system tables.
db_1   | 2020-02-09T16:53:47.953412Z 0 [Warning] No existing UUID has been found, so we assume that this is the first time that this server has been started. Generating a new UUID: bdad19cb-4b5c-11ea-a027-0242c0a80a03.
db_1   | 2020-02-09T16:53:47.957113Z 0 [Warning] Gtid table is not ready to be used. Table 'mysql.gtid_executed' cannot be opened.
db_1   | 2020-02-09T16:53:47.958298Z 1 [Warning] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
db_1   | 2020-02-09T16:53:48.693564Z 1 [Warning] 'user' entry 'root@localhost' ignored in --skip-name-resolve mode.
db_1   | 2020-02-09T16:53:48.694594Z 1 [Warning] 'user' entry 'mysql.session@localhost' ignored in --skip-name-resolve mode.
db_1   | 2020-02-09T16:53:48.695129Z 1 [Warning] 'user' entry 'mysql.sys@localhost' ignored in --skip-name-resolve mode.
db_1   | 2020-02-09T16:53:48.695592Z 1 [Warning] 'db' entry 'performance_schema mysql.session@localhost' ignored in --skip-name-resolve mode.
db_1   | 2020-02-09T16:53:48.695985Z 1 [Warning] 'db' entry 'sys mysql.sys@localhost' ignored in --skip-name-resolve mode.
db_1   | 2020-02-09T16:53:48.696090Z 1 [Warning] 'proxies_priv' entry '@ root@localhost' ignored in --skip-name-resolve mode.
db_1   | 2020-02-09T16:53:48.696300Z 1 [Warning] 'tables_priv' entry 'user mysql.session@localhost' ignored in --skip-name-resolve mode.
db_1   | 2020-02-09T16:53:48.696425Z 1 [Warning] 'tables_priv' entry 'sys_config mysql.sys@localhost' ignored in --skip-name-resolve mode.
db_1   | Database initialized
db_1   | Initializing certificates
db_1   | Generating a RSA private key
db_1   | .............................................+++++
db_1   | .+++++
db_1   | unable to write 'random state'
db_1   | writing new private key to 'ca-key.pem'
db_1   | -----
db_1   | Generating a RSA private key
db_1   | .........................+++++
db_1   | ................................+++++
db_1   | unable to write 'random state'
db_1   | writing new private key to 'server-key.pem'
db_1   | -----
db_1   | Generating a RSA private key
db_1   | ........................................................+++++
db_1   | .......................................................................................................+++++
db_1   | unable to write 'random state'
db_1   | writing new private key to 'client-key.pem'
db_1   | -----
db_1   | Certificates initialized
db_1   | MySQL init process in progress...
db_1   | 2020-02-09T16:53:52.331939Z 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use --explicit_defaults_for_timestamp server option (see documentation for more details).
db_1   | 2020-02-09T16:53:52.338264Z 0 [Note] mysqld (mysqld 5.7.24) starting as process 88 ...
db_1   | 2020-02-09T16:53:52.342658Z 0 [Note] InnoDB: PUNCH HOLE support available
db_1   | 2020-02-09T16:53:52.345876Z 0 [Note] InnoDB: Mutexes and rw_locks use GCC atomic builtins
db_1   | 2020-02-09T16:53:52.346533Z 0 [Note] InnoDB: Uses event mutexes
db_1   | 2020-02-09T16:53:52.347011Z 0 [Note] InnoDB: GCC builtin __atomic_thread_fence() is used for memory barrier
db_1   | 2020-02-09T16:53:52.347439Z 0 [Note] InnoDB: Compressed tables use zlib 1.2.11
db_1   | 2020-02-09T16:53:52.347864Z 0 [Note] InnoDB: Using Linux native AIO
db_1   | 2020-02-09T16:53:52.348711Z 0 [Note] InnoDB: Number of pools: 1
db_1   | 2020-02-09T16:53:52.349446Z 0 [Note] InnoDB: Using CPU crc32 instructions
db_1   | 2020-02-09T16:53:52.352053Z 0 [Note] InnoDB: Initializing buffer pool, total size = 128M, instances = 1, chunk size = 128M
db_1   | 2020-02-09T16:53:52.374298Z 0 [Note] InnoDB: Completed initialization of buffer pool
db_1   | 2020-02-09T16:53:52.377886Z 0 [Note] InnoDB: If the mysqld execution user is authorized, page cleaner thread priority can be changed. See the man page of setpriority().
db_1   | 2020-02-09T16:53:52.389720Z 0 [Note] InnoDB: Highest supported file format is Barracuda.
db_1   | 2020-02-09T16:53:52.406894Z 0 [Note] InnoDB: Creating shared tablespace for temporary tables
db_1   | 2020-02-09T16:53:52.409937Z 0 [Note] InnoDB: Setting file './ibtmp1' size to 12 MB. Physically writing the file full; Please wait ...
db_1   | 2020-02-09T16:53:52.616442Z 0 [Note] InnoDB: File './ibtmp1' size is now 12 MB.
db_1   | 2020-02-09T16:53:52.617610Z 0 [Note] InnoDB: 96 redo rollback segment(s) found. 96 redo rollback segment(s) are active.
db_1   | 2020-02-09T16:53:52.618823Z 0 [Note] InnoDB: 32 non-redo rollback segment(s) are active.
db_1   | 2020-02-09T16:53:52.619566Z 0 [Note] InnoDB: Waiting for purge to start
db_1   | 2020-02-09T16:53:52.670260Z 0 [Note] InnoDB: 5.7.24 started; log sequence number 2591440
db_1   | 2020-02-09T16:53:52.671455Z 0 [Note] Plugin 'FEDERATED' is disabled.
db_1   | 2020-02-09T16:53:52.682070Z 0 [Note] InnoDB: Loading buffer pool(s) from /var/lib/mysql/ib_buffer_pool
db_1   | 2020-02-09T16:53:52.684590Z 0 [Note] InnoDB: Buffer pool(s) load completed at 200209 16:53:52
db_1   | 2020-02-09T16:53:52.690301Z 0 [Note] Found ca.pem, server-cert.pem and server-key.pem in data directory. Trying to enable SSL support using them.
db_1   | 2020-02-09T16:53:52.691413Z 0 [Warning] CA certificate ca.pem is self signed.
db_1   | 2020-02-09T16:53:52.696830Z 0 [Warning] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.
db_1   | 2020-02-09T16:53:52.698713Z 0 [Warning] 'user' entry 'root@localhost' ignored in --skip-name-resolve mode.
db_1   | 2020-02-09T16:53:52.699389Z 0 [Warning] 'user' entry 'mysql.session@localhost' ignored in --skip-name-resolve mode.
db_1   | 2020-02-09T16:53:52.699887Z 0 [Warning] 'user' entry 'mysql.sys@localhost' ignored in --skip-name-resolve mode.
db_1   | 2020-02-09T16:53:52.700362Z 0 [Warning] 'db' entry 'performance_schema mysql.session@localhost' ignored in --skip-name-resolve mode.
db_1   | 2020-02-09T16:53:52.700817Z 0 [Warning] 'db' entry 'sys mysql.sys@localhost' ignored in --skip-name-resolve mode.
db_1   | 2020-02-09T16:53:52.701498Z 0 [Warning] 'proxies_priv' entry '@ root@localhost' ignored in --skip-name-resolve mode.
db_1   | 2020-02-09T16:53:52.703369Z 0 [Warning] 'tables_priv' entry 'user mysql.session@localhost' ignored in --skip-name-resolve mode.
db_1   | 2020-02-09T16:53:52.703956Z 0 [Warning] 'tables_priv' entry 'sys_config mysql.sys@localhost' ignored in --skip-name-resolve mode.
db_1   | 2020-02-09T16:53:52.713772Z 0 [Note] Event Scheduler: Loaded 0 events
db_1   | 2020-02-09T16:53:52.721653Z 0 [Note] mysqld: ready for connections.
db_1   | Version: '5.7.24'  socket: '/var/run/mysqld/mysqld.sock'  port: 0  MySQL Community Server (GPL)
db_1   | Warning: Unable to load '/usr/share/zoneinfo/iso3166.tab' as time zone. Skipping it.
db_1   | Warning: Unable to load '/usr/share/zoneinfo/leap-seconds.list' as time zone. Skipping it.
db_1   | Warning: Unable to load '/usr/share/zoneinfo/zone.tab' as time zone. Skipping it.
db_1   | Warning: Unable to load '/usr/share/zoneinfo/zone1970.tab' as time zone. Skipping it.
db_1   | 2020-02-09T16:53:56.959302Z 4 [Warning] 'user' entry 'root@localhost' ignored in --skip-name-resolve mode.
db_1   | 2020-02-09T16:53:56.960220Z 4 [Warning] 'user' entry 'mysql.session@localhost' ignored in --skip-name-resolve mode.
db_1   | 2020-02-09T16:53:56.960744Z 4 [Warning] 'user' entry 'mysql.sys@localhost' ignored in --skip-name-resolve mode.
db_1   | 2020-02-09T16:53:56.961208Z 4 [Warning] 'db' entry 'performance_schema mysql.session@localhost' ignored in --skip-name-resolve mode.
db_1   | 2020-02-09T16:53:56.961636Z 4 [Warning] 'db' entry 'sys mysql.sys@localhost' ignored in --skip-name-resolve mode.
db_1   | 2020-02-09T16:53:56.962216Z 4 [Warning] 'proxies_priv' entry '@ root@localhost' ignored in --skip-name-resolve mode.
db_1   | 2020-02-09T16:53:56.962711Z 4 [Warning] 'tables_priv' entry 'user mysql.session@localhost' ignored in --skip-name-resolve mode.
db_1   | 2020-02-09T16:53:56.963322Z 4 [Warning] 'tables_priv' entry 'sys_config mysql.sys@localhost' ignored in --skip-name-resolve mode.
db_1   | 
db_1   | 2020-02-09T16:53:56.981051Z 0 [Note] Giving 0 client threads a chance to die gracefully
db_1   | 2020-02-09T16:53:56.981952Z 0 [Note] Shutting down slave threads
db_1   | 2020-02-09T16:53:56.982615Z 0 [Note] Forcefully disconnecting 0 remaining clients
db_1   | 2020-02-09T16:53:56.983096Z 0 [Note] Event Scheduler: Purging the queue. 0 events
db_1   | 2020-02-09T16:53:56.983631Z 0 [Note] Binlog end
db_1   | 2020-02-09T16:53:56.984775Z 0 [Note] Shutting down plugin 'ngram'
db_1   | 2020-02-09T16:53:56.985319Z 0 [Note] Shutting down plugin 'partition'
db_1   | 2020-02-09T16:53:56.985783Z 0 [Note] Shutting down plugin 'BLACKHOLE'
db_1   | 2020-02-09T16:53:56.986270Z 0 [Note] Shutting down plugin 'ARCHIVE'
db_1   | 2020-02-09T16:53:56.986693Z 0 [Note] Shutting down plugin 'PERFORMANCE_SCHEMA'
db_1   | 2020-02-09T16:53:56.987126Z 0 [Note] Shutting down plugin 'MRG_MYISAM'
db_1   | 2020-02-09T16:53:56.987689Z 0 [Note] Shutting down plugin 'MyISAM'
db_1   | 2020-02-09T16:53:56.989856Z 0 [Note] Shutting down plugin 'INNODB_SYS_VIRTUAL'
db_1   | 2020-02-09T16:53:56.990394Z 0 [Note] Shutting down plugin 'INNODB_SYS_DATAFILES'
db_1   | 2020-02-09T16:53:56.990818Z 0 [Note] Shutting down plugin 'INNODB_SYS_TABLESPACES'
db_1   | 2020-02-09T16:53:56.991482Z 0 [Note] Shutting down plugin 'INNODB_SYS_FOREIGN_COLS'
db_1   | 2020-02-09T16:53:56.991930Z 0 [Note] Shutting down plugin 'INNODB_SYS_FOREIGN'
db_1   | 2020-02-09T16:53:56.992046Z 0 [Note] Shutting down plugin 'INNODB_SYS_FIELDS'
db_1   | 2020-02-09T16:53:56.992159Z 0 [Note] Shutting down plugin 'INNODB_SYS_COLUMNS'
db_1   | 2020-02-09T16:53:56.992229Z 0 [Note] Shutting down plugin 'INNODB_SYS_INDEXES'
db_1   | 2020-02-09T16:53:56.992287Z 0 [Note] Shutting down plugin 'INNODB_SYS_TABLESTATS'
db_1   | 2020-02-09T16:53:56.992387Z 0 [Note] Shutting down plugin 'INNODB_SYS_TABLES'
db_1   | 2020-02-09T16:53:56.992455Z 0 [Note] Shutting down plugin 'INNODB_FT_INDEX_TABLE'
db_1   | 2020-02-09T16:53:56.992513Z 0 [Note] Shutting down plugin 'INNODB_FT_INDEX_CACHE'
db_1   | 2020-02-09T16:53:56.992577Z 0 [Note] Shutting down plugin 'INNODB_FT_CONFIG'
db_1   | 2020-02-09T16:53:56.992685Z 0 [Note] Shutting down plugin 'INNODB_FT_BEING_DELETED'
db_1   | 2020-02-09T16:53:56.992755Z 0 [Note] Shutting down plugin 'INNODB_FT_DELETED'
db_1   | 2020-02-09T16:53:56.992811Z 0 [Note] Shutting down plugin 'INNODB_FT_DEFAULT_STOPWORD'
db_1   | 2020-02-09T16:53:56.992874Z 0 [Note] Shutting down plugin 'INNODB_METRICS'
db_1   | 2020-02-09T16:53:56.992939Z 0 [Note] Shutting down plugin 'INNODB_TEMP_TABLE_INFO'
db_1   | 2020-02-09T16:53:56.993063Z 0 [Note] Shutting down plugin 'INNODB_BUFFER_POOL_STATS'
db_1   | 2020-02-09T16:53:56.993134Z 0 [Note] Shutting down plugin 'INNODB_BUFFER_PAGE_LRU'
db_1   | 2020-02-09T16:53:56.993204Z 0 [Note] Shutting down plugin 'INNODB_BUFFER_PAGE'
db_1   | 2020-02-09T16:53:56.993274Z 0 [Note] Shutting down plugin 'INNODB_CMP_PER_INDEX_RESET'
db_1   | 2020-02-09T16:53:56.993340Z 0 [Note] Shutting down plugin 'INNODB_CMP_PER_INDEX'
db_1   | 2020-02-09T16:53:56.993408Z 0 [Note] Shutting down plugin 'INNODB_CMPMEM_RESET'
db_1   | 2020-02-09T16:53:56.993465Z 0 [Note] Shutting down plugin 'INNODB_CMPMEM'
db_1   | 2020-02-09T16:53:56.993534Z 0 [Note] Shutting down plugin 'INNODB_CMP_RESET'
db_1   | 2020-02-09T16:53:56.993644Z 0 [Note] Shutting down plugin 'INNODB_CMP'
db_1   | 2020-02-09T16:53:56.993712Z 0 [Note] Shutting down plugin 'INNODB_LOCK_WAITS'
db_1   | 2020-02-09T16:53:56.993768Z 0 [Note] Shutting down plugin 'INNODB_LOCKS'
db_1   | 2020-02-09T16:53:56.993902Z 0 [Note] Shutting down plugin 'INNODB_TRX'
db_1   | 2020-02-09T16:53:56.993974Z 0 [Note] Shutting down plugin 'InnoDB'
db_1   | 2020-02-09T16:53:56.997425Z 0 [Note] InnoDB: FTS optimize thread exiting.
db_1   | 2020-02-09T16:53:56.997740Z 0 [Note] InnoDB: Starting shutdown...
app_1  | DEPRECATION WARNING: ActiveSupport.halt_callback_chains_on_return_false= is deprecated and will be removed in Rails 5.2. (called from <top (required)> at /app/config/initializers/new_framework_defaults.rb:25)
app_1  | /app/vendor/bundle/ruby/2.5.0/gems/mysql2-0.4.10/lib/mysql2/client.rb:89:in `connect': Can't connect to MySQL server on 'db' (115) (Mysql2::Error)
app_1  |    from /app/vendor/bundle/ruby/2.5.0/gems/mysql2-0.4.10/lib/mysql2/client.rb:89:in `initialize'
app_1  |    from /app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.1.2/lib/active_record/connection_adapters/mysql2_adapter.rb:21:in `new'
app_1  |    from /app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.1.2/lib/active_record/connection_adapters/mysql2_adapter.rb:21:in `mysql2_connection'
app_1  |    from /app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.1.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:759:in `new_connection'
app_1  |    from /app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.1.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:803:in `checkout_new_connection'
app_1  |    from /app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.1.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:782:in `try_to_checkout_new_connection'
app_1  |    from /app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.1.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:743:in `acquire_connection'
app_1  |    from /app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.1.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:500:in `checkout'
app_1  |    from /app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.1.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:374:in `connection'
app_1  |    from /app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.1.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:931:in `retrieve_connection'
app_1  |    from /app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.1.2/lib/active_record/connection_handling.rb:116:in `retrieve_connection'
app_1  |    from /app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.1.2/lib/active_record/connection_handling.rb:88:in `connection'
app_1  |    from /app/vendor/bundle/ruby/2.5.0/gems/activerecord-5.1.2/lib/active_record/model_schema.rb:331:in `table_exists?'
app_1  |    from /app/vendor/bundle/ruby/2.5.0/bundler/gems/spree-5ff46f2f5990/core/app/models/spree/preferences/store.rb:89:in `should_persist?'
app_1  |    from /app/vendor/bundle/ruby/2.5.0/bundler/gems/spree-5ff46f2f5990/core/app/models/spree/preferences/store.rb:74:in `persist'
app_1  |    from /app/vendor/bundle/ruby/2.5.0/bundler/gems/spree-5ff46f2f5990/core/app/models/spree/preferences/store.rb:21:in `set'
app_1  |    from /app/vendor/bundle/ruby/2.5.0/bundler/gems/spree-5ff46f2f5990/core/app/models/spree/preferences/scoped_store.rb:17:in `[]='
app_1  |    from /app/vendor/bundle/ruby/2.5.0/bundler/gems/spree-5ff46f2f5990/core/app/models/spree/preferences/preferable_class_methods.rb:20:in `block in preference'
app_1  |    from /app/vendor/bundle/ruby/2.5.0/bundler/gems/spree-5ff46f2f5990/core/app/models/spree/preferences/preferable.rb:48:in `set_preference'
app_1  |    from /app/vendor/bundle/ruby/2.5.0/bundler/gems/spree-5ff46f2f5990/core/app/models/spree/preferences/configuration.rb:61:in `method_missing'
app_1  |    from /app/config/initializers/spree.rb:13:in `block in <top (required)>'
app_1  |    from /app/vendor/bundle/ruby/2.5.0/bundler/gems/spree-5ff46f2f5990/core/lib/spree/core.rb:59:in `config'
app_1  |    from /app/config/initializers/spree.rb:12:in `<top (required)>'
app_1  |    from /app/vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.2/lib/active_support/dependencies.rb:286:in `load'
app_1  |    from /app/vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.2/lib/active_support/dependencies.rb:286:in `block in load'
app_1  |    from /app/vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.2/lib/active_support/dependencies.rb:258:in `load_dependency'
app_1  |    from /app/vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.2/lib/active_support/dependencies.rb:286:in `load'
app_1  |    from /app/vendor/bundle/ruby/2.5.0/gems/railties-5.1.2/lib/rails/engine.rb:655:in `block in load_config_initializer'
app_1  |    from /app/vendor/bundle/ruby/2.5.0/gems/activesupport-5.1.2/lib/active_support/notifications.rb:168:in `instrument'
app_1  |    from /app/vendor/bundle/ruby/2.5.0/gems/railties-5.1.2/lib/rails/engine.rb:654:in `load_config_initializer'
app_1  |    from /app/vendor/bundle/ruby/2.5.0/gems/railties-5.1.2/lib/rails/engine.rb:612:in `block (2 levels) in <class:Engine>'
app_1  |    from /app/vendor/bundle/ruby/2.5.0/gems/railties-5.1.2/lib/rails/engine.rb:611:in `each'
app_1  |    from /app/vendor/bundle/ruby/2.5.0/gems/railties-5.1.2/lib/rails/engine.rb:611:in `block in <class:Engine>'
app_1  |    from /app/vendor/bundle/ruby/2.5.0/gems/railties-5.1.2/lib/rails/initializable.rb:30:in `instance_exec'
app_1  |    from /app/vendor/bundle/ruby/2.5.0/gems/railties-5.1.2/lib/rails/initializable.rb:30:in `run'
app_1  |    from /app/vendor/bundle/ruby/2.5.0/gems/railties-5.1.2/lib/rails/initializable.rb:59:in `block in run_initializers'
app_1  |    from /usr/local/lib/ruby/2.5.0/tsort.rb:228:in `block in tsort_each'
app_1  |    from /usr/local/lib/ruby/2.5.0/tsort.rb:350:in `block (2 levels) in each_strongly_connected_component'
app_1  |    from /usr/local/lib/ruby/2.5.0/tsort.rb:422:in `block (2 levels) in each_strongly_connected_component_from'
app_1  |    from /usr/local/lib/ruby/2.5.0/tsort.rb:431:in `each_strongly_connected_component_from'
app_1  |    from /usr/local/lib/ruby/2.5.0/tsort.rb:421:in `block in each_strongly_connected_component_from'
app_1  |    from /app/vendor/bundle/ruby/2.5.0/gems/railties-5.1.2/lib/rails/initializable.rb:48:in `each'
app_1  |    from /app/vendor/bundle/ruby/2.5.0/gems/railties-5.1.2/lib/rails/initializable.rb:48:in `tsort_each_child'
app_1  |    from /usr/local/lib/ruby/2.5.0/tsort.rb:415:in `call'
app_1  |    from /usr/local/lib/ruby/2.5.0/tsort.rb:415:in `each_strongly_connected_component_from'
app_1  |    from /usr/local/lib/ruby/2.5.0/tsort.rb:349:in `block in each_strongly_connected_component'
app_1  |    from /usr/local/lib/ruby/2.5.0/tsort.rb:347:in `each'
app_1  |    from /usr/local/lib/ruby/2.5.0/tsort.rb:347:in `call'
app_1  |    from /usr/local/lib/ruby/2.5.0/tsort.rb:347:in `each_strongly_connected_component'
app_1  |    from /usr/local/lib/ruby/2.5.0/tsort.rb:226:in `tsort_each'
app_1  |    from /usr/local/lib/ruby/2.5.0/tsort.rb:205:in `tsort_each'
app_1  |    from /app/vendor/bundle/ruby/2.5.0/gems/railties-5.1.2/lib/rails/initializable.rb:58:in `run_initializers'
app_1  |    from /app/vendor/bundle/ruby/2.5.0/gems/railties-5.1.2/lib/rails/application.rb:353:in `initialize!'
app_1  |    from /app/config/environment.rb:5:in `<top (required)>'
app_1  |    from /app/test/test_helper.rb:4:in `require'
app_1  |    from /app/test/test_helper.rb:4:in `<top (required)>'
app_1  |    from /app/test/controllers/admin/login_controller_test.rb:1:in `require'
app_1  |    from /app/test/controllers/admin/login_controller_test.rb:1:in `<top (required)>'
app_1  |    from /app/vendor/bundle/ruby/2.5.0/gems/railties-5.1.2/lib/rails/test_unit/test_requirer.rb:14:in `require'
app_1  |    from /app/vendor/bundle/ruby/2.5.0/gems/railties-5.1.2/lib/rails/test_unit/test_requirer.rb:14:in `block in require_files'
app_1  |    from /app/vendor/bundle/ruby/2.5.0/gems/railties-5.1.2/lib/rails/test_unit/test_requirer.rb:13:in `each'
app_1  |    from /app/vendor/bundle/ruby/2.5.0/gems/railties-5.1.2/lib/rails/test_unit/test_requirer.rb:13:in `require_files'
app_1  |    from /app/vendor/bundle/ruby/2.5.0/gems/railties-5.1.2/lib/rails/test_unit/minitest_plugin.rb:94:in `plugin_rails_init'
app_1  |    from /app/vendor/bundle/ruby/2.5.0/gems/minitest-5.11.3/lib/minitest.rb:81:in `block in init_plugins'
app_1  |    from /app/vendor/bundle/ruby/2.5.0/gems/minitest-5.11.3/lib/minitest.rb:79:in `each'
app_1  |    from /app/vendor/bundle/ruby/2.5.0/gems/minitest-5.11.3/lib/minitest.rb:79:in `init_plugins'
app_1  |    from /app/vendor/bundle/ruby/2.5.0/gems/minitest-5.11.3/lib/minitest.rb:130:in `run'
app_1  |    from /app/vendor/bundle/ruby/2.5.0/gems/railties-5.1.2/lib/rails/test_unit/minitest_plugin.rb:77:in `run'
app_1  |    from /app/vendor/bundle/ruby/2.5.0/gems/minitest-5.11.3/lib/minitest.rb:63:in `block in autorun'
db_1   | 2020-02-09T16:53:57.100166Z 0 [Note] InnoDB: Dumping buffer pool(s) to /var/lib/mysql/ib_buffer_pool
db_1   | 2020-02-09T16:53:57.100642Z 0 [Note] InnoDB: Buffer pool(s) dump completed at 200209 16:53:57
app_1  | Coverage report generated for MiniTest to /app/coverage. 278 / 716 LOC (38.83%) covered.
workspace_app_1 exited with code 1

Aborting on container exit...

The log line that reads Version: '5.7.24' socket: '/var/run/mysqld/mysqld.sock' port: 0 MySQL Community Server (GPL) has called my attention since the port is set to 0 despite me setting it to 3306 (and 3306 being the default). When running locally, the port is correctly set to 3306. Am I misunderstanding how the networking works inside CloudBuild? Is using docker-compose the recommended method to accomplish this?

5
  • Have you tried specifying the port using ports variable inside db? Check out this Stackoverflow post, which will give you an idea. Let me know how it goes. Commented Feb 10, 2020 at 12:54
  • @sllopis Tried it just now and the erroneous behavior continues. Commented Feb 10, 2020 at 16:34
  • That's interesting. I was researching around and came across the following GitHub thread that talks about changing MYSQL_HOST=0.0.0.0 to MYSQL_HOST=db. It has worked for some people. Commented Feb 11, 2020 at 16:50
  • Hi @Paulo, did issue is solved? If not, could you add more information? Commented Feb 18, 2020 at 8:42
  • @Juancki I believe I figured out the problem! Still workin on a solution though. The MySQL container sometimes restarts after setting up the DB (seems to be part of its normal process). However, its status (which is used by the depends_on) is set before restarting. Which means that the app may try to connect to the DB while it is restarting. Commented Mar 4, 2020 at 18:24

1 Answer 1

0

The issue was related to a race condition due to the docker-compose depends_on attribute waiting only for a container (in this case db) to be ready but having no guarantee on the actual code in the container (in this case a MySQL instance) running.

The solution was to add the script wait-for-mysql.sh shown below:

#!/bin/sh
# wait-for-mysql.sh

set -e

host="$1"
shift
cmd="$@"

until mysql -h "db" -u "root" --connect_timeout 1 -e "\q"; do
  >&2 echo "MySQL is unavailable - sleeping for 1 second"
  sleep 1
done

>&2 echo "MySQL is up - executing command"
exec $cmd

Then I edited the docker-compose.test.yml as shown below:

version: '3.4'

services:
  app:
    image: gcr.io/${PROJECT_ID}/<REDACTED>/test:${SHORT_SHA}
    command: ["./scripts/wait-for-mysql.sh", "&&", "bundle", "exec", "rails", "t"]
    ports:
      - 3000:3000
    depends_on:
      - db
    environment:
      - TEST_DATABASE_URL=mysql2://root@db/sales_test
  db:
    image: mysql:5.7.24
    ports:
      - 3306:3306
    environment:
      - MYSQL_ALLOW_EMPTY_PASSWORD=true
      - MYSQL_DATABASE=<REDACTED>

networks:
  default:
      external:
          name: cloudbuild
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.