How to run a command after creating a container using yml file

I have very limited experience with Docker, and having an issue, so I have the following yml file:

version: '2'
services:
    python:
        restart: always
        build: .path/to/docker/file
        ports:
            - "5000:5000"
            - "8888:8888"
        links:
            - db
        depends_on:
            - db
        volumes:
            - ./path/to/file/:/app:z
        entrypoint:
            - python
            - -u
            - /app/run.py

    db:
        image: mysql:latest
        environment:
            MYSQL_ROOT_PASSWORD: xxyyzz
            MYSQL_DATABASE: database_name
            MYSQL_USER: user_name
            MYSQL_PASSWORD: xxyyzz
        volumes:
            - ./Dump.sql:/db/Dump.sql:z
            - ./Dump_Test.sql:/db/Dump_Test.sql:z
            - ./big_fc.sql:/db/big_fc.sql:z
        ports:
            - "3306:3306"

I need to run the following commands for the second container db:

  • How can I test with serverspec that Jenkins is running in a jenkins docker container?
  • Docker-machine Port Forwarding on Windows not working
  • I got an error when I try to delete an image in docker
  • Self-hosted alternative to hub.docker.com?
  • What is the `<<-EOSQL` code block in Bash when running SQL?
  • docker-compose up -d error
  • echo '[mysqld]' >> /etc/mysql/conf.d/mysql.cnf 
    echo 'sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' >> /etc/mysql/conf.d/mysql.cnf 
    

    The point is that I want to run these commands just once as when you provide a docker file for the first container python. How can I achieve this?

  • Redis Docker connection refused
  • How do I mount a virtual machine folder on a docker container? [closed]
  • Docker cannot run hello-world from client
  • Multiple storage drivers panic in private registry
  • When Marathon run all docker apps on same host the docker service stop
  • User and file permission configuration in Docker containers (docker-compose version 3)
  • One Solution collect form web for “How to run a command after creating a container using yml file”

    My first advice is to build your own image with your own Dockerfile. In this case you will do it once and all the containers will have the same config.

    Dockerfile should look like this:

    FROM mysql:latest
    
    COPY create_config.sh /tmp/create_config.sh
    CMD ["/tmp/create_config.sh"]
    

    Create create_config.sh in the same dir as your Dockerfile and put your required commands there:

    #!/bin/bash
    
    echo '[mysqld]' >> /etc/mysql/conf.d/mysql.cnf 
    echo 'sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' >> /etc/mysql/conf.d/mysql.cnf
    

    Now you can use it in your docker-compose.yml:

    db:
        context: .
        dockerfile: Dockerfile
    

    Keep in mind, that context is the path to the dir where your new Dockerfile is.

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.