drmaa
drmaa

Reputation: 3684

How to import data to mongodb container and creating an image

I spent a lot of time on a task which seems a simple one. I want to run a monogodb container. then copy csv file in to the container or execute a command from docker file to import the csv file into the mongodb database. I tried using RUN, script.sh but nothing is working. Please help. I want to automate the task and when I copy the data, I will make a new image for my automated task.

Here is the Dockerfile I have.

FROM mongo
RUN mkdir -p /app/data
WORKDIR /app/data
COPY gene.csv /app/data
COPY script.sh /app/data
EXPOSE 27017
# CMD ["/app/data/script.sh"]

The script file is

#! /bin/sh 
mongoimport -d Gene -c genes --type csv --file gene.csv --headerline

Upvotes: 1

Views: 6682

Answers (3)

ishak O.
ishak O.

Reputation: 191

Too late, maybe help to others.

    FROM mongo
    RUN mkdir -p /app/data
    WORKDIR /app/data
    COPY gene.csv /app/data
    COPY script.sh /app/data
    CMD ["mongod", "&&", "mongoimport", "-d", "Gene", "-c", "Genes", "--file", "gene.csv", "--headerline"]

Upvotes: 2

drmaa
drmaa

Reputation: 3684

I was able to do that but through another container. Whose job is to insert the data in to the mongodb database container and exit. For more details check the accepted answer here. I changed the JSON file with my csv file and the command which import it, although I found that mongodb works better with json.

Upvotes: 0

johnharris85
johnharris85

Reputation: 18986

Edit: This answer solves a problem, but not the problem, as apparently mongo has to be running for any of this to work. In that case a custom entrypoint is probably the way to go.

Using RUN is the right way, as then when you use CMD you can actually run the Mongo process. However your problem is with your COPY instructions.

COPY gene.csv /app/data
COPY script.sh /app/data

These lines need a / on the end, as right now they say 'copy this file from my host, to be this file in my container'. Whereas you're trying to say 'copy this file from my host, into this folder in my container. They should read as below (but can be simplified even further, see final Dockerfile at the end):

COPY gene.csv /app/data/
COPY script.sh /app/data/

Your script will then run if you put it in a RUN instruction and that layer will be committed into the image, then you can have a CMD at the end to run mongo (or just leave it blank and inherit the default ENTRYPOINT / CMD from the parent (mongo) image. So the full Dockerfile becomes:

FROM mongo
WORKDIR /app/data        # This will be created if it doesn't exist
COPY gene.csv .
COPY script.sh .
RUN chmod +x script.sh && sync && ./script.sh

Upvotes: 1

Related Questions