简体   繁体   中英

docker mysql container exits after being started

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:

在此处输入图像描述

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', 'tacobell@gmail.gov' ), ( 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 );

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

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

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