Problems installing MongoDB in a docker container

So, I have a Dockerfile for a container containing MongoDB:

FROM ubuntu:20.04

RUN apt update
RUN apt-get install -y wget
RUN apt-get install -y software-properties-common

RUN wget -qO - https://www.mongodb.org/static/pgp/server-4.2.asc | apt-key add -
RUN echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.2 multiverse" | tee /etc/apt/sources.list.d/mongodb-org-4.2.list
RUN apt update
RUN apt-get install -y mongodb-org

EXPOSE 27017

CMD /usr/bin/mongod 

The problem is that when trying to build, I get the error

(...)
Setting up mongodb-org-server (4.2.11) ...
Adding system user `mongodb' (UID 105) ...
Adding new user `mongodb' (UID 105) with group `nogroup' ...
Not creating home directory `/home/mongodb'.
Adding group `mongodb' (GID 106) ...
Done.
Adding user `mongodb' to group `mongodb' ...
Adding user mongodb to group mongodb
Done.
System has not been booted with systemd as init system (PID 1). Can't operate.
Failed to connect to bus: Host is down
dpkg: error processing package mongodb-org-server (--configure):
 installed mongodb-org-server package post-installation script subprocess returned error exit status 1
Setting up mongodb-org-shell (4.2.11) ...
Setting up mongodb-org-mongos (4.2.11) ...
dpkg: dependency problems prevent configuration of mongodb-org:
 mongodb-org depends on mongodb-org-server; however:
  Package mongodb-org-server is not configured yet.

dpkg: error processing package mongodb-org (--configure):
 dependency problems - leaving unconfigured
Processing triggers for libc-bin (2.31-0ubuntu9.1) ...
Errors were encountered while processing:
 mongodb-org-server
 mongodb-org
E: Sub-process /usr/bin/dpkg returned an error code (1)
The command '/bin/sh -c apt-get install -y mongodb-org' returned a non-zero code: 100

I recently had a somewhat similar problem asked in Problems installing MongoDB in a container regarding something similar with Singularity.
In there, the solution was to upgrade to Ubuntu 20.04, however in this case this is already done.

Note that I also get three other errors/warnings along the way:

Warning: apt-key output should not be parsed (stdout is not a terminal)

WARNING: apt does not have a stable CLI interface. Use with caution in scripts.

debconf: delaying package configuration, since apt-utils is not installed

But as far as I read those are harmless, including the last one (see docker - Avoid apt-utils warning in Debian Dockerfile - Server Fault).

When I separately install the server part itself using

RUN apt-get install -y mongodb-org-server

I get

Setting up mongodb-org-server (4.2.11) ...
Adding system user `mongodb' (UID 105) ...
Adding new user `mongodb' (UID 105) with group `nogroup' ...
Not creating home directory `/home/mongodb'.
Adding group `mongodb' (GID 106) ...
Done.
Adding user `mongodb' to group `mongodb' ...
Adding user mongodb to group mongodb
Done.
System has not been booted with systemd as init system (PID 1). Can't operate.
Failed to connect to bus: Host is down
dpkg: error processing package mongodb-org-server (--configure):
 installed mongodb-org-server package post-installation script subprocess returned error exit status 1
Processing triggers for libc-bin (2.31-0ubuntu9.1) ...
Errors were encountered while processing:
 mongodb-org-server
E: Sub-process /usr/bin/dpkg returned an error code (1)
The command '/bin/sh -c apt-get install -y mongodb-org-server' returned a non-zero code: 100

Hi @Ksortakh_Kraxthar

In the main you have already been answered on this topic Connection error when running MongoDB inside a container: connection refused

You seem intent on rolling your own, look at the dockerfile linked in that post for inspiration.

Your next issue beyond that is one of platform compatibility. Look here for mongodb 4.2

Main reason I use my own is that it’s kind of a MCVE. I tend to switch working environment a lot. But using the code in there is fine with me.

So I took the full code https://github.com/docker-library/mongo/blob/master/4.4/Dockerfile and tried to get it to work (without any alteration), sadly what I get is

Step 9/22 : RUN set -ex; 	export GNUPGHOME="$(mktemp -d)"; 	for key in $GPG_KEYS; do 		gpg --batch --keyserver ha.pool.sks-keyservers.net --recv-keys "$key"; 	done; 	gpg --batch --export $GPG_KEYS > /etc/apt/trusted.gpg.d/mongodb.gpg; 	command -v gpgconf && gpgconf --kill all || :; 	rm -r "$GNUPGHOME"; 	apt-key list
 ---> Running in 47432bab1570
+ mktemp -d
+ export GNUPGHOME=/tmp/tmp.QuN2oscQ9m
+ gpg --batch --keyserver ha.pool.sks-keyservers.net --recv-keys 20691EEC35216C63CAF66CE1656408E390CFB1F5
gpg: keybox '/tmp/tmp.QuN2oscQ9m/pubring.kbx' created
gpg: keyserver receive failed: Cannot assign requested address
The command '/bin/sh -c set -ex; 	export GNUPGHOME="$(mktemp -d)"; 	for key in $GPG_KEYS; do 		gpg --batch --keyserver ha.pool.sks-keyservers.net --recv-keys "$key"; 	done; 	gpg --batch --export $GPG_KEYS > /etc/apt/trusted.gpg.d/mongodb.gpg; 	command -v gpgconf && gpgconf --kill all || :; 	rm -r "$GNUPGHOME"; 	apt-key list' returned a non-zero code: 2

I still don’t understand how the official image does not suffice, even as a base. :man_shrugging:

That is a gpg ipv6 issue.

--- Dockerfile.orig	2020-12-15 09:49:49.793284295 -0500
+++ Dockerfile	2020-12-15 09:47:11.308997656 -0500
@@ -62,6 +62,8 @@
 ENV GPG_KEYS 20691EEC35216C63CAF66CE1656408E390CFB1F5
 RUN set -ex; \
 	export GNUPGHOME="$(mktemp -d)"; \
+  mkdir ~/.gnupg; \
+  echo "disable-ipv6" >> ~/.gnupg/dirmngr.conf; \
 	for key in $GPG_KEYS; do \
 		gpg --batch --keyserver ha.pool.sks-keyservers.net --recv-keys "$key"; \
 	done; \
@@ -109,3 +111,4 @@
 
 EXPOSE 27017
 CMD ["mongod"]

It suffices, just saying why I didn’t do it at first.

Added your lines, still the same error:

Step 9/22 : RUN set -ex; 	export GNUPGHOME="$(mktemp -d)"; 	mkdir ~/.gnupg;     echo "disable-ipv6" >> ~/.gnupg/dirmngr.conf; 	for key in $GPG_KEYS; do 		gpg --batch --keyserver ha.pool.sks-keyservers.net --recv-keys "$key"; 	done; 	gpg --batch --export $GPG_KEYS > /etc/apt/trusted.gpg.d/mongodb.gpg; 	command -v gpgconf && gpgconf --kill all || :; 	rm -r "$GNUPGHOME"; 	apt-key list
 ---> Running in f2805787a89a
+ mktemp -d
+ export GNUPGHOME=/tmp/tmp.OC79cNnf7R
+ mkdir /root/.gnupg
+ echo disable-ipv6
+ gpg --batch --keyserver ha.pool.sks-keyservers.net --recv-keys 20691EEC35216C63CAF66CE1656408E390CFB1F5
gpg: keybox '/tmp/tmp.OC79cNnf7R/pubring.kbx' created
gpg: keyserver receive failed: Cannot assign requested address
The command '/bin/sh -c set -ex; 	export GNUPGHOME="$(mktemp -d)"; 	mkdir ~/.gnupg;     echo "disable-ipv6" >> ~/.gnupg/dirmngr.conf; 	for key in $GPG_KEYS; do 		gpg --batch --keyserver ha.pool.sks-keyservers.net --recv-keys "$key"; 	done; 	gpg --batch --export $GPG_KEYS > /etc/apt/trusted.gpg.d/mongodb.gpg; 	command -v gpgconf && gpgconf --kill all || :; 	rm -r "$GNUPGHOME"; 	apt-key list' returned a non-zero code: 2

I’m also reading that gpg keyservers are flaky, yes they are. So some retries might be needed.

Or do the “bad” thing and do what you had for your previous Dockerfile:
RUN wget -qO - https://www.mongodb.org/static/pgp/server-4.4.asc | apt-key add -

Couldn’t get it to work in any configuration.
Tested out some other official dockerfiles from the github, couldn’t build any of them.
Instead I switched to using bitnami/mongodb:latest directly, that works.

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.