1

I am uploading an environment with docker-compose but I want to automatically create a table in docker-compose. However it is not working.

docker-compose

mysql:
        image: mysql:5.7
        stdin_open: true
        tty: true
        container_name: mysql
        restart: always
        ports:
          - "3306:3306"
        environment:
          - MYSQL_ROOT_PASSWORD=root
          - MYSQL_DATABASE=one
        networks:
          - network1
        volumes:
          - db-data:/var/lib/mysql
          - ./dump:/docker-entrypoint-initdb.d

/dump/initdb.sql

create database one;
use one;
CREATE TABLE IF NOT EXISTS `user` (
  `user_id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(255) DEFAULT NULL,
  `first_name` varchar(50) DEFAULT NULL,
  `last_name` varchar(50) DEFAULT NULL,
  `gender` varchar(10) DEFAULT NULL,
  `password` varchar(50) DEFAULT NULL,
  `status` tinyint(10) DEFAULT NULL,
  PRIMARY KEY (`user_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=10001 ;

1 Answer 1

4

Try using a Dockerfile:

FROM mysql:5.7

COPY initdb.sql /docker-entrypoint-initdb.d/initdb.sql

Make sure this is in the same directory as the initdb.sql script.

Now in your compose:

mysql:
    build: ./dump
    stdin_open: true
    tty: true
    container_name: mysql
    restart: always
    ports:
      - "3306:3306"
    environment:
      - MYSQL_ROOT_PASSWORD=root
      - MYSQL_DATABASE=one
    networks:
      - network1
    volumes:
      - db-data:/var/lib/mysql
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.