简体   繁体   中英

File not being synced from container to host in docker-compose

So, I am using docker-compose to run a simple flask app alongside nginx.

Here is the docker-compose.yml :

version: '3.2'
services:
    nginx:
        image: nginx:latest
        container_name: nginx
        ports:
            - 80:80
        volumes:
            - ./nginx.conf:/etc/nginx/conf.d/default.conf
        depends_on:
            - flask
        networks:
            - my-network
    flask:
        build:
          context: .
          dockerfile: Dockerfile
        image: webapp-flask
        container_name: flask
        volumes:
            - type: bind
              source: ./
              target: /app
            - type: bind
              source: ./users.json
              target: /app/users.json
        networks:
            my-network:
                aliases:
                    - flask-app
        ports:
            - 8080:5000
networks:
    my-network:

In the volumes: section I am trying to bind the current host directory to /app directory on the host, also the users.json file (which I want to be synced)

Here is the Dockerfile for the flask:

FROM python:3
RUN pip install flask flask-restful
EXPOSE 5000
CMD python app/app.py

The app.py makes changes to the users.json as requests are made. Everything is working fine except the fact that when the users.json file is updated on the container (via a request, let's say), the update does not reflect on my host machine.

Just for information, I am using Windows and the Docker Toolbox. Is there anything special that I need to do to bind users.json between host and my container?

EDIT: This problem was due to my own stupidity, first problem is that in my Dockerfile I use the python:3 image and then in my docker-compose.yml I again used a new image of webapp-flask . I don't know why this wasn't conflicting in the first place, but apparently then real problem was that whenever I was running docker-compose up it used a cached build of my older files and never rebuilt the container. I figured this out when I made changes to my app.py and they didn't take effect when running the whole thing. So, all I did was to remove the older containers, remove the line where it specifies the image in the docker-compose.yml and it worked.

Akhand Mishra ,

Few questions:

  • Where does the users.json file live on your host system? Is it outside of your application directory? (If not, then)
  • Is there any reason that the volume configuration cannot be simplified to bind the entire application directory to the container? It appears you are already doing this, but also binding the users.json file as an additional target.

My recommendation would be to possibly make an additional /data/ directory on your host machine which could be used to house the users.json file. So, something like this in your docker-compose.yml :

flask:
    build:
        context: .
        dockerfile: Dockerfile
    image: webapp-flask
    container_name: flask
    volumes:
        - .:/app/
        - .:/data/
    networks:
        my-network:
            aliases:
                - flask-app
    ports:
        - 8080:5000

I'm unsure if volumes can be mounted to an explicit singular file via compose, and suspect that it may need to be an entire directory that is called out in the volume yaml keys.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM