0

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.

1
  • If you bind-mount a host directory over a container directory, whatever contents existed in the host directory always hide what was built in the image. This means you don't usually want to bind-mount a directory over the directory that contains your actual application; maybe you want a separate /workspace/data directory instead. Commented Dec 30, 2021 at 19:25

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

1) yes, myapp is the folder with source code; 2) about the folder inside the container being replaced by the contents of the mapped folder, I understand that the other way around also works - thing in the postgresql container when the first time started, he copy the data folder inside the container to the host file system in the bind folder. Am I missing something?
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.