如何在构建另一个容器的过程中填充Mysql

如何在构建另一个容器的过程中填充Mysql

本文介绍了如何在构建另一个容器的过程中填充Mysql docker容器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建一个docker-compose文件,该文件会自动为我的应用程序构建一个容器,并为一个mysql-container存储所有数据.

I am trying to build a docker-compose file that automatically builds a container for my application and a mysql-container that stores all the data.

在我的应用程序的dockerfile中,我有一个脚本,该脚本设置了应用程序运行所必需的所有数据库表和预设值.是否有可能以某种方式将mysql容器的引用传递给myapplication,使其在构建myapplication时可以访问mysql容器?

Within my dockerfile for my application I have a script, that sets up all the database tables and preset values, that are necessary for the application to run. Is it possible to somehow pass the reference of the mysql container to myapplication that during build of myapplication it can access the mysql container?

如果是的话,我该怎么办?如果没有,那该怎么办?

If so, how can I do that, and if not, how should it be done then?

docker-compose.yml

docker-compose.yml

mysql:
  image: mysql/mysql-server
  ports:
    - "3306:3306"
  environment:
    MYSQL_USER: root
    MYSQL_ROOT_PASSWORD: mysql-root-password

myapplication:
  command: /home/myapplication/startup.sh
  build: ./..
  dockerfile: ./dockerfiles/myapplication
  ports:
    - "443:443"
    - "8090:8090"
  links:
    - mysql:docker-mysql
  environment:
    MYSQL_SERVER: docker-mysql

myapplication-dockerfile

myapplication-dockerfile

FROM ubuntu:latest

EXPOSE 443

ENV applicationPath="/home/application"

COPY . ${applicationPath}

#Execute installationScript
RUN /bin/bash -c "./${applicationPath}/Tools/install_all.sh"

推荐答案

执行 docker-compose build 命令时,docker将构建定义的每个服务的映像在docker-compose.yml文件中(在您的情况下为 mysql myapplication ),因此这意味着将不运行任何容器.

When executing the docker-compose build command, docker will just build the images of each service defined in the docker-compose.yml file (in your case mysql and myapplication), so that means that no container will be run.

一旦执行 docker-compose run 命令,docker将针对先前构建的映像运行一个容器,一个用于 mysql myapplication .现在两者都建立起来了,它们可以彼此交互了.

Once you execute the docker-compose run command, docker will run a container per image built previously, one for the mysql and another for the myapplication. Now that both are up, they can interact with each other.

在运行容器时以及在执行startup.sh脚本之前,我将只执行install_all.sh脚本(猜测它包含数据库设置).请注意,只有在启动 mysql 容器后才能访问它,这意味着只有在执行 docker-compose up 后才能访问.

I will just execute the install_all.sh script (guessing that it contains the database setup) when running the container and before the startup.sh script is executed. Note that the mysql container will only be accessible when it is up, that mean only after the docker-compose up execution.

docker-compose.yml

...
myapplication:
  command: bash -c "/home/myapplication/Tools/install_all.sh && /home/myapplication/startup.sh"
...

此外,请注意,您的 myapplication 容器必须已安装mysql-client才能与mysql服务器通信.只需将以下行添加到您的Dockerfile即可安装它:

Also, note that your myapplication container has to have installed a mysql-client to be able to communicate with the mysql server. Just adding the below line to your Dockerfile will install it:

RUN apt-get update && apt-get install mysql-client -y

在您的 install_all.sh脚本中,您可以像下面这样对mysql进行一些调用(创建数据库):

And in your install_all.sh script, you can have some calls to mysql like below (that create a database):

#!/bin/bash
mysql -h $DOCKER_MYSQL_PORT_3306_TCP_ADDR -P $DOCKER_MYSQL_PORT_3306_TCP_PORT -u $DOCKER_MYSQL_ENV_MYSQL_USER -p $DOCKER_MYSQL_ENV_MYSQL_ROOT_PASSWORD -e "CREATE DATABASE IF NOT EXISTS new_database;"

这篇关于如何在构建另一个容器的过程中填充Mysql docker容器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 15:20