Skip to content

Commit aa9ee32

Browse files
authored
GitHub container (#1)
* Updated to use Alpine * Added LABEL and change library location to lib64 * Updated helper script to use GCR image * Tidy up README
1 parent 0c11d8c commit aa9ee32

File tree

3 files changed

+38
-101
lines changed

3 files changed

+38
-101
lines changed

Dockerfile

Lines changed: 21 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,39 @@
1-
FROM ubuntu:20.04
1+
FROM alpine:3.13
22

3-
ARG DEBIAN_FRONTEND=noninteractive
4-
ARG DRIVER_VERSION=3.6.2
5-
ARG DRIVER_C_VERSION=1.17.3
6-
ARG MONGODB_URI
3+
LABEL org.opencontainers.image.source=https://github.com/mongodb-developer/get-started-cxx
74

8-
RUN apt-get update && apt-get install -y \
9-
nano \
10-
sudo \
11-
build-essential \
12-
wget \
13-
cmake \
14-
git \
15-
python3.9 \
16-
python3.9-distutils \
17-
pkg-config \
18-
libssl-dev \
19-
libsasl2-dev && \
20-
apt-get clean && \
21-
rm -rf /var/lib/apt/lists/*
5+
ARG DRIVER_VERSION=3.6.2
6+
ARG DRIVER_C_VERSION=1.17.4
227

23-
RUN export uid=1000 gid=1000 && \
24-
mkdir -p /home/ubuntu && mkdir /workspace && \
25-
echo "ubuntu:x:${uid}:${gid}:Developer,,,:/home/ubuntu:/bin/bash" >> /etc/passwd && \
26-
echo "ubuntu:x:${uid}:" >> /etc/group && \
27-
echo "ubuntu ALL=(ALL) NOPASSWD: ALL" > /etc/sudoers.d/ubuntu && \
28-
chmod 0440 /etc/sudoers.d/ubuntu && \
29-
chown ${uid}:${gid} -R /home/ubuntu
8+
RUN apk add --no-cache wget cmake make git tar gcc g++ musl-dev openssl-dev perl
9+
RUN addgroup -S gsgroup && adduser -S gsuser -G gsgroup
3010

31-
ENV HOME /home/ubuntu
32-
ENV WORKSPACE /workspace
3311
ENV CDRIVER_VERSION ${DRIVER_C_VERSION}
34-
ENV LD_LIBRARY_PATH /usr/local/lib
12+
ENV LD_LIBRARY_PATH /usr/local/lib64
3513
ENV DRIVER_VERSION ${DRIVER_VERSION}
36-
ENV MONGODB_URI ${MONGODB_URI}
14+
ENV HOME /home/gsuser
3715

38-
WORKDIR ${HOME}
39-
40-
RUN wget https://github.com/mongodb/mongo-c-driver/releases/download/${CDRIVER_VERSION}/mongo-c-driver-${CDRIVER_VERSION}.tar.gz && \
16+
RUN cd ${HOME} && wget https://github.com/mongodb/mongo-c-driver/releases/download/${CDRIVER_VERSION}/mongo-c-driver-${CDRIVER_VERSION}.tar.gz && \
4117
tar xzf mongo-c-driver-${CDRIVER_VERSION}.tar.gz
4218

19+
RUN cd ${HOME} && wget https://github.com/mongodb/mongo-cxx-driver/archive/r${DRIVER_VERSION}.tar.gz && \
20+
tar -xzf r${DRIVER_VERSION}.tar.gz
21+
22+
RUN chown -R gsuser ${HOME} && chmod -R 750 ${HOME}
23+
4324
RUN cd ${HOME}/mongo-c-driver-${CDRIVER_VERSION} && \
4425
mkdir cmake-build && \
4526
cd cmake-build && \
46-
cmake -DENABLE_AUTOMATIC_INIT_AND_CLEANUP=OFF .. && \
27+
cmake -DENABLE_AUTOMATIC_INIT_AND_CLEANUP=OFF -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr/local -DENABLE_TESTS=OFF .. && \
4728
make && make install
4829

49-
RUN cd ${HOME}
50-
51-
RUN wget https://github.com/mongodb/mongo-cxx-driver/archive/r${DRIVER_VERSION}.tar.gz && \
52-
tar -xzf r${DRIVER_VERSION}.tar.gz
53-
5430
RUN cd ${HOME}/mongo-cxx-driver-r${DRIVER_VERSION}/build && \
55-
cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr/local -DBUILD_VERSION=0.0.1 -DCMAKE_PREFIX_PATH=/usr/local .. && \
31+
cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr/local -DBUILD_VERSION=0.0.1 -DCMAKE_PREFIX_PATH=/usr/local -DENABLE_TESTS=OFF .. && \
5632
make EP_mnmlstc_core && \
57-
make && make install
58-
59-
RUN mkdir ${HOME}/cxx
60-
COPY ./cxx/getstarted.cpp ${HOME}/cxx/getstarted.cpp
61-
62-
RUN chown -R ubuntu ${HOME}/cxx && chmod -R 750 ${HOME}/cxx
63-
64-
USER ubuntu
33+
make && make install && \
34+
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 && \
35+
apk --update --no-cache del perl
6536

66-
WORKDIR ${WORKSPACE}/cxx
37+
USER gsuser
6738

68-
ENTRYPOINT ["/bin/bash", "-c"]
39+
ENTRYPOINT ["/bin/sh", "-c"]

README.md

Lines changed: 12 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -17,59 +17,24 @@ Have Docker running on your machine. You can download and install from: https://
1717
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/).
1818

1919

20-
## Execution Steps
20+
## Execution Step
2121

22-
1. Build Docker image with a tag name. Within the top level directory execute:
23-
```
24-
docker build . -t start-cxx
25-
```
26-
This will build a docker image with a tag name `start-cxx`.
27-
28-
2. Execute the helper shell script followed by the MongoDB URI that you would like to connect to.
29-
```
30-
./get-started.sh "mongodb+srv://usr:pwd@example.mongodb.net/dbname?retryWrites=true"
31-
```
32-
33-
To use a different driver version, specify the driver version after the MongoDB URI. For example:
34-
```
35-
./get-started.sh "mongodb+srv://usr:pwd@example.mongodb.net/dbname?retryWrites=true" 3.6.0
36-
```
37-
38-
39-
## Alternative Execution Steps (without helper)
40-
41-
#### Build Steps
42-
43-
1. Build Docker image with a tag name. Within this directory execute:
44-
* To use the default driver version and specify `MONGODB_URI`:
45-
```
46-
docker build . -t start-cxx --build-arg MONGODB_URI="mongodb+srv://usr:pwd@example.mongodb.net/dbname?retryWrites=true"
47-
```
48-
* To use a different driver version and specify `MONGODB_URI`. For example:
49-
```
50-
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"
51-
```
52-
This will build a docker image with a tag name `start-cxx`.
53-
As a result of the build, the example code is compiled for the specified driver version and ready to be executed.
54-
55-
2. Run the Docker image by executing:
56-
```
57-
docker run --tty --interactive --hostname cxx start-cxx bash
58-
```
22+
Execute the helper shell script followed by the MongoDB URI that you would like to connect to.
23+
```
24+
./get-started.sh "mongodb+srv://usr:pwd@example.mongodb.net/dbname?retryWrites=true"
25+
```
5926

60-
The command above will run a `start-cxx` tagged Docker image. Sets the hostname as `cxx`.
27+
## Changing The Driver Version
6128

62-
#### Execution
29+
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:
6330

64-
1. Compile and execute the code example by following below steps:
65-
* `c++ --std=c++11 getstarted.cpp -o getstarted $(pkg-config --cflags --libs libmongocxx)`
66-
* `./getstarted`
31+
```
32+
docker build . -t start-cxx --build-arg DRIVER_VERSION=3.6.0
33+
```
6734

68-
From within the docker environment, you can also change the `MONGODB_URI` by changing the environment variable:
35+
This will build a docker image with a tag name `start-cxx`.
36+
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.
6937

70-
```sh
71-
export MONGODB_URI="mongodb+srv://usr:pwd@new.mongodb.net/dbname?retryWrites=true"
72-
```
7338

7439
## Tutorials
7540

get-started.sh

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
#!/bin/bash
22
MONGODB_URI=${1}
3+
DOCKER_IMAGE=ghcr.io/mongodb-developer/get-started-cxx
4+
35
if [ -z ${MONGODB_URI} ]
46
then
57
read -p "MONGODB URI (Required): " MONGODB_URI
6-
fi
8+
fi
79

8-
DRIVER_VERSION=${2:-3.6.2}
910
echo "Executing ... "
1011
docker run --rm -e MONGODB_URI=${MONGODB_URI} \
1112
-v "$(pwd)":/workspace \
12-
-w /workspace/cxx start-cxx \
13-
"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; \
13+
-w /workspace/cxx ${DOCKER_IMAGE} \
14+
"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; \
1415
./getstarted"

0 commit comments

Comments
 (0)