diff --git a/Dockerfile b/Dockerfile index 040a5e6..89ebf1d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -21,7 +21,7 @@ RUN apt-get update && apt-get install -y \ rm -rf /var/lib/apt/lists/* RUN export uid=1000 gid=1000 && \ - mkdir -p /home/ubuntu && \ + mkdir -p /home/ubuntu && mkdir /workspace && \ echo "ubuntu:x:${uid}:${gid}:Developer,,,:/home/ubuntu:/bin/bash" >> /etc/passwd && \ echo "ubuntu:x:${uid}:" >> /etc/group && \ echo "ubuntu ALL=(ALL) NOPASSWD: ALL" > /etc/sudoers.d/ubuntu && \ @@ -29,9 +29,11 @@ RUN export uid=1000 gid=1000 && \ chown ${uid}:${gid} -R /home/ubuntu ENV HOME /home/ubuntu +ENV WORKSPACE /workspace ENV CDRIVER_VERSION ${DRIVER_C_VERSION} -ENV CPPDRIVER_VERSION ${DRIVER_VERSION} ENV LD_LIBRARY_PATH /usr/local/lib +ENV DRIVER_VERSION ${DRIVER_VERSION} +ENV MONGODB_URI ${MONGODB_URI} WORKDIR ${HOME} @@ -46,10 +48,10 @@ RUN cd ${HOME}/mongo-c-driver-${CDRIVER_VERSION} && \ RUN cd ${HOME} -RUN wget https://github.com/mongodb/mongo-cxx-driver/archive/r${CPPDRIVER_VERSION}.tar.gz && \ - tar -xzf r${CPPDRIVER_VERSION}.tar.gz +RUN wget https://github.com/mongodb/mongo-cxx-driver/archive/r${DRIVER_VERSION}.tar.gz && \ + tar -xzf r${DRIVER_VERSION}.tar.gz -RUN cd ${HOME}/mongo-cxx-driver-r${CPPDRIVER_VERSION}/build && \ +RUN cd ${HOME}/mongo-cxx-driver-r${DRIVER_VERSION}/build && \ cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr/local -DBUILD_VERSION=0.0.1 -DCMAKE_PREFIX_PATH=/usr/local .. && \ make EP_mnmlstc_core && \ make && make install @@ -61,6 +63,6 @@ RUN chown -R ubuntu ${HOME}/cxx && chmod -R 750 ${HOME}/cxx USER ubuntu -WORKDIR ${HOME}/cxx +WORKDIR ${WORKSPACE}/cxx -CMD ["/bin/bash"] +ENTRYPOINT ["/bin/bash", "-c"] diff --git a/README.md b/README.md index 82974df..1ebb1d8 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,29 @@ Have Docker running on your machine. You can download and install from: https:// In order to execute the code example, you need to specify `MONGODB_URI` environment variable to connect to a MongoDB cluster. If you don't have any you can create one by signing up [MongoDB Atlas Free-tier M0](https://docs.atlas.mongodb.com/getting-started/). -## Build Steps + +## Execution Steps + +1. Build Docker image with a tag name. Within the top level directory execute: + ``` + docker build . -t start-cxx + ``` + This will build a docker image with a tag name `start-cxx`. + +2. Execute the helper shell script followed by the MongoDB URI that you would like to connect to. + ``` + ./get-started.sh "mongodb+srv://usr:pwd@example.mongodb.net/dbname?retryWrites=true" + ``` + + To use a different driver version, specify the driver version after the MongoDB URI. For example: + ``` + ./get-started.sh "mongodb+srv://usr:pwd@example.mongodb.net/dbname?retryWrites=true" 3.6.0 + ``` + + +## Alternative Execution Steps (without helper) + +#### Build Steps 1. Build Docker image with a tag name. Within this directory execute: * To use the default driver version and specify `MONGODB_URI`: @@ -32,12 +54,12 @@ In order to execute the code example, you need to specify `MONGODB_URI` environm 2. Run the Docker image by executing: ``` - docker run --tty --interactive --hostname cxx start-cxx + docker run --tty --interactive --hostname cxx start-cxx bash ``` The command above will run a `start-cxx` tagged Docker image. Sets the hostname as `cxx`. -## Execution Steps +#### Execution 1. Compile and execute the code example by following below steps: * `c++ --std=c++11 getstarted.cpp -o getstarted $(pkg-config --cflags --libs libmongocxx)` diff --git a/get-started.sh b/get-started.sh new file mode 100755 index 0000000..1da8dcd --- /dev/null +++ b/get-started.sh @@ -0,0 +1,14 @@ +#!/bin/bash +MONGODB_URI=${1} +if [ -z ${MONGODB_URI} ] +then + read -p "MONGODB URI (Required): " MONGODB_URI +fi + +DRIVER_VERSION=${2:-3.6.2} +echo "Executing ... " +docker run --rm -e MONGODB_URI=${MONGODB_URI} \ + -v "$(pwd)":/workspace \ + -w /workspace/cxx start-cxx \ + "c++ --std=c++11 ./getstarted.cpp -o getstarted -I/usr/local/include/mongocxx/v_noabi -I/usr/local/include/bsoncxx/v_noabi -L/usr/local/lib -lmongocxx -lbsoncxx; \ + ./getstarted"