Install YOLOv8 Inside Docker Container in Linux

Install YOLOv8 Inside Docker Container in Linux

YOLO (You Only Look Once) is an algorithm for object detection, image segmentation and classification. YOLOv8 is 8th version of YOLO which introduced by Ultralytics in January 2023.

This tutorial explains how to install YOLOv8 inside a Docker container in the Linux. Commands have been tested on Ubuntu.

Prepare environment

Make sure you have installed Docker in your system. If you are using Ubuntu, installation instructions can be found in the post.

Create Docker image

Create Dockerfile and add the following content:

FROM nvidia/cuda:12.0.0-runtime-ubuntu22.04

WORKDIR /yolov8

ADD /root/.config/Ultralytics/

RUN apt-get update \
    && apt-get install --no-install-recommends -y  \
        libgl1-mesa-glx libglib2.0-0 python3 python3-pip \
    && pip3 install ultralytics \
    && rm -rf /var/lib/apt/lists/*


The nvidia/cuda is used as a base image. So, make sure you have NVIDIA graphics card on the system.

Run command to create Docker image:

docker build -t yolov8 .

Testing YOLOv8

Create directory to store input images:

mkdir -p ~/yolov8/inputs

Download sample image:

curl -o ~/yolov8/inputs/test.jpg

Run the following commands in YOLOv8 container to perform required task:

  • Object detection:
docker run -it --rm \
    -v ~/yolov8:/yolov8 \
    yolov8 detect predict save source=inputs/test.jpg

Output directory: ~/yolov8/runs/detect.

  • Image segmentation:
docker run -it --rm \
    -v ~/yolov8:/yolov8 \
    yolov8 segment predict save source=inputs/test.jpg

Output directory: ~/yolov8/runs/segment.

  • Image classification:
docker run -it --rm \
    -v ~/yolov8:/yolov8 \
    yolov8 classify predict save source=inputs/test.jpg

Output directory: ~/yolov8/runs/classify.

Few results:

Detection, segmentation, classification using YOLOv8

Remove Docker image

To remove YOLOv8 image, run the following command:

docker rmi yolov8

The 2 Comments Found

  1. Avatar
    Michael Bushey Reply
    docker run -it --rm -v /file/data/yolov8:/yolov8 yolov8 detect predict source=inputs/test.jpg
    Ultralytics YOLOv8.0.65  Python-3.10.6 torch-2.0.0+cu117 CPU
    YOLOv8s summary (fused): 168 layers, 11156544 parameters, 0 gradients, 28.6 GFLOPs
    image 1/1 /yolov8/inputs/test.jpg: 384x640 1 cat, 1 bed, 51.6ms
    Speed: 0.3ms preprocess, 51.6ms inference, 0.7ms postprocess per image at shape (1, 3, 640, 640)
    ls -l inputs
    total 57
    -rw-r--r-- 1 michael michael 50652 Apr  5 15:35 test.jpg

    Is there any way to get it to save the output? You show Output directory: ~/yolov8/runs/detect I manually created the runs directory, but nothing is saved in it.

    • Avatar
      lindevs Reply

      YOLOv8 CLI has been changed. Now, output images are not saved by default. Use save argument to do that. I updated the post regarding this. In your case, images will be saved to the /file/data/yolov8/runs/detect directory. Also, make sure permissions are correct.

Leave a Comment

Cancel reply

Your email address will not be published.