Monday, 3 April 2017

Docker notes

A couple of notes based on my recent efforts to dockerize TV Graph.

1. Environments, etc. created by Docker build stages are only persistant within that stage. That means that something like
RUN . ./venv/bin/activate
RUN pip install -U -r conf/requirements.txt
won't work because the virtual environment is activated in a different shell than pip is run.
For the same reason you can't do
RUN mongod &
RUN mongo < conf/mongo.js
as the MongoDB server won't be running in the same environment as the client.
This, however, will work:
RUN mongod && mongo < conf/mongo.js

2. Speaking of MongoDB, the official Docker image for MongoDB marks Mongo's default /data/db/ folder as a Docker volume, which means any changes done to it during the build stage won't be visible after build, which is an issue if you need to change the database during build for example to create indexes (see above).
One workaround is to force MongoDB use a different folder for data storage by using the --dbpath parameter for mongodb. The downside of it is that of course unless you manually map this folder out of the container, it's contents are going to be lost once the container is deleted.

3. For some odd reason, there appears to be some odd sort of a race condition or something when trying to run a MongoDB script during Docker build. The script I was trying to run was very simple:
db = db.getSiblingDB("tvgraph");
db.createUser({user: "tvgraph", pwd: "tvgraph", roles: ["readWrite"]});
db.tvseries.createIndex({show_title: "text"});
db.tvseries.createIndex({"created_at": 1}, {expireAfterSeconds: 43200});
Yet the last line (ini this case, the TTL index) was never executed. Shifting the lines around, adding more lines, adding blank lines at the end of the file didn't help. What worked is inserting sleep(1000) after every statement to give it time to run. Somehow this worked and unfortunately I don't have time to investigate any further.

No comments:

Post a Comment