问题描述
我正在尝试构建一个 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 容器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!