This is a simple tutorial on how to use Ultralytics inside Docker in VS Code.
-
Please make sure to have the following directory structure:
ravi@dell:~/ultralytics_ws$ tree -a . ├── .devcontainer │ ├── devcontainer.json │ └── Dockerfile └── src
-
Below is the content of
devcontainer.json
{ "name": "Ultralytics Container", "privileged": true, "remoteUser": "ravi", "build": { "dockerfile": "Dockerfile", "args": { "USERNAME": "ravi" } }, "workspaceFolder": "/home/ws", "workspaceMount": "source=${localWorkspaceFolder},target=/home/ws/src,type=bind", "customizations": { "vscode": { "extensions":[ "twxs.cmake", "donjayamanne.python-extension-pack", "eamodio.gitlens" ] } }, "containerEnv": { "DISPLAY": "unix:0", }, "runArgs": [ "--net=host", "--ipc=host", "--gpus=all", "-e", "DISPLAY=${env:DISPLAY}" ], "mounts": [ "source=/tmp/.X11-unix,target=/tmp/.X11-unix,type=bind,consistency=cached", "source=/dev/dri,target=/dev/dri,type=bind,consistency=cached" ], "postCreateCommand": "sudo chown -R $(whoami) /home/ws/" }
Do not forget to update the
remoteUser
andUSERNAME
as per the username of your machine. -
Below is the content of
Dockerfile
FROM ultralytics/ultralytics:latest ARG USERNAME=USERNAME ARG USER_UID=1000 ARG USER_GID=$USER_UID # Set the timezone to Tokyo. Otherwise, it asks to set the geographic area later ENV TZ=Asia/Tokyo RUN ln -snf /usr/share/zoneinfo/$CONTAINER_TIMEZONE /etc/localtime && echo $CONTAINER_TIMEZONE > /etc/timezone # Create the user RUN groupadd --gid $USER_GID $USERNAME \ && useradd --uid $USER_UID --gid $USER_GID -m $USERNAME \ # # [Optional] Add sudo support. Omit if you don't need to install software after connecting. && apt-get update \ && apt-get install -y sudo \ && echo $USERNAME ALL=\(root\) NOPASSWD:ALL > /etc/sudoers.d/$USERNAME \ && chmod 0440 /etc/sudoers.d/$USERNAME RUN apt-get update && apt-get upgrade -y RUN apt-get install -y python3-pip # Install linux packages # g++ required to build 'tflite_support' and 'lap' packages, libusb-1.0-0 required for 'tflite_support' package RUN apt update \ && apt install --no-install-recommends -y gcc git zip curl htop libgl1 libglib2.0-0 libpython3-dev gnupg g++ libusb-1.0-0 # Security updates # https://security.snyk.io/vuln/SNYK-UBUNTU1804-OPENSSL-3314796 RUN apt upgrade --no-install-recommends -y openssl tar # Install pip packages RUN python3 -m pip install --upgrade pip wheel # Install OpenCV RUN apt install -y libopencv-dev python3-opencv # Needed for imshow in OpenCV RUN apt-get update && apt-get install -y libsm6 # Set environment variables ENV OMP_NUM_THREADS=1 # Avoid DDP error "MKL_THREADING_LAYER=INTEL is incompatible with libgomp.so.1 library" https://github.com/pytorch/pytorch/issues/37377 ENV MKL_THREADING_LAYER=GNU ENV SHELL /bin/bash # [Optional] Set the default user. Omit if you want to keep the default as root. USER $USERNAME CMD ["/bin/bash"]
-
Inside VS Code, please search in Extensions or press CTRL+SHIFT+X for the
Remote Development
Extension and install it. -
Open VS Code at the project root as shown below:
ravi@dell:~/ultralytics_ws$ code .
-
Press CTRL+SHIFT+P to open the command palette and search for the command
Dev Containers: Reopen in Container
and execute it. Please wait as it takes some time. -
You can open a terminal inside VS Code and execute various commands as show below:
ravi@dell:/home/ws$ yolo version 8.2.11