Skip to content

Instantly share code, notes, and snippets.

@blgnksy
Last active May 11, 2022 09:20
Show Gist options
  • Save blgnksy/e8caf2a4f903995e528236f38982548f to your computer and use it in GitHub Desktop.
Save blgnksy/e8caf2a4f903995e528236f38982548f to your computer and use it in GitHub Desktop.
Docker

Docker

[TOC]

Adding users to Docker group

$ usermod -aG docker user_name

Dockerfile template

#### modify below
ARG username=gdl
ARG groupid=1000
ARG userid=1000
## end modify

# -m option creates a fake writable home folder for Jupyter.
RUN groupadd -g $groupid $username \
    && useradd -m -r -u $userid -g $username $username
USER $username

RUN adduser ${username} --uid ${userid} --gecos '' --disabled-password \
    && echo "${username} ALL=(root) NOPASSWD:ALL" > /etc/sudoers.d/${username} \
    && chmod 0440 /etc/sudoers.d/${username}

WORKDIR ${workdir}
RUN chown ${username}:${username} ${workdir}

# change timezone
RUN ln -sf /usr/share/zoneinfo/Turkey /etc/localtime

# Runs first bash.bashrc when the container initializing
COPY bashrc /etc/bash.bashrc
RUN chmod a+rwx /etc/bash.bashrc

CMD ["bash", "-c", "source /etc/bash.bashrc && ...."]

.bahrc for Docker images

http://www.network-science.de/ascii/ http://www.glassgiant.com/ascii/

export PS1="\[\e[31m\]tf-docker\[\e[m\] \[\e[33m\]\w\[\e[m\] > "
export TERM=xterm-256color
alias grep="grep --color=auto"
alias ls="ls --color=auto"

echo -e "\e[1;31m"
cat<<DBDS
..........................,............................................,......,=
............,......................,..,,,,,,,.,...............................,~
..................................,,ODNDDNNNNN8,,,.............................~
................................,O8NNMMMMMMMMNMMND,,...........................~
............................,,8DNNDNNMMMMMMMMMMMMMNN8=,........................~
..........................,8DDNNNNNMMMMMMMMMMMMMMNMNN8O,.......................=
.......................,:DDDNMMMMNNNMMMMMMMMMMMMMMMMMNNZ,,.....................=
.....................,=8N8DNMMMMMMNNMMMMMMMMMMMNNNNNMMMNND,,...................=
...................,+8DNN88MMMMMNNO$Z$OODDD88DD8DDNDDDMMMMN8,..................=
...................8DNNNNNNMDN8O$77777$$777777$$$ZZZ$O$OMMNN8,.................~
..................ZDDMNNDDND877777I777777777777777777I7$ZDMMMO,................~
.................IDDNND$DDZ$7IIII7I777777777777777I7IIIII$DMNN7................~
................,O88N8OI7I7II?IIIIII7I777III777IIIIIIIIIII$NNDD:...............~
................$8D8OO???I???IIIII777777777I77I7IIIIIIII??I$NDDD,..............~
................ZOO8OI??????IIIII777777I77777777III?IIII???7ZNDD,..............~
...............,Z8D8Z?++????IIII777777777I777777III7III?I??IZ8M88..............~
...............~888Z7??+??????III7IIIIIIIIIIIIIIIIIIIIII???I7ON8O..............~
...............O8$8$7????????????IIIIIIIIIIIIII?I???IIII????I$8D8,.............~
...............7$ZO7I+?????IIIIIIIIIII?III?I?IIIIIIII77II????$Z8D,.............~
...............ZZZ7????II$O8OOD8OOO$7I??II??I7$$ZO888D88$7I??IOOD,.............~
..............,OO$????7$OODNNMMN88O$7IIIII?I7O8NMMMMMNMDOZ7I??ZZ8,.............~
..............:OZ?7NMO$OZ8Z$$$777778MMDZO8NMD$$ZOOOZZ$ZZZZ$NN$7ZD,.............~
..............7ZZ+7?N7Z$$7$77$$7$ZZZ$DI77Z777Z8MDD8O7ZO$7ZZ+7OIDO,.............~
..............?87+Z7Z7IIIZ$7ZDDDO8D8O87I77777888NMMNI8ZO7I7?ZZI8Z....,.........~
..............78Z?8I????7$$+8NMNON8Z$NII$77N7$$D8NMN?$7$7I?+I8+O$,.............~
..............,I~Z??++??II?7?8DOO8OOOD?I77I8$O888OZZ7III7II???NN,..............~
...............?$+?++??IIIII77$OOZ8O7?+?I7II$$OZZZ$777II7II????~.,=,...........~
............,??,$++++??IIIIII77777I?I?III7IIII7$Z777III777II??ID7?++...........~
............++?IZ++++???IIIIII$Z7I???IIIIIIII?II$$$77IIIIIII???O?+?I,..........~
............+I=I$++++++???I77$77II?????IIIII?IIII7$$$77I???????8+=+7:..........~
............++~=O++++++++??IIII?II????+??I?IIIII7I77III???????+++++7:..........~
............++==?+++++++++??IIIIII7??+=+??+??I77I7I7II?????????7+++?...........~
............~+~+?+++++++++???III777??7I?III7II$$77III??????????I?+I~...........~
.............+++=++++++++?????II7$7?77?III77O7$Z$7IIII????????+77+?............~
.............==?I++++++?++?+??III7IIIIIII7777I7$ZII?II?????+?+?7I?~............~
..............+===++++++++++???IIIII7I?IIII77777$7II?I?????+???=+=.............~
..........:...,==++++++++++?????IIIII??I7?I77777777II????????++?+..............~
...............,=+++++++++??????I??????I7II?I?II7IIIII?????+?+++,..............~
................==++++++++?????III??I77III77III7$IIIIII??????++=...............~
.................==+++++????+?I7OOZO$$$ZZOZZZO8DNZ7IIIII???++,.................~
..................,++++++??+?II$7???II7I7777$777ZD$IIIII???++..................~
...................+++++???+?I7II??I??IIIII?IIII7$Z7IIII??+?~..................~
...................=++++?????IIII????IIIIII7III777777IIII??+..........:........~
...................:+++?????II?III????IIIII7III77777IIIII??+...................~
...................,=+++???IIIIIII??III777IIIII77I7IIIII???=...................~
...................,=+++????II?I??IIIIIIIII7I7777II7III???++...................~
...................,==+++??I?II??II???IIIIIIII777I77III???++...................~
...................,=++++?IIII????????II$IIIIIIIII7777I???+=...................~
...................,==++???IIIIIIIIIIIII$?I777777777III??+++...................~
...................,==+++???III7III7I77$$77$$$7777I7IIII?+++...................:
...................,==++???I??IIII77$$$$Z$ZZZZ777IIIIII??+++...................~
...................,++++++?????III7777$$$$$$$$777IIIII?????+?I,,...............~
...............,=7?++++++++??????II777777$$$$77I7IIIII???????IIO+,.............~
..........,,:Z78??++=+=+++?+??????II7I77I77IIIIIIII?I???++???IIOZZ8I7,.........~
....,..?DDZZ87$Z?+++++====+++???????I?IIIIIIIIIIII?I?+++++????IOO8O8Z$OOO,,....~
...+DDNDOOOMOO8O?++++======++++++???????II7?IIII?????++++++???IN888D88ZZZ88Z:,.~
Z8DNDZOO8Z8OO88D?+++========++++???+?????III??I???+++++++++???IDD8888O8O8ZO88OO=
8DDZZ88888888ODD?+++=========++++++????IIIIII???+?+++++++++???$DDDDO888D8D8888O=
OZOZZ8DO8O8NOODN?++===========++=+++????II????+++++++++++=++?INDDDDO8DDND88D8OD~
OOOO8OOO88DDOO8D$+===============+++++???????+++++++++++++++?D8DDNDDDDND8D8DD88=
O8O8ZOOOOO8ODZ8O87=====~=========+==+++???+++++++=++=+====+?88D8N88D8OO8888Z888=
          ______   _        _______  _        _        _______          
         (  ___ \ ( \      (  ____ \( (    /|| \    /\(  ____ \|\     /|
         | (   ) )| (      | (    \/|  \  ( ||  \  / /| (    \/( \   / )
         | (__/ / | |      | |      |   \ | ||  (_/ / | (_____  \ (_) / 
         |  __ (  | |      | | ____ | (\ \) ||   _ (  (_____  )  \   /  
         | (  \ \ | |      | | \_  )| | \   ||  ( \ \       ) |   ) (   
         | )___) )| (____/\| (___) || )  \  ||  /  \ \/\____) |   | |   
         |/ \___/ (_______/(_______)|/    )_)|_/    \/\_______)   \_/
                                                              
DBDS
echo -e "\e[0;33m"

if [[ $EUID -eq 0 ]]; then
  cat <<WARN
WARNING: You are running this container as root, which can cause new files in
mounted volumes to be created as the root user on your host machine.

To avoid this, run the container by specifying your user's userid:

$ docker run -u \$(id -u):\$(id -g) args...
WARN
else
  cat <<EXPL
You are running this container as user with ID $(id -u) and group $(id -g),
which should map to the ID and group for your user on the Docker host. Great!
EXPL
fi

# Turn off colors
echo -e "\e[m"

Dockerrun.sh

# Sends the current user ID and GID in order to prevent creating files within the container which belongs to root user.
docker run -u $(id -u):$(id -g) -v $(pwd):/my-devel -it tf

# --network=host

# Using the NVIDIA Video Codec SDK
docker run -it --gpus=all -e NVIDIA_DRIVER_CAPABILITIES=video,compute,utility 

Stop All exited containers

docker rm $(docker ps -qa --filter status=exited )

MacOSX GUI Support

brew install socat
brew  install --cask xquartz
open -a XQuartz

socat TCP-LISTEN:6000,reuseaddr,fork UNIX-CLIENT:\"$DISPLAY\"
docker run -e DISPLAY=hostip:0 [...] image OR DISPLAY=hostip:0 docker-compose up [-d]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment