Tuesday, January 4, 2022

[SOLVED] docker-compose - cannot use bind mount in folder created when using BUILD

Issue

I have a docker-compose file which uses a Dockerfile to build the image. In this image (Dockerfile) I created the folder /workspace which I'd like to bind mount for persistence in my local filesystem.

After the docker-compose up, the folder is empty if I bind mount, but if I do not mount this folder everything works fine (and the folder exist with all the files I added).

This is my docker-compose.yml:

version: "3.9"
services:
  web:
    build: .
    command: uwsgi --ini /workspace/confs/uwsgi.ini --logger file:/workspace/logs/uswgi.log --processes 1 --workers 1 --plugins-dir=/usr/lib/uwsgi/plugins/ --plugin=python
    environment:
      - DB_HOST=db
      - DB_NAME=***
      - DB_USER=***
      - DB_PASS=***
    depends_on:
      - db
      - redis
      - memcached
    volumes:
      - ./workspace:/workspace
    networks:
      - asyncmail
      - traefik
# db, redis and memcached are ommited here
# aditional labels for traefik is also ommited

This is my Dockerfile:

FROM ubuntu:trusty
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1
SHELL ["/bin/bash", "-c"]
RUN mkdir /workspace
RUN apt-get update -y && apt-get upgrade -y 
RUN apt-get install -y redis-server python3-pip git-core postgresql-client
RUN apt-get install -y libpq-dev python3-dev libffi-dev libtiff5-dev zlib1g-dev libjpeg8-dev libyaml-dev libpython3-dev openssh-client uwsgi-plugin-python3 libpcre3 libpcre3-dev uwsgi-plugin-python

ADD myapp /workspace/
WORKDIR /workspace/src/
RUN /bin/bash -c "pip3 install cffi \
    && pip3 install -r /workspace/src/requirements.txt \
    && ./manage.py collectstatic --noinput"

RUN ln -sf /usr/share/zoneinfo/America/Sao_Paulo /etc/localtime

# CMD ["uwsgi", "--ini", "/workspace/confs/uwsgi.ini", "--logger", "file:/workspace/logs/uswgi.log"]
  • I know there is some items it could be optimized, but when I do a docker-compose up -d the folder ./workspace is created with only a folder inside called src. Inside the container the /workspace only have this empty folder too;
  • If I remove the volumes line in docker-compose, inside the container, the folder /workspace have all the sourcecode of my app.

What am I doing wrong that I can't bind mount the workspace folder?

PS: I know this image i'm using (ubuntu trusty) is old, but my old app only run with this version.


Solution

am I correct in assuming that the files you want to appear inside workspace are actually in a folder called "myapp" in your host machine (it seems so from this line)

ADD myapp /workspace/

I think you meant to map that into your docker container, so under volumes

volumes:
      - ./myapp:/workspace

volume maps work one way, that is the folder inside the container is replaced by the contents of the mapped folder on the host, not the other way around...



Answered By - NiRR