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'
        restart: always
        build: .path/to/docker/file
            - "5000:5000"
            - "8888:8888"
            - db
            - db
            - ./path/to/file/:/app:z
            - python
            - -u
            - /app/

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

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

  • Install MySQL connector/J in Docker container
  • Docker compose can not create container for postgresql and redis
  • Linux dev environment in osx (docker as mv or any other)
  • Unable to mount specific paths as volumes
  • unable to find the /etc/default/docker with latest docker 17.03.0-ce
  • docker run registry error 301 response missing Location header
  • echo '[mysqld]' >> /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?

  • Can't kill supervisord inside of docker container
  • docker executable file not found in $PATH
  • Using the host ip in docker-compose
  • Redis sentinel docker image / Dockerfile
  • AWS Multicontainer Docker using PHP and NGinx to install Laravel
  • Docker Compose with Rails hanging up on command
  • 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 /tmp/
    CMD ["/tmp/"]

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

    echo '[mysqld]' >> /etc/mysql/conf.d/mysql.cnf 

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

        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.