Containers Pablo F. Ordóñez October 18, 2018 1 Welcome Song: Sola vaya Interpreter: La Sonora Ponceña 2 Goals Containers!= ( Moby-Dick ) Containers are part of the Linux Kernel Make your own container 3 Cgroups tutorial 3.1 Set Server 3.2 Containers Wave Service provision Virtualization Example First Physical boxes Second Virtual machines Hardware VBox VMw KVM Third Containers OS Docker LXD Lxc 3.3 Control Groups Control groups (cgroups) is a kernel feature that limits, accounts for and isolates the CPU, memory, disk I/O and network s usage of one or more processes. The cgroups frameworks provides the following: 3.4 Install Resource limiting: a group can be configured not to exceed a specified memory limit or use more than the desired amount of processors or be limited to specific peripheral devices. Prioritization: one or more groups may be configured to utilize fewer or more CPUs or disk I/O throughput. Accounting: a group s resource usage is monitored and measured. Control: groups of processes can be frozen or stopped and restarted. sudo apt-get -y install cgroup-bin cgroup-lite cgroup-tools cgroupfs-mount libcgroup1 3.5 Testing script create a bash file in ~/bin rm ~/bin/testcgroups.sh touch ~/bin/testcgroups.sh chmod +x ~/bin/testcgroups.sh ll ~/bin/testcgroups.sh Create a mock bash script
echo " #!/bin/sh while [ 1 ]; do echo "hello world" sleep 5 done " > ~/bin/testcgroups.sh cat ~/bin/testcgroups.sh 3.6 The manual approach 3.6.1 create a cgroup named foo under the memory subsystem sudo mkdir /sys/fs/cgroup/memory/foo 3.6.2 Limit the memory for anything running under the cgroup foo to 50MB: echo 500000 sudo tee /sys/fs/cgroup/memory/foo/memory.limit_in_bytes sudo cat /sys/fs/cgroup/memory/foo/memory.limit_in_bytes 3.6.3 Testing Launch the app and monitoring Move the application to cgroup foo under the memory controller Verify that it s running within the desired cgroup sh ~/bin/testcgroups.sh & newpid=$! ps -U pordonez echo > $newpid /sys/fs/cgroup/memory/foo/cgroup.procs ps -o cgroup $newpid Terminate the process kill $newpid ps -U pordonez 4 Dockerfile tutorial Docker is a platform for developers and sysadmins to develop, deploy, and run applications with containers. Each Dockerfile is a script, composed of various commands (instructions) and arguments listed successively to automatically perform actions on a base image in order to create (or form) a new one Dockerfiles begin with defining an image FROM which the build process starts. Followed by various other methods, commands and arguments (or conditions), in return, provide a new image which is to be used for creating docker containers. 4.1 Syntax Dockerfile syntax consists of two kind of main line blocks: comments and commands + arguments # Print "Hello docker!" RUN echo "Hello docker!" 4.2 Dockerfile Commands 4.2.1 ADD Copies the files from the source on the host into the container s own filesystem # Usage: ADD [source directory or URL] [destination directory] ADD /my_app_folder /docker_my_app_folder
4.2.2 RUN It takes a command as its argument and runs it to form the image It is used to build the image 4.2.3 CMD Similar to RUN It is not executed during build, but when a container is instantiated It should be considered as an initial, default command that gets executed (i.e. run) with the creation of containers based on the image. # Usage 1: CMD application "argument", "argument",.. CMD "echo" "Hello docker!" 4.2.4 ENTRYPOINT Set default application that is used every time a container is created 4.2.5 ENV Set the environment variables like dictionary (key:value) ENV PATH /opt/conda/bin:$path 4.2.6 EXPOSE Associate a specified port to enable networking between the running process inside the container and the outside world # Ipython Jupyter EXPOSE 8888 4.2.7 FROM It defines the base image to use to start the build process # Usage: FROM [image name] FROM ubuntu 4.2.8 WORKDIR Set where the command defined with CMD is to be executed. 4.3 Example 4.3.1 Create and empty Dockerfile cd ~/mydockerfiles pwd if [ -f "Dockerfile" ]; then rm Dockerfile; else touch Dockerfile; fi ll 4.3.2 Defining Our File and Its Purpose ############################################################ # Dockerfile to build Pytorch Jupyter container images # Based on Ubuntu ########################################################### " > Dockerfile
4.3.3 Setting The Base Image to Use FROM nvidia/cuda:9.0-cudnn7-runtime-ubuntu16.04 cat Dockerfile 1. What s is the base image Dockerfile Cudnn Dockerfile Runtime Dockerfile Base 4.3.4 Defining The Maintainer (Author) # File Author / Maintainer MAINTAINER Example Ernesto Rafael 4.3.5 Metadata LABEL description="prebuilt jupyter environment" LABEL maintainer="https://github.com/erra/notebooks" 4.3.6 APP ARG PYTHON_VERSION=3.5 RUN apt-get update && apt-get install -y --no-install-recommends \ build-essential \ bzip2 \ cmake \ git \ curl \ vim \ wget \ python3 \ python3-setuptools \ libhdf5-dev \ ca-certificates \ libjpeg-dev \ libpng-dev \ graphviz\ psmisc \ && apt-get -qq clean \ && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* RUN wget https://repo.continuum.io/miniconda/miniconda3-latest-linux-x86_64.sh \ -O ~/miniconda.sh --quiet --no-check-certificate && \ chmod +x ~/miniconda.sh && \ ~/miniconda.sh -b -p /opt/conda && \ rm ~/miniconda.sh ENV PATH /opt/conda/bin:$path RUN /opt/conda/bin/conda install -y --quiet python=$python_version \ cython \ ipython \ pyyaml \ mkl \ mkl-include \
typing\ h5py \ jupyterlab \ matplotlib \ msgpack-python \ nltk \ numpy \ opencv \ pandas \ protobuf \ pytables \ scipy \ scikit-learn \ scikit-image \ && \ /opt/conda/bin/conda clean --yes --all --quiet RUN /opt/conda/bin/conda install -y --quiet pytorch torchvision -c pytorch && \ /opt/conda/bin/conda clean --yes --all --quiet RUN pip --no-cache-dir install -q --upgrade pip RUN pip --no-cache-dir install -q -U \ tensorflow-gpu \ tensorboardx \ visdom ## Set up our notebook config. COPY jupyter_notebook_config.py /root/.jupyter/ ## Jupyter has issues with being run directly: ## https://github.com/ipython/ipython/issues/7062 ## We just add a little wrapper script. COPY run_jupyter.sh / COPY ltb /usr/local/bin/ ## Tensorboard EXPOSE 6006 # IPython EXPOSE 8888 WORKDIR /notebooks CMD ["/run_jupyter.sh", "--allow-root"] 4.4 Build docker build -t cu91nnpytorch. 4.5 Run Jupyter nvidia-docker run -it -v ~/Documents/Developer/jupyter/:/notebooks -p 9999:8888 -p 6006:6006 cu91nnpytorch:latest Tensorboardx nvidia-docker run -it cu91nnpytorch:latest /bin/bash tbl Ipython
nvidia-docker run -it cu91nnpytorch:latest /bin/bash ipython