{ "metadata": { "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.5-final" }, "orig_nbformat": 2, "kernelspec": { "name": "Python 3.8.5 64-bit ('streamlit': conda)", "display_name": "Python 3.8.5 64-bit ('streamlit': conda)", "metadata": { "interpreter": { "hash": "d5b47a323bfc67f94a37e7cb8fd52db1ad06cc55d227a5b8c375f2f5af6e26dc" } } } }, "nbformat": 4, "nbformat_minor": 2, "cells": [ { "source": [ "# Docker cheatsheet" ], "cell_type": "markdown", "metadata": {} }, { "source": [ "Main ideas and docker file commands ([based on this blog post](https://towardsdatascience.com/how-docker-can-help-you-become-a-more-effective-data-scientist-7fc048ef91d5))." ], "cell_type": "markdown", "metadata": {} }, { "source": [ "## Glossary\n", "\n", "- Image: blueprint to build\n", "- Container: instance of image\n", "- Dockerfile: recipe for image, a text file\n", "- Layer: modifications of the image" ], "cell_type": "markdown", "metadata": {} }, { "source": [ "## Dockerfile commands\n", "\n", "- `FROM ubuntu:18.04`: build image upon ubuntu base image\n", "- `LABEL maintainer=\"Firstname Lastname email`: optioal metadata\n", "- `ENV LANG=C.UTF-8 LC_ALL=C.UTF-8`: Environment variables\n", "- `RUN apt-get update && apt-get install -y python-pip`: shell commands to run\n", "- `EXPOSE 7745`: open a port (e.g. for jupyter). Publises only if used `-p` or `-P` with `run`\n", "- `VOLUME /ds`:\n", " - mount externally mounted volumes (need to specify the mountpoint at container run/creation)\n", " - data inside this won't be saved, but outide of it will\n", "- `WORKDIR /ds`: starting point for relative file references\n", "- `COPY hom* /mydir/`: copies new files and add them to the container's filesystem at a destination path\n", "- `CMD ['/bin/bash']`:\n", " - run a bash shell to prevent closing the container\n", " - only the last `CMD` will be executed" ], "cell_type": "markdown", "metadata": {} }, { "source": [ "## Build a Docker Image\n", "\n", "```shell\n", "docker build -t tutorial -f ./Dockerfile.gpu ./\n", "```\n", "\n", "(This builds an **image** not a container)\n", "\n", "- `-t tutorial`: name or tag\n", "- `-f ./Dockerfile.gpu`: Dockerfile location\n", "- `./`: context host directory, the relative path refrence point" ], "cell_type": "markdown", "metadata": {} }, { "source": [ "## Build a Docker container from an Image\n", "\n", "```shell\n", "docker run -it --name container1 --net=host -v ~/docker_files/:/ds tutorial\n", "```\n", "- `-it`: interactive run\n", "- `--net=host`: bind ports to host\n", "- `-v /docker_files/:/ds`: Mount host directory to you as a volume : the mount detination\n", "- `tutorial`: image name\n" ], "cell_type": "markdown", "metadata": {} }, { "source": [ "## Container-related commands\n", "\n", "Open a terminal iteractively in the `container1` container.\n", "\n", "```shell\n", "docker exec -it container1 bash\n", "```\n", "\n", "Save current state of container as new image (username is e.g. for DockerHub)\n", "```shell\n", "docker commit container1 username/image2name:tagname\n", "```\n", "\n", "List running containers\n", "```shell\n", "docker ps -a -f status=running\n", "```\n", "\n", "List images\n", "```shell\n", "docker images\n", "```\n", "\n", "Push image to registry\n", "```shell\n", "docker push username/image2name:tagname\n", "```" ], "cell_type": "markdown", "metadata": {} }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ] }