0

Shell script:

#!/bin/sh
service mysql start
mysql < /mysql/dockerSql.sql
service mysql stop

Docker file:

FROM mysql:5.6
ADD setup.sh /mysql/setup.sh
ADD dockerSql.sql /mysql/dockerSql.sql
ENTRYPOINT ["sh", "/mysql/setup.sh"]

Docker compose file:

version: '3'

services:  
  nariadi-front:
    container_name: nariadi_frontend_container
    build: ./NariadiFrontEndDocker
    ports: 
        - "5002:80"
    depends_on:
        - nariadi-service

  nariadi-service:
    container_name: nariadi_backend_container
    build: ./Nariadi
    ports: 
        - "5008:5008"
    depends_on: 
        - mysql-docker-nariadi
    #command: python manage.py loaddata -t nariadi_docker -u admin

  mysql-docker-nariadi:
    build: ./docker-mysql
    ports:
        - "3310:3306"
    tty: true
    #command: --default-authentication-plugin=mysql_native_password

Response:

mysql-docker-nariadi_1  | No directory, logging in with HOME=/
mysql-docker-nariadi_1  | ..
mysql-docker-nariadi_1  | [info] MySQL Community Server 5.6.40 is started.
mysql-docker-nariadi_1  | ...
mysql-docker-nariadi_1  | [info] MySQL Community Server 5.6.40 is stopped.
mysql-docker-nariadi_1  | ------_DONEEEEEEEEEEE_------------
nariadi_backend_container | [2018-08-09 10:42:52 +0000] [1] [INFO] Starting gunicorn 19.7.1
nariadi_backend_container | [2018-08-09 10:42:52 +0000] [1] [INFO] Listening at: http://0.0.0.0:5008 (1)
nariadi_backend_container | [2018-08-09 10:42:52 +0000] [1] [INFO] Using worker: sync
nariadi_backend_container | [2018-08-09 10:42:52 +0000] [8] [INFO] Booting worker with pid: 8
nariadi_backend_container | [2018-08-09 10:42:52 +0000] [10] [INFO] Booting worker with pid: 10
nariadi_backend_container | [2018-08-09 10:42:52 +0000] [11] [INFO] Booting worker with pid: 11
nariadi_backend_container | [2018-08-09 10:42:53 +0000] [12] [INFO] Booting worker with pid: 12
nariadi_frontend_container | AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.18.0.2. Set the 'ServerName' directive globally to suppress this message
nariadi_frontend_container | AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.18.0.2. Set the 'ServerName' directive globally to suppress this message
nariadi_frontend_container | [Thu Aug 09 10:42:54.528127 2018] [mpm_event:notice] [pid 1:tid 140035064244096] AH00489: Apache/2.4.33 (Unix) configured -- resuming normal operations
nariadi_frontend_container | [Thu Aug 09 10:42:54.528422 2018] [core:notice] [pid 1:tid 140035064244096] AH00094: Command line: 'httpd -D FOREGROUND'
desktop_mysql-docker-nariadi_1 exited with code 0

When I run docker-compose up, mysql-docker-nariadi service exists with code 0. Shell script is executing and .sql file as well. How can I prevent from exiting?

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • Possible duplicate of [How to add a startup script to a mysql docker container?](https://stackoverflow.com/questions/46482257/how-to-add-a-startup-script-to-a-mysql-docker-container) – David Maze Aug 09 '18 at 13:29
  • That question should answer your actual problem. As a general statement around your more specific problem, you should assume that commands like `service`, `systemctl`, _etc._ just don't work in Docker and find a different way to do whatever the task is. – David Maze Aug 09 '18 at 13:30
  • So, I need to add this volumes in my docker-compose file? And I need to remove "service start" and "service stop"? – Darko Andreev Aug 09 '18 at 14:03

2 Answers2

0

You need to run your image in daemon mode - that's how they do it in the official MySQL image:

ENTRYPOINT ["mysqld"]

In your specific case, you can run "mysqld" at the end of your entrypoint SH file /mysql/setup.sh.

Neekoy
  • 2,325
  • 5
  • 29
  • 48
  • I added "mysqld" at the end of my setup.sh file and it exits again. Here is the response now: https://i.imgur.com/ERgbZYy.png – Darko Andreev Aug 09 '18 at 11:15
  • Can you remove 'service mysql stop' from the shell script? – Neekoy Aug 09 '18 at 11:49
  • I removed and the result is same. Check it please https://i.imgur.com/oxTaIrQ.png – Darko Andreev Aug 09 '18 at 12:37
  • Oh yea - that one. You need to add this to your Dockerfile - "RUN su mysql". Add this above the ENTRYPOINT. – Neekoy Aug 09 '18 at 13:14
  • Exiting again. Same result. I added infinite loop and with this infinite loop it's working now, but it's kind of a "hack", not exactly problem solver – Darko Andreev Aug 09 '18 at 13:59
  • @DarkoAndreev Argh, can you make a GitHub repo with all the files and link me so I can see on my end what's going on without going back and forward in here? – Neekoy Aug 09 '18 at 14:00
  • Can you give me your Skype so we can communicate there? Repo will send you there as well – Darko Andreev Aug 09 '18 at 14:20
  • I've got only Slack and Keybase for comms. Can you create a Keybase account and give me the username? It's a pretty good encrypted chat app. Btw I just noticed you're Bulgarian - I'm too :P – Neekoy Aug 09 '18 at 14:50
  • My username is: darkica - Yes I am, nice to meet you :) – Darko Andreev Aug 09 '18 at 14:55
0

In order to import dump when container is strted you need just add it to docker-compose file using volume directives:

volumes:
  - ./dump.sql:/docker-entrypoint-initdb.d/dump.sql

No need to use ENTRYPOINT. It should look:

mysql-docker-nariadi:
    image: mysql:5.6
    ports:
        - "3310:3306"
    volumes:
      - ./dump.sql:/docker-entrypoint-initdb.d/dump.sql
malyy
  • 859
  • 5
  • 10