customizing NVIDIA tensorflow docker image

(0) configuring users and environment

# launch the container from the original NVIDIA image

# in the container launched:
$ apt-get update
$ apt-get install -y sudo openssh-server vim screen mc

(0.1) Configure sshd using /etc/ssh/sshd_config file. e.g., one may use the following list of parameters:

Port 2224
AddressFamily any
ListenAddress 0.0.0.0

PermitRootLogin no
PubkeyAuthentication yes
HostbasedAuthentication no
IgnoreRhosts yes
PasswordAuthentication no
PermitEmptyPasswords no
ChallengeResponseAuthentication no
UsePAM yes
AllowTcpForwarding yes
X11Forwarding yes
X11UseLocalhost yes
PermitTTY yes
PrintMotd no
TCPKeepAlive yes
PermitTunnel no
AcceptEnv LANG LC_*
Subsystem       sftp    /usr/lib/openssh/sftp-server

(0.2) now configure the user:

$ groupadd -g 1001 <groupname>
# 1001 is a group ID
$ useradd -c 'user description' -u 1001 -g <groupname> -m -d /home/<uname> -s /bin/bash <uname>

# setting the new user password
$ passwd <uname>
# type a password twice. actually, with the preferences mentioned above, the user will not be asked for a password

$ cp /root/.bashrc /home/<uname>/
$ chown <uname>:<gname> /home/<uname>/.bashrc
$ cp /root/.profile /home/<uname>/
$ chown <uname>:<gname> /home/<uname>/.profile

make the new user a sudoer:

$ visudo
# add a line like this:
<uname> ALL=(ALL) NOPASSWD:ALL

(0.3) now add environment variables to the .bashrc:

# at the host
$ docker inspect <original_container_name>:<original_container_tag>
# find the "Env" section and copy all the vars

copy the contents of the Env section and paste it to the file /home/<uname>/.bashrc preceding every line with the export token

Commit changes to the container and relaunch it from behalf of a new user:

# at the host
# list all the containers launched
$ docker ps -a

# list all the images at the host:
$ docker images

# commit changes:
$ docker commit <container_name_or_id> <new_image_name>:<new_image_tag>

# in the container:
$ exit

# at the host:
$ docker run --shm-size=1g --ulimit memlock=-1 --ulimit stack=67108864 -it --rm -v /src/volume/mountpoint:/container/volume/mountpoint -u <uid>:<gid> -p 2223:2223 <new_image_name>:<new_image_tag> /bin/bash

find the public key of the new user and import its contents to the authorized_keys file:

# in the container launched
$ cd
$ mkdir .ssh
$ chmod 700 .ssh
$ cd .ssh
$ cat /path/to/the/public/key/file/user_docker_TESLA.pub >> authorized_keys
$ chmod 600 authorized_keys

renew host keys:

# in the container
$ sudo rm -rvf /etc/ssh/ssh_host_*
$ sudo dpkg-reconfigure openssh-server

# a fix in Ubuntu 16.04
$ sudo mkdir -p -m 0755 /var/run/sshd

commit changes, relaunch the container

(1) installing basemap.

(1.1) installing GEOS

# ensure cmake is installed
$ sudo apt-get update
$ sudo apt-get install -y cmake

$ git clone https://git.osgeo.org/gitea/geos/geos.git
$ cd geos
$ mkdir build
$ cd build
$ cmake ../
$ make
$ sudo make install

(1.2) installing PROJ

$ sudo -E apt-get install proj-bin

ensure environment variables point to the right places (refer to the basemap docs)

export PROJ_LIB=/usr/share/proj
export GEOS_DIR=/usr/local

(1.3) installing basemap itself

$ git clone https://github.com/matplotlib/basemap.git
$ cd basemap
$ sudo -E python setup.py install

[DEPRECATED]
(1.3.a) according to this issue, we now have to remove the file __init__.py from the directory /usr/local/lib/python3.6/dist-packages/basemap-1.2.1-py3.6-linux-x86_64.egg/mpl_toolkits/

(1.3.b) according to this issue, we now should ensure the workaround is applied in both the files:
/usr/local/lib/python3.6/dist-packages/basemap-1.2.1-py3.6-linux-x86_64.egg/mpl_toolkits/basemap/proj.py
and
/usr/local/lib/python3.6/dist-packages/basemap-1.2.1-py3.6-linux-x86_64.egg/mpl_toolkits/basemap/__init__.py

In particular, the line
from matplotlib.cbook import dedent
is replaced with the block:

try:
    from inspect import cleandoc as dedent
except ImportError:
    # Deprecated as of version 3.1. Not quite the same
    # as textwrap.dedent.
    from matplotlib.cbook import dedent
$ sudo mv /usr/local/lib/python3.6/dist-packages/basemap-1.2.1-py3.6-linux-x86_64.egg/mpl_toolkits/__init__.py /usr/local/lib/python3.6/dist-packages/basemap-1.2.1-py3.6-linux-x86_64.egg/mpl_toolkits/__init__.py.bak

(2) install other packages

$ sudo -E pip install opencv-python imgaug scikit-image imageio pandas scikit-learn netcdf4
$ sudo -E apt-get install -y libsm6 libxext6 libxrender-dev libgl1-mesa-glx