0%

Docker cheatsheet

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
FROM ubuntu:16.04

USER root

# install necessary packages
RUN apt-get update && \
apt-get -y install sudo vim zsh curl git build-essential openssh-server python-tk python3-tk x11-apps net-tools iputils-ping software-properties-common

# for X11 forwarding
RUN echo "X11UseLocalhost no" >> /etc/ssh/sshd_config

# duplicate the current user; UID, GID can be input via command line
ARG UNAME=weepingfish
ARG UID=1000
ARG GID=1000
RUN useradd -m -u $UID -g $GID -o -s /bin/bash $UNAME

# set default password
RUN echo "weepingfish:password" | chpasswd && adduser weepingfish sudo

# setup custom $HOME
RUN usermod --home /newhome/weepingfish weepingfish

# set the default user and default login directory
USER weepingfish
WORKDIR /newhome/weepingfish

# install zsh
RUN wget https://github.com/robbyrussell/oh-my-zsh/raw/master/tools/install.sh -O - | zsh || true

Usage

1
2
3
4
5
6
7
8
9
10
11
# build an image (in /path/to/Dockerfile)
docker build --build-arg UID=$(id -u) --build-arg GID=$(id -g) --build-arg UNAME=$USER -t ubuntu:16.04 .

# run a deamon container with nvidia support (need to install nvidia-docker first)
nvidia-docker run -dit --name vm1 -e DISPLAY=$DISPLAY -v /tmp/.X11-unix:/tmp/.X11-unix -v /newhome:/newhome -p 8040-8049:8040-8049 --hostname vm1 --gpus all ubuntu:16.04

# start an interactive docker session
docker exec -it vm1 zsh

# stop and delete the container
docker container stop vm1 && docker container rm vm1