简体   繁体   中英

Create docker container, execute a script and delete the container

I want a command that, in first, will create a docker container, then execute a sql command on my database and then delete the container when it's done.

For the moment I have this Dockerfile:

FROM ubuntu:18.04

# copy database-file.sh
WORKDIR /database-file
RUN mkdir database-file
COPY * /database-file/

# Update and install mysql-client
RUN apt-get update && apt-get -y install mysql-client

# Prepare database-prepare script to be use
RUN chmod +x ./database-prepare.sh

VOLUME /app/log

# Exec my script
ENTRYPOINT ["./database-prepare.sh"]

I took an Ubuntu image, I don't know if it's the best but for the moment, it's ok i think. I just want to test.

So there is my script:

### Global ./database-prepare.sh ###

#!/bin/bash

# Get service name by Docker secret.
service=$(cat /run/secrets/<society_name>_DATABASE_PREPARE__SERVICE)
# Format the service name.
serviceName=${service//./-}

# Clone the repo of my service
git clone --recurse-submodules git@bitbucket.org:<society_name>/<society_name>.binding.$service.git /$serviceName

# CD in the right folder
cd /$serviceName/build/

# Prepare my script and execute it
chmod +x database-prepare.sh
./database-prepare.sh

Then each service have his own database-prepapre.sh that will create a new user in my database:

### Service ./database-prepare.sh ###

#!/bin/bash

# Get password with Docker secret
<society_name>_MSQL_ROOT_PWD=$(cat /run/secrets/<society_name>_MYSQL_PWD) # My Mysql password
<society_name>_BINDING_ABEEWAY_GENERIC_DBS__<society_name>_DATA__PWD=$(cat /run/secrets/<society_name>_BINDING_ABEEWAY_GENERIC_DBS__<society_name>_DATA__PWD) # My password for this user

mysql -h <society_name>-sql-server.mysql.database.azure.com --user='administrateur@<society_name>-sql-server' --ssl --password=$<society_name>_MYSQL_ROOT_PWD << EOF
DROP USER IF EXISTS '<society_name>.binding.abeeway.generic'@'%';
CREATE USER '<society_name>.binding.abeeway.generic'@'%' IDENTIFIED BY '$<society_name>_BINDING_ABEEWAY_GENERIC_DBS__<society_name>_DATA__PWD';

GRANT SELECT, INSERT ON <society_name>_data.ms_gpsposition TO '<society_name>.binding.abeeway.generic'@'%';
GRANT SELECT, INSERT ON <society_name>_data.ms_log TO '<society_name>.binding.abeeway.generic'@'%';
GRANT SELECT, INSERT ON <society_name>_data.ms_temperature TO '<society_name>.binding.abeeway.generic'@'%';
EOF

And know here is my issue:

./database-prepare.sh: line 6: unexpected EOF while looking for matching `"'
./database-prepare.sh: line 13: syntax error: unexpected end of file

So my error is in the file of my Service, where I have "End Of File" text. I try to execute this code, directly in a running container and there is no problem with the EOF... I don't really know where I make a mistake, I'm a novice in Dockerfile, and I think my mistake it's maybe caused by the Image I took, or I maybe I forgot to install a useful tool to make my script work correctly. Or just my script isn't good.. I don't know.

I hope you have enough information to help me, tell me if you need more. Thank you very much !

Ps: If you know how to delete my service after he executed his command, I would be grateful ! I work on swarm cluster

I finally find a solution, I rewrite my Dockerfile with another image:

FROM alpine:3.7 # <----- Changed

WORKDIR /database-file
RUN mkdir database-file
COPY * /database-file/

RUN apt-get update && apt-get -y install mysql-client # <----- Deleted
RUN apk add mysql-client # <----- Added
RUN apk add git # <----- Added
RUN apk add openssh-client # <----- Added

RUN chmod +x ./database-prepare.sh
VOLUME /app/log

ENTRYPOINT ["sh","./database-prepare.sh"] # <----- Changed

I think the real problem was my ENTRYPOINT, after reflection I think my old ENTRYPOINT doesn't call correctly my script.

I hope this will help another person.

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