Docker run script after container start

  bash, docker, oracle

I try to run a script after the container deployment.

Unfortunately currently the script starts as soon as I run the "docker-compose up" command.

This is an image of an Oracle DB, it takes a few minutes to deploy.

Now I enter the container to launch the script to create the DB.

Now I would like to automate this by deploying the DB as soon as the container is deployed.

Any ideas?

Docker-compose :

version : "3"
services:
  oracle:
    build:
      context: .
      dockerfile: ./Dockerfile
    ports:
      - "11521:1521"
    environment:
      - ORACLE_PWD=testing12345
    volumes:
    - ./:/var/www/html

Dockerfile :

FROM oracle/database:18.4.0-xe


COPY deployDb.sh /usr/local/bin/deployDb.sh
RUN chmod +x /usr/local/bin/deployDb.sh

CMD ["/bin/bash", "/usr/local/bin/deployDb.sh"]

Script :

#!/bin/bash

CONNT_ADMIN=system/[email protected] #your env
SQL_FILE_DATA=./testdata_SO-Emballage.sql
SQL_FILE_DB=./crebas_SO-Emballage.sql

sqlplus -s ${CONNT_ADMIN} <<EOF

ALTER SESSION SET "_ORACLE_SCRIPT"=true;
DROP USER so_emballage cascade;
CREATE USER so_emballage IDENTIFIED BY so_emballage;
GRANT connect, resource to so_emballage;

@${SQL_FILE_DB}
@${SQL_FILE_DATA}

EOF

Return :

Creating network "mspr_default" with the default driver
Building oracle
Step 1/4 : FROM oracle/database:18.4.0-xe
 ---> 48fc445cf732
Step 2/4 : COPY deployDb.sh /usr/local/bin/deployDb.sh
 ---> 00698cc72614
Step 3/4 : RUN chmod +x /usr/local/bin/deployDb.sh
 ---> Running in 8d335146f651
Removing intermediate container 8d335146f651
 ---> 7ff62847fca6
Step 4/4 : CMD ["/bin/bash", "/usr/local/bin/deployDb.sh"]
 ---> Running in 6edda8873fa4
Removing intermediate container 6edda8873fa4
 ---> d9222babdb0a

Successfully built d9222babdb0a
Successfully tagged mspr_oracle:latest
Creating mspr_oracle_1 ... done
Attaching to mspr_oracle_1
oracle_1  | ERROR:
oracle_1  | ORA-12541: TNS:no listener
oracle_1  | 
oracle_1  | 
oracle_1  | ERROR:
oracle_1  | ORA-12547: TNS:lost contact
oracle_1  | 
oracle_1  | 
oracle_1  | SP2-0306: Invalid option.
oracle_1  | Usage: CONN[ECT] [{logon|/|proxy} [AS {SYSDBA|SYSOPER|SYSASM|SYSBACKUP|SYSDG|SYSKM|SYSRAC}] [edition=value]]
oracle_1  | where <logon> ::= <username>[/<password>][@<connect_identifier>]
oracle_1  |       <proxy> ::= <proxyuser>[<username>][/<password>][@<connect_identifier>]
oracle_1  | SP2-0157: unable to CONNECT to ORACLE after 3 attempts, exiting SQL*Plus
mspr_oracle_1 exited with code 1

Source: Docker Questions

LEAVE A COMMENT