在扩展Docker容器之前创建数据库

时间:2019-05-01 09:28:17

标签: postgresql docker docker-compose gitlab devops

我有一个docker compose文件,其中包含一个应用程序容器和一个postgres容器。

该应用程序取决于postgres容器。

我想在应用容器开始连接到postgres并运行脚本以在其中创建数据库之前。如何在docker compose中做到这一点?有什么办法可以在依赖项的中间运行某些东西?

或者当我通过ssh连接到服务器以在部署阶段上下运行docker compose时,还是应该这样做。这样,我还需要以某种方式在服务器中安装psql。我希望第一个。

您能指出我正确的方向吗?

2 个答案:

答案 0 :(得分:2)

  

我希望在应用容器开始连接到postgres并运行脚本以在其中创建数据库之前

您可以通过将(SQL)脚本安装到Postgres容器的from __future__ import print_function, division from keras.datasets import mnist from keras.layers import Input, Dense, Reshape, Flatten, Dropout, multiply from keras.layers import BatchNormalization, Activation, Embedding, ZeroPadding2D from keras.layers.advanced_activations import LeakyReLU from keras.layers.convolutional import UpSampling2D, Conv2D from keras.models import Sequential, Model from keras.optimizers import Adam import matplotlib.pyplot as plt import numpy as np class CGAN(): def __init__(self,width=50, height=50, channels=1): # Input shape self.width = width self.height = height self.img_rows = 50 self.img_cols = 50 self.channels = 1 self.img_shape = (self.img_rows, self.img_cols, self.channels) self.num_classes = 10 self.latent_dim = 100 optimizer = Adam(0.0002, 0.5) # Build and compile the discriminator self.discriminator = self.build_discriminator() self.discriminator.compile(loss=['binary_crossentropy'], optimizer=optimizer, metrics=['accuracy']) # Build the generator self.generator = self.build_generator() # The generator takes noise and the target label as input # and generates the corresponding digit of that label noise = Input(shape=(self.latent_dim,)) label = Input(shape=(1,)) img = self.generator([noise, label]) # For the combined model we will only train the generator self.discriminator.trainable = False # The discriminator takes generated image as input and determines validity # and the label of that image valid = self.discriminator([img, label]) # The combined model (stacked generator and discriminator) # Trains generator to fool discriminator self.combined = Model([noise, label], valid) self.combined.compile(loss=['binary_crossentropy'], optimizer=optimizer) def build_generator(self): model = Sequential() model.add(Dense(256, input_dim=self.latent_dim)) model.add(LeakyReLU(alpha=0.2)) model.add(BatchNormalization(momentum=0.8)) model.add(Dense(512)) model.add(LeakyReLU(alpha=0.2)) model.add(BatchNormalization(momentum=0.8)) model.add(Dense(1024)) model.add(LeakyReLU(alpha=0.2)) model.add(BatchNormalization(momentum=0.8)) model.add(Dense(np.prod(self.img_shape), activation='tanh')) model.add(Reshape(self.img_shape)) model.summary() noise = Input(shape=(self.latent_dim,)) label = Input(shape=(1,), dtype='int32') label_embedding = Flatten()(Embedding(self.num_classes, self.latent_dim)(label)) model_input = multiply([noise, label_embedding]) img = model(model_input) return Model([noise, label], img) 文件夹中(参见the image documentation –初始化脚本)来实现类似的目的。这样,您的应用程序不必连接到PostgreSQL容器,但是容器可以自己完成所有操作。

不过,这里的一个常见问题是,您的应用可能会在准备好接受连接且应用崩溃之前立即尝试连接到PostgreSQL。您可以通过以下方法来避免这种情况:在您的应用程序中实现一些try-catch机制以等待PostgreSQL准备就绪,或者更改应用程序容器的入口点,即/docker-entrypoint-initdb.d

答案 1 :(得分:1)

通过声明环境变量POSTGRES_DB,您可以告诉Postgres在启动时创建一个空数据库并使用env vars名称。

version: '3.5'

services:

  db:
    image: postgres:11.2-alpine
    restart: always
    environment:
      POSTGRES_DB: myDB
      POSTGRES_PASSWORD: myPassword
      POSTGRES_USER: myUser