'Docker stack isn't updating the folder structure with new image
Recently I have changed my dockerfile to use a cleaner folder structure but this isn't being updated in the stack deploy
My folder structure:
├── Dockerfile.dev
├── Dockerfile.prod
├── env/
├── requirements.txt
├── src
│ ├── app
│ │ ├── __init__.py
│ │ ├── modules
│ │ ├── __pycache__
│ │ ├── services
│ │ └── util
│ ├── __init__.py
│ └── main.py
└── version.conf
Docker compose file (recorder api part):
api-recorder:
image: img-api-recorder:latest
build:
context: ../api-recorder-python/
dockerfile: Dockerfile.${DOCKER_ENV}
deploy:
mode: replicated
replicas: 1
restart_policy:
condition: on-failure
placement:
constraints: [node.role == manager]
volumes:
- ${BASE_DIR}api-recorder-python:${WORKDIR}
depends_on:
- zookeeper
environment:
PYTHON_ENV: ${DOCKER_ENV}
Old Dockerfile:
FROM python:3
WORKDIR /usr/src/app
RUN apt-get update
RUN pip install --upgrade pip
RUN pip install kafka-python
RUN pip install python-dotenv
RUN pip install pymongo pymongo[srv]
RUN pip install psycopg2
RUN ln -snf /usr/share/zoneinfo/America/Sao_Paulo \
/etc/localtime && \
echo "America/Sao_Paulo" > /etc/timezone
COPY . .
CMD ["python3", "-u", "src/main.py"]
So what I did was create the requirements.txt and changed the COPY command
New Dockerfile:
FROM python:3
WORKDIR /usr/src
COPY ./requirements.txt ./
RUN apt-get update
RUN pip install --upgrade pip
RUN pip install -r requirements.txt
RUN ln -snf /usr/share/zoneinfo/America/Sao_Paulo \
/etc/localtime && \
echo "America/Sao_Paulo" > /etc/timezone
COPY ./src ./
# CMD ["python3", "-u", "main.py"]
CMD ["python3", "-m", "http.server"]
The weird thing is that the new Dockerfile is being built correctly in a new image because if I run docker run -it [image_name]:latest bash and list the directories I receive this:
which is the new structure made by the new Dockerfile on the other hand if I run the stack deploy and enter inside the container I will be in the /usr/src path and it will have a wrong structure:
The content inside the app folder is wrong, it should have the program code inside it How can I clean it? I already tried delete all the volumes, images, containers, I even reinstalled docker, I don't know what else to do.
Solution 1:[1]
Your docker compose has both an image and build section. Precedence is given to the image so that is being used not the build.
You probably want:
api-recorder:
build:
context: ../api-recorder-python/
dockerfile: Dockerfile.${DOCKER_ENV}
deploy:
mode: replicated
replicas: 1
restart_policy:
condition: on-failure
placement:
constraints: [node.role == manager]
volumes:
- ${BASE_DIR}api-recorder-python:${WORKDIR}
depends_on:
- zookeeper
environment:
PYTHON_ENV: ${DOCKER_ENV}
Solution 2:[2]
I discovered what is happening.
In my docker-compose file I was setting a volume that was overwriting my path, this is happening because my WORKDIR environment is set to /usr/src/app which is the same path that I set in my dockerfile but in the docker-compose this path is being mirrored to my api-recorder-python folder structure
So the only thing that I did was change the volume to ${BASE_DIR}api-recorder-python/src:${WORKDIR} and I changed my WORKDIR to /usr/src and all worked fine
Here:
api-recorder:
image: img-api-recorder:latest
build:
context: ../api-recorder-python/
dockerfile: Dockerfile.${DOCKER_ENV}
deploy:
mode: replicated
replicas: 1
restart_policy:
condition: on-failure
placement:
constraints: [node.role == manager]
volumes:
- ${BASE_DIR}api-recorder-python/src:${WORKDIR}
depends_on:
- zookeeper
environment:
PYTHON_ENV: ${DOCKER_ENV}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Belly Buster |
| Solution 2 | CAIO WANDERLEY |


