How to fill a Mysql docker container while building another container?

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

There is a script in my docker file for my application that sets all the database tables and predefined values ​​needed to run the application. Is it possible to somehow pass the mysql container reference to myapplication so that during myapplication build it can access the mysql container?

If so, how can I do this, and if not, how can I do this?

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

FROM ubuntu:latest

EXPOSE 443

ENV applicationPath="/home/application"

COPY . ${applicationPath}

#Execute installationScript
RUN /bin/bash -c "./${applicationPath}/Tools/install_all.sh"
+4
source share
1

docker-compose build , docker-compose.yml( mysql myapplication), , .

docker-compose run docker , , mysql myapplication. , , .

, ?

install_all.sh script (, ) startup.sh script. , mysql , , docker-compose up.

-compose.yml

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

, , myapplication mysql- mysql. Dockerfile, :

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

install_all.sh script mysql, ( ):

#!/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;"

, - , .

+3

Source: https://habr.com/ru/post/1627948/


All Articles