---
title: How to Access the Raspberry Pi Camera in Docker
description: This tutorial provides step-by-step instructions for accessing the Raspberry Pi Camera in Docker. Learn more.
---

[All Things IoT | Losant Blog ](https://www.losant.com/blog)

# [How to Access the Raspberry Pi Camera in Docker](https://www.losant.com/blog/how-to-access-the-raspberry-pi-camera-in-docker)

 Written by [Brandon Cannaday](https://www.losant.com/blog/author/brandon-cannaday) | Thu, Apr 30, 2020

[Docker](https://www.docker.com/) is an increasingly important component used to distribute software, especially for complex use cases in [Edge Computing](https://docs.losant.com/edge-compute/overview/) and IoT. The [Raspberry Pi Camera](https://www.raspberrypi.org/products/camera-module-v2/) offers a compelling and easy-to-access source of image data when performing early development and proving IoT concepts. However, gaining access to the camera in Docker can be a difficult process.

This tutorial provides step-by-step instructions for accessing the Raspberry Pi Camera from inside a Docker container.

Image Source: [Raspberry Pi](https://www.raspberrypi.org/products/camera-module-v2/)

## How to Let Non-Root Users Access the Raspberry Pi Camera

The Raspberry Pi Camera is treated like any other Linux device. When it's attached, it's mounted at `/dev/vchiq`. With a base installation of Raspbian, the camera device is only accessible by the "pi" user and the "root" user. Since Docker containers have their own set of users, any attempt to use the camera will be met with an "Access Denied" error.

Let's first start by understanding why the camera is only accessible by the "pi" and "root" users. Linux uses a system called [udev](https://en.wikipedia.org/wiki/Udev) for device management. Among many other tasks, udev has a set of rules that apply permissions whenever a device is attached. Raspbian comes with a udev rule for the camera, which can be viewed at `/lib/udev/rules.d/10-local-rpi.rules`.

```
SUBSYSTEM=="vchiq", GROUP="video", MODE="0660"SUBSYSTEM=="vc-sm", GROUP="video", MODE="0660"SUBSYSTEM=="bcm2708_vcio", GROUP="video", MODE="0660"
```

The "root" user will always have access to any Linux device, but the rules shown above are also granting read and write access (0660) to any user in the "video" group. Since the "pi" user is a member of the "video" group, it also has access to the camera.

In Raspbian, and most other Debian-base Linux distributions, the built-in udev rules are located in `/lib/udev/rules.d`. These rules can't be changed, however udev does allow us to add additional rules in `/etc/udev/rules.d`.

In order to grant all users access to the Raspberry Pi Camera, create the file `/etc/udev/rules.d/99-camera.rules` with the following content:

```
SUBSYSTEM=="vchiq",MODE="0666"
```

It's common practice to prefix udev rule files with a number. This is because udev rules are executed in alphabetical order, and using a number prefix allows us to control that order. We're using "99" because we want the built-in rules to execute before our custom rule.

The rule we added looks very similar to the built-in rule, with two exceptions:

1. The "GROUP" statement is removed so the camera can be accessed by users outside of the "video" group.
2. The "MODE" is changed from "0660" to "0666". Changing the last number from a "0" to a "6" grants read/write access to all users. Feel free to read up on [Linux permissions](https://en.wikipedia.org/wiki/File_system_permissions) to learn more about how that works.

All Docker containers will now have access to the camera device. The device can be passed into the container using Docker's `--device` flag:

```
--device /dev/vchiq
```

## How to Mount the Raspberry Pi Camera Dependencies into a Docker Container

The Raspberry Pi Camera tools like [PiCamera](https://picamera.readthedocs.io/en/release-1.13/) and [raspistill](https://www.raspberrypi.org/documentation/usage/camera/raspicam/raspistill.md) require several dependencies in order to work. Raspbian has all of these dependencies pre-installed in the `/opt/vc` folder. Instead of attempting to build these into the Docker container itself, it's much easier to mount these in from the host file system.

```
-v /opt/vc:/opt/vc
```

Inside the `/opt/vc` directory is another directory, `lib`, that contains a bunch of [shared libraries](https://tldp.org/HOWTO/Program-Library-HOWTO/shared-libraries.html). By default, Linux won't use this directory unless we instruct it to. The easiest way to do that is by defining the `LD_LIBRARY_PATH` environment variable:

```
--env LD_LIBRARY_PATH=/opt/vc/lib
```

Without this environment variable set, attempting to use one of the Raspberry Pi Camera tools will result in the following error:

```
OSError: libbcm_host.so: cannot open shared object file: No such file or directory
```

## How to Access the Raspberry Pi Camera Using Python in a Docker Container

Now that we have permission to access the camera device and all required dependencies, we can use the camera to capture images. To access the camera using Python, the Docker container requires the [PiCamera module](https://picamera.readthedocs.io/en/release-1.13/) to be installed.

Building a Docker container is outside the scope of this article, however the following is an example Dockerfile that would result in a Python container with PiCamera installed:

```
FROM python:3ADD my_script.py /RUN pip install picameraCMD [ "python", "./my_script.py" ]
```

For more details about building a Python-based Docker container, check out the [Dockerize your Python Application](https://runnable.com/docker/python/dockerize-your-python-application) article.

Because of the groundwork we've already laid, the PiCamera module will be able to directly access the camera using any Python script inside the Docker container. The following snippet demonstrates how to capture a basic image using PiCamera:

```
from time import sleepfrom picamera import PiCameracamera = PiCamera()camera.resolution = (1024, 768)camera.start_preview()sleep(2)camera.capture('output.jpg')exit()
```

## How to Access the Raspberry Pi Camera Using raspistill in a Docker Container

The [raspistill](https://www.raspberrypi.org/documentation/usage/camera/raspicam/raspistill.md) CLI tool makes it easy to perform basic image capturing using the Raspberry Pi camera without writing code. Because raspistill is installed in `/opt/vc/bin`, we've already made it available inside the Docker container when we mounted in the camera dependencies.

To use raspistill inside the Docker container, execute the following command:

```
/opt/vc/bin/raspistill -o output.jpg
```

## Use the Raspberry Pi Camera as an IoT Data Source

The application in the screenshot below uses the [Losant Enterprise IoT Platform](https://www.losant.com/iot-platform) to receive, store, and process raw image data from a Raspberry Pi Camera to visualize and understand parking trends.

The Raspberry Pi is running [Losant's Edge Agent](https://docs.losant.com/edge-compute/edge-agent-installation/) to handle the connection and secure reporting of image data to the cloud. An [Application Workflow](https://docs.losant.com/workflows/overview/) is then used to send the image data to [Google's Vision API](https://cloud.google.com/vision) to detect cars.

To learn more about building this application, see the following tutorials:

1. [How to Access the Raspberry Pi Camera using the Losant Edge Agent](https://www.losant.com/blog/how-to-access-the-raspberry-pi-camera-from-the-losant-edge-agent)
2. [Analyzing IoT Image Data using Losant and the Google Vision API](https://www.losant.com/blog/analyzing-iot-image-data-using-losant-and-the-google-vision-api)

Losant provides a full-featured and no-cost [Developer Sandbox](https://www.losant.com/iot-platform/sandbox?utm_campaign=Blog&utm_source=Brandon_Turtorial_RaspberryPi) where you can explore the platform yourself. If you'd like to learn more about Losant or talk to one of our team members, please [contact us](https://www.losant.com/contact-us)!

[View full post](https://www.losant.com/blog/how-to-access-the-raspberry-pi-camera-in-docker)

```json
{
  "@context" : "http://schema.org",
  "@type" : "BlogPosting",
  "author" : {
    "@type" : "Person",
    "name" : "Brandon Cannaday"
  },
  "dateModified" : "2022-03-10T22:30:03.190Z",
  "datePublished" : "2020-04-30T20:58:18Z",
  "headline" : "How to Access the Raspberry Pi Camera in Docker",
  "image" : {
    "@type" : "ImageObject",
    "height" : 500,
    "url" : "https://cdn2.hubspot.net/hubfs/742943/Blog/tutorials/how-to-access-raspberry-pi-camera-docker/raspberry-pi-camera-1.jpg",
    "width" : 1000
  },
  "mainEntityOfPage" : "https://www.losant.com/blog/how-to-access-the-raspberry-pi-camera-in-docker",
  "publisher" : {
    "@type" : "Organization",
    "logo" : {
      "@type" : "ImageObject",
      "height" : 60,
      "url" : "/hs/hsstatic/content_shared_assets/static-1.4092/img/default-amp-logo.png",
      "width" : 60
    },
    "name" : "All Things IoT"
  }
}
```