Docker-compose: cannot build WITHOUT sudo but I can run containers without it

The cause of the problem is that you have a volume mount point with root:root owner & group and caused this behavior.

The solution to this problem is to create a file named .dockerignore and put all the folders mounted as volumes.

For example if you have the following docker-compose.yml

version: '2'
services:
  data_map_prod:
    build:
      context: .
      dockerfile: Dockerfile
    image: 'pcmagas/data-map:latest'
    links:
      - 'neo4j'
      - 'mongodb'
    volumes:
      - './docker-volumes/app_dev:/var/log/datamap'
    ports:
      - "9780:9780"
    environment:
      NEO4J_HOST: 'neo4j'
      MONGO_CONNECTION_STRING: 'mongodb://mongodb:map'

  data_map_dev:
    build:
      context: .
      dockerfile: Dockerfile_dev
    image: 'pcmagas/data-map:dev'
    links:
      - 'neo4j_dev'
      - 'mongodb'
    volumes:
      - './src:/opt/map/src'
      - './www:/opt/map/www'
      - './package.json:/opt/map/package.json'
      - './docker-volumes/app_dev:/var/log/datamap'
    ports:
      - "9781:9780"
    environment:
      NEO4J_HOST: 'neo4j_dev'
      NEO4J_USER: 'neo4j'
      NEO4J_PASSWORD: 'neo4j'
      MONGO_CONNECTION_STRING: 'mongodb://mongodb:map_dev'

  neo4j_dev:
    image: 'neo4j'
    ports:
      - '7474:7474'
    volumes:
      - './docker-volumes/neo4j_dev/data:/data'
    environment:
      NEO4J_AUTH: 'neo4j/neo45j'

  neo4j:
    image: 'neo4j'
    volumes:
      - './docker-volumes/neo4j/data:/data'
    environment:
      NEO4J_AUTH: 'neo4j/neo45j'

  mongodb:
    image: 'mongo'
    ports:
      - '27017:27017'
    volumes:
      - './docker-volumes/mongodb/:/data/db'

Then you should create the following .dockerignore:

./docker-volumes

As you can see all the volumes are in ./docker-volumes folder.

Furtermore you can find solutions in: https://stackoverflow.com/questions/29101043/cant-connect-to-docker-from-docker-compose#29111083


One of the reason is that any file used to build the docker image is not owned by the current user. We can change this by using this command:

sudo chown -R $USER .