Docker - программное обеспечение для автоматизации развёртывания и управления приложениями в средах с поддержкой контейнеризации.
Dockerfile
FROM python:3.7-alpine
WORKDIR /app
COPY requirements.txt /app
RUN pip3 install -r requirements.txt --no-cache-dir
COPY . /app
ENTRYPOINT ["python3"]
CMD ["app.py"]
Выбор базового образа (FROM):
FROM <image>[:<tag>] [AS <name>]
FROM python:3.7
Запуск команд (RUN):
RUN has 2 forms:
RUN <command> (shell form, the command is run in a shell,
which by default is /bin/sh -c on Linux)
RUN ["executable", "param1", "param2"] (exec form)
RUN pip install -r requirements.txt
Запуск программы в контейнере
ENTRYPOINT ["python3"]
CMD ["app.py"]
ENTRYPOINT ["executable", "param1", "param2"]
ENTRYPOINT command param1 param2
The CMD instruction has three forms:
CMD ["executable","param1","param2"] (exec form)
CMD ["param1","param2"] (as default parameters to ENTRYPOINT)
CMD command param1 param2 (shell form)
CMD ["python3", "app.py"]
CMD python3 app.py
https://docs.docker.com/engine/reference/builder/#understand-how-cmd-and-entrypoint-interact
Добавление файлов в образ
COPY /source/file/path /destination/path
ADD /source/file/path /destination/path
ADD source.file.tar.gz /temp
ADD http://source.file/url /destination/path
USER <user>[:<group>]
USER <UID>[:<GID>]
USER root
ARG DEBIAN_FRONTEND noninteractive
RUN apt-get update && apt-get -yq dist-upgrade \
&& apt-get install -yq --no-install-recommends \
wget \
bzip2 \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*
ENV NB_USER=jovyan \
NB_UID=1000
RUN useradd -m -s /bin/bash -N -u $NB_UID $NB_USER
USER $NB_USER
Порты в контейнере
EXPOSE <port> [<port>/<protocol>...]
EXPOSE 80
The EXPOSE instruction does not actually publish the port. It functions as a type of documentation between the person who builds the image and the person who runs the container, about which ports are intended to be published.
ARG <name>[=<default value>]
ENV <key>=<value> ...
ARG MINICONDA_VERSION=py38_4.9.2
ENV NVIDIA_VISIBLE_DEVICES=all
docker build --build-arg <key>=<value> .
docker run --env <key>=<value>
RUN apt-get update && apt-get install -y cmake wget \
&& rm -rf /var/lib/apt/lists/*
docker pull <<name>> - скачать образ из регистри на машину
docker build <</path/to/dir>> - собрать образ
docker run <<name>> - запустить контейнер
docker ps - список работающих контейнеров
docker start/stop/restart <<name>> - работа с контейнером
docker rm <<name>> - удалить контейнер
docker logs <<name>> - логи контейнера
- Volumes are stored in a part of the host filesystem which is managed by Docker (/var/lib/docker/volumes/ on Linux). Non-Docker processes should not modify this part of the filesystem.
- Bind mounts may be stored anywhere on the host system. Non-Docker processes on the Docker host or a Docker container can modify them at any time.
- tmpfs mounts are stored in the host system’s memory only, and are never written to the host system’s filesystem.
version: '3'
services:
web:
build: app
ports:
- '5000:5000'
docker-compose build – собрать проект
docker-compose up –d – запустить проект
docker-compose down – остановить проект
docker-compose logs -f [service name]` – посмотреть логи сервиса
docker-compose ps – вывести список контейнеров
docker-compose exec [service name] [command] – выполнить команду
docker-compose images – список образов
Kubernetes adds distributed computing features on top of containers:
- Pods: pods are logical groups of containers that share resources like memory, CPU, storage, and network.
- Auto-scaling: Kubernetes can automatically adapt to changing workloads by starting and stopping pods as needed.
- Self-healing: containers are monitored and restarted on failure.
- Load-balancing: requests are distributed over the healthy available pods.
- Rollouts: Kubernetes supports automated rollouts and rollbacks. Making otherwise complex procedures like Canary and Blue-Green releases trivial.