Home > Software engineering >  docker-compose - cannot use bind mount in folder created when using BUILD
docker-compose - cannot use bind mount in folder created when using BUILD

Time:01-01

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.

CodePudding user response:

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...

CodePudding user response:

I ended up with adding to the container the sourcecode directory to fix this problem. @NiRR answer helped a lot.

The final Dockerfile was changes to not include sourcecode in the image:

FROM ubuntu:trusty
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1
ARG DEBIAN_FRONTEND=noninteractive
SHELL ["/bin/bash", "-c"]
RUN apt-get update -y && apt-get upgrade -y 
RUN apt-get install -y 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

WORKDIR /workspace/src
COPY myapp/src/requirements.txt .
RUN /bin/bash -c "pip3 install cffi \
    && pip3 install -r requirements.txt"

# To set timezone
RUN ln -sf /usr/share/zoneinfo/America/Sao_Paulo /etc/localtime

And I changed the docker-compose to the following final version:

version: "3.9"
services:
  web:
    build: .
    command: ./start.sh
    environment:
      - DB_HOST=db
      - DB_NAME=***
      - DB_USER=***
      - DB_PASS=***
    volumes:
      - ./myapp:/workspace
  • Now in the container start all the sourcecode from myapp is copied to inside the container;
  • Everything is under GIT control
  • If the code changes, we can make a push/pull and docker-compose up -d to restart the container. The new version will already be there.
  • Related