1

I am trying to launch a docker MySQL container with an initialization script. My Dockerfile looks like this:

FROM node:11
WORKDIR /usr/src/app
COPY ./init.sql /docker-entrypoint-initdb.d/
COPY . .
RUN npm install
ENV PORT=8000
EXPOSE ${PORT}
CMD [ "npm", "start" ]

And I have my init.sql file in two places, the home directory as well as the docker-entrypoint-initdb.d/ folder. The init.sql file only has create table and insert statements. I'm running my Docker commands in this order:

//start network
docker network create --driver bridge mysql-net

//launch docker mysql container with init script to create tables & data
docker run -d --name mysql-server --network mysql-net -p 3306:3306 -e "MYSQL_ROOT_PASSWORD=rootpassword" -e "MYSQL_DATABASE=yelplikeapp" -e "MYSQL_USER=yelplikeapp" -e "MYSQL_PASSWORD=hunter2" -v "/c/Users/marti/Documents/projectFolder:/docker-entrypoint-initdb.d" mysql:5

launching the mysql-server container works initially, but after a couple seconds when I run 'docker ps -a'I can see that it exits as soon as it starts:

enter image description here

Why is my mysql-server container exiting? I've been trying to go into the container and view the database to verify that all the tables were created successfully. But I was having trouble doing that and think that this is the root of the problem.

For transparencies sake I'll post my full init.sql file: When I check the logs for mysql-server, there's an error message saying ERROR 1050 (42S01) at line 1: Table 'users' already exists

drop database yelplikeapp;
create database yelplikeapp;
use yelplikeapp;

SET FOREIGN_KEY_CHECKS = 0;
DROP TABLE IF EXISTS users;
DROP TABLE IF EXISTS businesses;
DROP TABLE IF EXISTS photos;
DROP TABLE IF EXISTS reviews;
SET FOREIGN_KEY_CHECKS = 1;

CREATE TABLE users (
  id MEDIUMINT NOT NULL AUTO_INCREMENT,
  username VARCHAR(255) NOT NULL,  
  PRIMARY KEY (id)
);

INSERT INTO users(id, username) VALUES 
( NULL, 'brungoTungis' ),
( NULL, 'kramer' ),
( NULL, 'elaine');

CREATE TABLE businesses (
  id MEDIUMINT NOT NULL AUTO_INCREMENT,
  ownerid MEDIUMINT NOT NULL,
  name VARCHAR(255) NOT NULL,
  address VARCHAR(255) NOT NULL,
  city VARCHAR(255) NOT NULL,
  state VARCHAR(255) NOT NULL,
  zip VARCHAR(255) NOT NULL,
  phone VARCHAR(255) NOT NULL,
  category VARCHAR(255) NOT NULL,
  subcategory VARCHAR(255) NOT NULL,
  website VARCHAR(255),
  email VARCHAR(255),
  PRIMARY KEY (id),
  FOREIGN KEY (ownerid) REFERENCES users(id) ON DELETE CASCADE
);

INSERT INTO businesses(id, ownerid, name, address, city, state, zip, phone, category, subcategory, website, email) VALUES ( NULL, 1, 'taco bell', '1157th ave nw', 'corvallis', 'or', '98023', '423-232-1313', 'fastfood', 'mexican', 'www.tacobell.com', '[email protected]' ), ( NULL,  1, 'kfc', '1158th ave nw', 'seattle', 'wa', '98223', '423-232-993', 'fastfood', 'chicken', NULL, NULL );

CREATE TABLE photos (
  id MEDIUMINT NOT NULL AUTO_INCREMENT,
  userid MEDIUMINT NOT NULL,
  businessid MEDIUMINT NOT NULL,
  caption VARCHAR(255),
  PRIMARY KEY (id),
  FOREIGN KEY (userid) REFERENCES users(id),
  FOREIGN KEY (businessid) REFERENCES businesses(id) ON DELETE CASCADE
);

INSERT INTO photos(id, userid, businessid, caption) VALUES 
( NULL, 2, 1, 'coolcaption' ),
( NULL, 3, 2, NULL ),
( NULL, 3, 1, 'badcaption');

CREATE TABLE reviews (
  id MEDIUMINT NOT NULL AUTO_INCREMENT,
  userid MEDIUMINT NOT NULL,
  businessid MEDIUMINT NOT NULL,
  dollars MEDIUMINT NOT NULL,
  stars MEDIUMINT NOT NULL,
  review VARCHAR(255),
  PRIMARY KEY (id),
  FOREIGN KEY (userid) REFERENCES users(id),
  FOREIGN KEY (businessid) REFERENCES businesses(id)
);

INSERT INTO reviews(id, userid, businessid, dollars, stars, review) VALUES 
( NULL, 2, 1, 2, 3, 'mycoolreview' ), ( NULL, 1, 2, 2, 3, NULL );
8
  • 1
    You may try to do "docker logs CONTAINER_ID" to check what failed on MySql container. Commented May 6, 2019 at 20:55
  • Your SQL is certainly not an issue Commented May 6, 2019 at 21:05
  • @ŁukaszGawron checked the logs and found and error saying 'users table already existed', going to try and add 'drop table if exists;' statements to my sqlfile before every create table and re-build my mysql-server container Commented May 6, 2019 at 21:14
  • Not sure why you are copying SQL script to this node container in Dockerfile you provide. Could you explain that? Commented May 6, 2019 at 21:18
  • @ŁukaszGawron I thought that was needed in order to copy my init.sql file into the mysql-server container. Also I updated my post with my new sql code. Even after running it without that copy command I'm still getting a 'users table already exists' error Commented May 6, 2019 at 21:23

2 Answers 2

1

solved it, i removed the copy statement from my dockerfile, and also deleted on of my init.sql files.I had two in my project files, one in my homeRepo/init.sql and homeRepo/docker-entrypoint-initdb.d/

I deleted the homeRepo/init.sql file, re - ran the creation of my docker mysql-server creation, and it worked

Sign up to request clarification or add additional context in comments.

Comments

0

Run the "getenforce" command. If the output says "enforcing", run the command "setenforce 0".

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.