简体   繁体   中英

Docker Compose Multiple Containers

I have a Python script which connects to MySql and inserts data into a database.That is the one container, I want to build. I want to build another container which will have a Python script which will connect to the database of the first container and execute some queries. I am trying to follow the documentation of Docker, however I find it difficult to make the proper yml file. Any guidance will be very helpful.

It depends on how complex is what you want to make, but the docker-compose.yml file should look similar to this:

version: '3'
services:

  my_database:
    image: mysql
    [... MySQL configs]


  my_python_container:
    build: .
    depends_on:
      - my_database
    links:
      - my_database

I have not knowledge about the configuration of the MySQL database so I left that part blank ( [... MySQL configs] ). The my_python_conainer is in a Dockerfile in the same folder, similar to:

FROM python
COPY script.py script.py
CMD python script.py

This should be enough to get the connection, but you have to consider in your program that the mysql hostname will be the name given to the container.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM