DEV Community

Frederick Ollinger
Frederick Ollinger

Posted on

Docker Compose Access A Mounted Volume Inside a Container

Problem: Using a container to build my project but I can't access the mounted volume due to permission issues.

Assumptions: This tutorial assumes you are working on a reasonably up to date Linux box with Docker and Docker Compose installed, and that you have a basic familiarity with the Linux command line.

A mounted volume is when you mount a directory from your filesystem inside a container. This allows you to access files which were not available when you built the container. Also, you have access to the build artifacts outside the container after building inside the containter.

Docker Compose File:

version: '3.8'
services:
  gcc:
    image: "gcc"
    command: /bin/bash
    group_add:
        - $GID
    volumes:
      - ${PWD}/fullbuild:/root/fullbuild
Enter fullscreen mode Exit fullscreen mode

This file will pull the official gcc Docker container. It expects the group user id to be $GID and the source code to build to be at fullbuild.

We usually make the $GID the same as the default group for the user who is currently running docker-compose on the host system.

If we do not have the fullbuild directory make it:

    mkdir -p fullbuild
Enter fullscreen mode Exit fullscreen mode

The copy any source code in.

Before we run the function, we need to ensure that our default group has access to both fullbuild and its subdirs:

find fullbuild -type d -exec chmod g+rwx {} +
Enter fullscreen mode Exit fullscreen mode

Finally, when we run and enter the container, ensure that $GID is set:

GID=`id -g` docker-compose run gcc
Enter fullscreen mode Exit fullscreen mode

That's it. After we run the last command, we'll be inside a Docker container with access to gcc as well as having access to all the source code inside the container. Source code is in /root/fullbuild. Build artifacts will be available in fullbuild outside of the container.

Top comments (0)