简体   繁体   中英

Prevent scripts from exiting in docker container

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?

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 .

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

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