Sync files from docker container to host using Dockerfile or docker-compose

Viewed 197

I am new to docker and I am just done with making an ubuntu container and copying the content from host(my local machine) to the the ubuntu container. But as I am making some directories/files in work directory in the container, I can see it is not getting synced to my the host folder, which I initially copied.

I want this sync as I use docker system prune or docker rmi -f image_name frequently for some testing purposes, which eventually deletes the content and then I have no backup of those files.

I have used COPY to copy files from host to container but need help with the reverse, so that it can happen automatically everytime before I delete the container/image.

Any working solution on this would be very appreciable.

Here is my docker-compose file

version: '3'
services:
    ubuntu:
        build:
            context: .
            dockerfile: Dockerfile
        image: ubuntu
        ports:
            - 8091
        volumes:
            - ./dir:/dir
volumes:
    dir:
        external: false

Here is my Dockerfile

FROM ubuntu:latest
WORKDIR /dir
VOLUME /dir

RUN apt-get update
RUN apt-get install -y

EXPOSE 8000
COPY . .
CMD ["/bin/bash"]

Solution: I just found the solution

docker cp <containerId>:/file/path/within/container /host/path/target

This is the command which you need to use to copy content from container to the host machine.

0 Answers
Related