Create a Dockerfile with Python 3.7, PyGit2, and MongoDB

Create a Dockerfile with Python 3.7, PyGit2, and MongoDB

In this post, a new Dockerfile is created to build a Docker Image with Python 3.7, PyGit2, and MongoDB. The base image is amazonlinux.

Python 3.7

To install Python 3.7, it’s simple, just get the default packages.

1
yum -y install python3 python3-devel

PyGit2

For PyGit2, there are not prebuilt packages for its dependency libgit2. It has to be built from source.

The CI scripts of libgit2 are good resource for building, but they are supposed on debian or ubuntu using apt.

1
https://github.com/libgit2/libgit2/blob/master/ci/setup-linux.sh

The converted commands are as following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
yum -y update
yum -y install tar gzip make automake gcc gcc-c++ kernel-devel cmake openssl-devel libssh2-devel libcurl-devel git wget
cd /root && \
git clone --depth 10 --single-branch --branch mbedtls-2.6.1 https://github.com/ARMmbed/mbedtls.git mbedtls && \
cd mbedtls && \
CFLAGS=-fPIC cmake -DENABLE_PROGRAMS=OFF -DENABLE_TESTING=OFF -DUSE_SHARED_MBEDTLS_LIBRARY=OFF -DUSE_STATIC_MBEDTLS_LIBRARY=ON . && \
cmake --build . && \
make install

cd /root && \
wget https://github.com/libgit2/libgit2/archive/v0.27.8.tar.gz && \
tar xvf v0.27.8.tar.gz && \
cd libgit2-0.27.8 && \
cmake . && \
make && \
make install

After that, install PyGit2 with pip.

1
2
3
4
pip3 install cffi
pip3 install pygit2
echo /usr/local/lib > /etc/ld.so.conf.d/libgit2.conf
ldconfig

mongodb

Download the latest version from MongoDB official website and extract it. That’s all for this.

1
2
3
cd /root && \
wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-amazon2-4.0.6.tgz && \
tar xvf mongodb-linux-x86_64-amazon2-4.0.6.tgz

Complete Dockerfile

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
FROM amazonlinux:latest
RUN yum -y update
RUN yum -y install tar gzip make automake gcc gcc-c++ kernel-devel cmake openssl-devel libssh2-devel libcurl-devel git wget
RUN cd /root && \
git clone --depth 10 --single-branch --branch mbedtls-2.6.1 https://github.com/ARMmbed/mbedtls.git mbedtls && \
cd mbedtls && \
CFLAGS=-fPIC cmake -DENABLE_PROGRAMS=OFF -DENABLE_TESTING=OFF -DUSE_SHARED_MBEDTLS_LIBRARY=OFF -DUSE_STATIC_MBEDTLS_LIBRARY=ON . && \
cmake --build . && \
make install
RUN cd /root && \
wget https://github.com/libgit2/libgit2/archive/v0.27.8.tar.gz && \
tar xvf v0.27.8.tar.gz && \
cd libgit2-0.27.8 && \
cmake . && \
make && \
make install

RUN yum -y install python3 python3-devel
RUN pip3 install cffi
RUN pip3 install pygit2
RUN echo /usr/local/lib > /etc/ld.so.conf.d/libgit2.conf
RUN ldconfig

RUN python3 -c 'import pygit2'

RUN cd /root && \
wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-amazon2-4.0.6.tgz && \
tar xvf mongodb-linux-x86_64-amazon2-4.0.6.tgz

RUN mkdir /root/data

EXPOSE 27017
COPY entrypoint.sh /root/entrypoint.sh
ENTRYPOINT [ "/bin/bash", "/root/entrypoint.sh"]

entrypoint.sh

1
2
3
4
5
#!/bin/bash

/root/mongodb-linux-x86_64-amazon2-4.0.6/bin/mongod --bind_ip_all --dbpath /root/data --fork --logpath /var/log/mongo.log

exec $1
文章目录
  1. 1. Python 3.7
  2. 2. PyGit2
  3. 3. mongodb
  4. 4. Complete Dockerfile
  5. 5. entrypoint.sh
,