Skip to content

Commit f0c8557

Browse files
seemetherefacebook-github-bot
authored andcommitted
docker: Refactor Dockerfile process for official images (pytorch#32515)
Summary: ## Commit Message: Refactors Dockerfile to be as parallel as possible with caching and adds a new Makefile to build said Dockerfile. Also updated the README.md to reflect the changes as well as updated some of the verbage around running our latest Docker images. Adds the new Dockerfile process to our CircleCI workflows ## How to build: Building the new images is pretty simple, just requires `docker` > 18.06 since the new build process relies on `buildkit` caching and multi-stage build resolving. ### Development images For `runtime` images: ``` make -f docker.Makefile runtime-image ``` For `devel` images: ``` make -f docker.Makefile devel-image ``` Builds are tagged as follows: ```bash docker.io/${docker_user:-whoami}/pytorch:$(git describe --tags)-${image_type} ``` Example: ``` docker.io/seemethere/pytorch:v1.4.0a0-2225-g9eba97b61d-runtime ``` ### Official images Official images are the ones hosted on [`docker.io/pytorch/pytorch`](https://hub.docker.com/r/pytorch/pytorch) To do official images builds you can simply add set the `BUILD_TYPE` variable to `official` and it will do the correct build without building the local binaries: Example: ``` make -f docker.Makefile BUILD_TYPE=official runtime-image ``` ## How to push: Pushing is also super simple (And will automatically tag the right thing based off of the git tag): ``` make -f docker.Makefile runtime-push make -f docker.Makefile devel-push ``` Signed-off-by: Eli Uriegas <eliuriegas@fb.com> Pull Request resolved: pytorch#32515 Differential Revision: D19558619 Pulled By: seemethere fbshipit-source-id: a06b25cd39ae9890751a60f8f36739ad6ab9ac99
1 parent 8fd3eae commit f0c8557

File tree

3 files changed

+146
-8
lines changed

3 files changed

+146
-8
lines changed

Dockerfile

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# syntax = docker/dockerfile:experimental
2+
#
3+
# NOTE: To build this you will need a docker version > 18.06 with
4+
# experimental enabled and DOCKER_BUILDKIT=1
5+
#
6+
# If you do not use buildkit you are not going to have a good time
7+
#
8+
# For reference:
9+
# https://docs.docker.com/develop/develop-images/build_enhancements/
10+
ARG BASE_IMAGE=ubuntu:18.04
11+
ARG PYTHON_VERSION=3.7
12+
13+
FROM ${BASE_IMAGE} as dev-base
14+
RUN --mount=type=cache,id=apt-dev,target=/var/cache/apt \
15+
apt-get update && apt-get install -y --no-install-recommends \
16+
build-essential \
17+
ca-certificates \
18+
ccache \
19+
cmake \
20+
curl \
21+
git \
22+
libjpeg-dev \
23+
libpng-dev && \
24+
rm -rf /var/lib/apt/lists/*
25+
RUN /usr/sbin/update-ccache-symlinks
26+
RUN mkdir /opt/ccache && ccache --set-config=cache_dir=/opt/ccache
27+
ENV PATH /opt/conda/bin:$PATH
28+
29+
FROM dev-base as conda
30+
RUN curl -v -o ~/miniconda.sh -O https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh && \
31+
chmod +x ~/miniconda.sh && \
32+
~/miniconda.sh -b -p /opt/conda && \
33+
rm ~/miniconda.sh && \
34+
/opt/conda/bin/conda install -y python=${PYTHON_VERSION} conda-build pyyaml numpy ipython&& \
35+
/opt/conda/bin/conda clean -ya
36+
37+
FROM dev-base as submodule-update
38+
WORKDIR /opt/pytorch
39+
COPY . .
40+
RUN git submodule update --init --recursive
41+
42+
FROM conda as build
43+
WORKDIR /opt/pytorch
44+
COPY --from=conda /opt/conda /opt/conda
45+
COPY --from=submodule-update /opt/pytorch /opt/pytorch
46+
RUN --mount=type=cache,target=/opt/ccache \
47+
TORCH_CUDA_ARCH_LIST="3.5 5.2 6.0 6.1 7.0+PTX" TORCH_NVCC_FLAGS="-Xfatbin -compress-all" \
48+
CMAKE_PREFIX_PATH="$(dirname $(which conda))/../" \
49+
python setup.py install
50+
51+
FROM conda as conda-installs
52+
ARG INSTALL_CHANNEL=pytorch-nightly
53+
RUN /opt/conda/bin/conda install -c "${INSTALL_CHANNEL}" -y pytorch torchvision && \
54+
/opt/conda/bin/conda clean -ya
55+
56+
FROM ${BASE_IMAGE} as official
57+
LABEL com.nvidia.volumes.needed="nvidia_driver"
58+
RUN --mount=type=cache,id=apt-final,target=/var/cache/apt \
59+
apt-get update && apt-get install -y --no-install-recommends \
60+
ca-certificates \
61+
libjpeg-dev \
62+
libpng-dev && \
63+
rm -rf /var/lib/apt/lists/*
64+
COPY --from=conda-installs /opt/conda /opt/conda
65+
ENV PATH /opt/conda/bin:$PATH
66+
ENV NVIDIA_VISIBLE_DEVICES all
67+
ENV NVIDIA_DRIVER_CAPABILITIES compute,utility
68+
ENV LD_LIBRARY_PATH /usr/local/nvidia/lib:/usr/local/nvidia/lib64
69+
WORKDIR /workspace
70+
71+
FROM official as dev
72+
# Should override the already installed version from the official-image stage
73+
COPY --from=build /opt/conda /opt/conda

README.md

+18-8
Original file line numberDiff line numberDiff line change
@@ -280,20 +280,30 @@ ccmake build # or cmake-gui build
280280

281281
### Docker Image
282282

283-
Dockerfile is supplied to build images with cuda support and cudnn v7. You can pass `-e PYTHON_VERSION=x.y` flag to specify which Python version is to be used by Miniconda, or leave it unset to use the default. Build from pytorch repo directory as docker needs to copy git repo into docker filesystem while building the image.
284-
```
285-
docker build -t pytorch -f docker/pytorch/Dockerfile . # [optional] --build-arg WITH_TORCHVISION=0
286-
```
283+
#### Using pre-built images
287284

288-
You can also pull a pre-built docker image from Docker Hub and run with nvidia-docker,
289-
but this is not currently maintained and will pull PyTorch 0.2.
290-
```
291-
nvidia-docker run --rm -ti --ipc=host pytorch/pytorch:latest
285+
You can also pull a pre-built docker image from Docker Hub and run with docker v19.03+
286+
287+
```bash
288+
docker run --gpus all --rm -ti --ipc=host pytorch/pytorch:latest
292289
```
290+
293291
Please note that PyTorch uses shared memory to share data between processes, so if torch multiprocessing is used (e.g.
294292
for multithreaded data loaders) the default shared memory segment size that container runs with is not enough, and you
295293
should increase shared memory size either with `--ipc=host` or `--shm-size` command line options to `nvidia-docker run`.
296294

295+
#### Building the image yourself
296+
297+
**NOTE:** Must be built with a docker version > 18.06
298+
299+
The `Dockerfile` is supplied to build images with cuda support and cudnn v7.
300+
You can pass `PYTHON_VERSION=x.y` make variable to specify which Python version is to be used by Miniconda, or leave it
301+
unset to use the default.
302+
```bash
303+
make -f docker.Makefile
304+
# images are tagged as docker.io/${your_docker_username}/pytorch
305+
```
306+
297307
### Building the Documentation
298308

299309
To build documentation in various formats, you will need [Sphinx](http://www.sphinx-doc.org) and the

docker.Makefile

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
DOCKER_REGISTRY = docker.io
2+
DOCKER_ORG = $(shell docker info 2>/dev/null | sed '/Username:/!d;s/.* //')
3+
DOCKER_IMAGE = pytorch
4+
DOCKER_FULL_NAME = $(DOCKER_REGISTRY)/$(DOCKER_ORG)/$(DOCKER_IMAGE)
5+
6+
ifeq ("$(DOCKER_ORG)","")
7+
$(warning WARNING: No docker user found using results from whoami)
8+
DOCKER_ORG = $(shell whoami)
9+
endif
10+
11+
BASE_RUNTIME = ubuntu:18.04
12+
BASE_DEVEL = nvidia/cuda:10.1-cudnn7-devel-ubuntu18.04
13+
14+
# The conda channel to use to install pytorch / torchvision
15+
INSTALL_CHANNEL = pytorch
16+
17+
PYTHON_VERSION = 3.7
18+
# Can be either official / dev
19+
BUILD_TYPE = dev
20+
BUILD_PROGRESS = auto
21+
BUILD_ARGS = --build-arg BASE_IMAGE=$(BASE_IMAGE) --build-arg PYTHON_VERSION=$(PYTHON_VERSION) --build-arg INSTALL_CHANNEL=$(INSTALL_CHANNEL)
22+
DOCKER_BUILD = DOCKER_BUILDKIT=1 docker build --progress=$(BUILD_PROGRESS) --target $(BUILD_TYPE) -t $(DOCKER_FULL_NAME):$(DOCKER_TAG) $(BUILD_ARGS) .
23+
DOCKER_PUSH = docker push $(DOCKER_FULL_NAME):$(DOCKER_TAG)
24+
25+
.PHONY: all
26+
all: devel-image
27+
28+
.PHONY: devel-image
29+
devel-image: BASE_IMAGE := $(BASE_DEVEL)
30+
devel-image: DOCKER_TAG := $(shell git describe --tags)-devel
31+
devel-image:
32+
$(DOCKER_BUILD)
33+
34+
.PHONY: devel-image
35+
devel-push: BASE_IMAGE := $(BASE_DEVEL)
36+
devel-push: DOCKER_TAG := $(shell git describe --tags)-devel
37+
devel-push:
38+
$(DOCKER_PUSH)
39+
40+
.PHONY: runtime-image
41+
runtime-image: BASE_IMAGE := $(BASE_RUNTIME)
42+
runtime-image: DOCKER_TAG := $(shell git describe --tags)-runtime
43+
runtime-image:
44+
$(DOCKER_BUILD)
45+
docker tag $(DOCKER_FULL_NAME):$(DOCKER_TAG) $(DOCKER_FULL_NAME):latest
46+
47+
.PHONY: runtime-image
48+
runtime-push: BASE_IMAGE := $(BASE_RUNTIME)
49+
runtime-push: DOCKER_TAG := $(shell git describe --tags)-runtime
50+
runtime-push:
51+
$(DOCKER_PUSH)
52+
53+
.PHONY: clean
54+
clean:
55+
-docker rmi -f $(shell docker images -q $(DOCKER_FULL_NAME))

0 commit comments

Comments
 (0)