diff --git a/Dockerfile b/Dockerfile index 89ebf1d..e3e9782 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,68 +1,39 @@ -FROM ubuntu:20.04 +FROM alpine:3.13 -ARG DEBIAN_FRONTEND=noninteractive -ARG DRIVER_VERSION=3.6.2 -ARG DRIVER_C_VERSION=1.17.3 -ARG MONGODB_URI +LABEL org.opencontainers.image.source=https://github.com/mongodb-developer/get-started-cxx -RUN apt-get update && apt-get install -y \ - nano \ - sudo \ - build-essential \ - wget \ - cmake \ - git \ - python3.9 \ - python3.9-distutils \ - pkg-config \ - libssl-dev \ - libsasl2-dev && \ - apt-get clean && \ - rm -rf /var/lib/apt/lists/* +ARG DRIVER_VERSION=3.6.2 +ARG DRIVER_C_VERSION=1.17.4 -RUN export uid=1000 gid=1000 && \ - 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 && \ - chmod 0440 /etc/sudoers.d/ubuntu && \ - chown ${uid}:${gid} -R /home/ubuntu +RUN apk add --no-cache wget cmake make git tar gcc g++ musl-dev openssl-dev perl +RUN addgroup -S gsgroup && adduser -S gsuser -G gsgroup -ENV HOME /home/ubuntu -ENV WORKSPACE /workspace ENV CDRIVER_VERSION ${DRIVER_C_VERSION} -ENV LD_LIBRARY_PATH /usr/local/lib +ENV LD_LIBRARY_PATH /usr/local/lib64 ENV DRIVER_VERSION ${DRIVER_VERSION} -ENV MONGODB_URI ${MONGODB_URI} +ENV HOME /home/gsuser -WORKDIR ${HOME} - -RUN wget https://github.com/mongodb/mongo-c-driver/releases/download/${CDRIVER_VERSION}/mongo-c-driver-${CDRIVER_VERSION}.tar.gz && \ +RUN cd ${HOME} && wget https://github.com/mongodb/mongo-c-driver/releases/download/${CDRIVER_VERSION}/mongo-c-driver-${CDRIVER_VERSION}.tar.gz && \ tar xzf mongo-c-driver-${CDRIVER_VERSION}.tar.gz +RUN cd ${HOME} && wget https://github.com/mongodb/mongo-cxx-driver/archive/r${DRIVER_VERSION}.tar.gz && \ + tar -xzf r${DRIVER_VERSION}.tar.gz + +RUN chown -R gsuser ${HOME} && chmod -R 750 ${HOME} + RUN cd ${HOME}/mongo-c-driver-${CDRIVER_VERSION} && \ mkdir cmake-build && \ cd cmake-build && \ - cmake -DENABLE_AUTOMATIC_INIT_AND_CLEANUP=OFF .. && \ + cmake -DENABLE_AUTOMATIC_INIT_AND_CLEANUP=OFF -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr/local -DENABLE_TESTS=OFF .. && \ make && make install -RUN cd ${HOME} - -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${DRIVER_VERSION}/build && \ - cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr/local -DBUILD_VERSION=0.0.1 -DCMAKE_PREFIX_PATH=/usr/local .. && \ + cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr/local -DBUILD_VERSION=0.0.1 -DCMAKE_PREFIX_PATH=/usr/local -DENABLE_TESTS=OFF .. && \ make EP_mnmlstc_core && \ - make && make install - -RUN mkdir ${HOME}/cxx -COPY ./cxx/getstarted.cpp ${HOME}/cxx/getstarted.cpp - -RUN chown -R ubuntu ${HOME}/cxx && chmod -R 750 ${HOME}/cxx - -USER ubuntu + make && make install && \ + for i in `ls /usr/local/lib64/pkgconfig | grep cxx-static.pc`; do sed -i -e 's;libdir=${prefix}/lib;libdir=${prefix}/lib64;g' /usr/local/lib64/pkgconfig/$i; done && \ + apk --update --no-cache del perl -WORKDIR ${WORKSPACE}/cxx +USER gsuser -ENTRYPOINT ["/bin/bash", "-c"] +ENTRYPOINT ["/bin/sh", "-c"] diff --git a/README.md b/README.md index 1ebb1d8..9c19481 100644 --- a/README.md +++ b/README.md @@ -17,59 +17,24 @@ 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/). -## Execution Steps +## Execution Step -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`: - ``` - docker build . -t start-cxx --build-arg MONGODB_URI="mongodb+srv://usr:pwd@example.mongodb.net/dbname?retryWrites=true" - ``` - * To use a different driver version and specify `MONGODB_URI`. For example: - ``` - docker build . -t start-cxx --build-arg DRIVER_VERSION=3.6.0 --build-arg MONGODB_URI="mongodb+srv://usr:pwd@example.mongodb.net/dbname?retryWrites=true" - ``` - This will build a docker image with a tag name `start-cxx`. - As a result of the build, the example code is compiled for the specified driver version and ready to be executed. - -2. Run the Docker image by executing: - ``` - docker run --tty --interactive --hostname cxx start-cxx bash - ``` +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" +``` - The command above will run a `start-cxx` tagged Docker image. Sets the hostname as `cxx`. +## Changing The Driver Version -#### Execution +The Docker image has been built with a specific version. If you would like to use a different version, you could build a new image. Example: -1. Compile and execute the code example by following below steps: - * `c++ --std=c++11 getstarted.cpp -o getstarted $(pkg-config --cflags --libs libmongocxx)` - * `./getstarted` +``` +docker build . -t start-cxx --build-arg DRIVER_VERSION=3.6.0 +``` -From within the docker environment, you can also change the `MONGODB_URI` by changing the environment variable: +This will build a docker image with a tag name `start-cxx`. +Open the helper script `get-started.sh` in an editor of your choice, and change the value of `DOCKER_IMAGE` to `start-cxx` and save the file. Execute the helper script followed by the MongoDB URI. -```sh -export MONGODB_URI="mongodb+srv://usr:pwd@new.mongodb.net/dbname?retryWrites=true" -``` ## Tutorials diff --git a/get-started.sh b/get-started.sh index 1da8dcd..6bdc48e 100755 --- a/get-started.sh +++ b/get-started.sh @@ -1,14 +1,15 @@ #!/bin/bash MONGODB_URI=${1} +DOCKER_IMAGE=ghcr.io/mongodb-developer/get-started-cxx + if [ -z ${MONGODB_URI} ] then read -p "MONGODB URI (Required): " MONGODB_URI -fi +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; \ + -w /workspace/cxx ${DOCKER_IMAGE} \ + "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/lib64 -lmongocxx -lbsoncxx; \ ./getstarted"