From container to host

Hello,
Is it possible to mount a file or directory from the container to the host? For example, what should I do if I need the Nginx log files real-time?

Thank you.

Yes, that would be a bind mount
It generally follows this pattern: hostpath:containerpath:permissions

Using docker run

docker run -v /host/mount/path:/container/mount/path image [command]

Using docker compose:

services:
  my-service:
    image: some/image:latest
    volumes:
      - /host/mount/path:/container/mount/path
      # - hostpath:containerpath:permissions

Using Docker Desktop, the paths can be selected within the Optional settings window upon starting a new container

For nginx, I believe the default log path (Assuming it is enabled) is /var/log/nginx

Hello,
Thank you so much for your reply.
Did you understand my question? I want to mount a part of the container in the host, not a part of the host in the container!

There is no such option

If you have data on the container that you don’t want to lose, you can use docker cp to copy it over to your host machine, before mounting it over to any new container you create

If you want the nginx log files to be on your host machine, simply mount /some/host/path:/var/log/nginx

Any files created will be created on your host path

And as my previous message stated, if you want to save your current log files, you can use docker cp, before mounting it in the future

Hi,
Thank you so much for your reply.
I added the following lines to the YAML file:

 volumes:
        - "/var/run/docker.sock:/var/run/docker.sock"
        - /home/Containers/Nginx/log:/var/log/apt/

But, on the host, I don’t see anything in the log folder.

Hello,
I need the log files in real time.

Thanks.

You’re mounting /var/log/apt instead of /var/log/nginx

Hi again,
Yes, I just wanted to test. Is there a problem?

No problem, but it is probably not the logs you’re looking for?

If this method worked, then the contents of that directory should be shown on the host.

No, the contents of the host’s directory will be shown on the container, not the other way around.

If the container had created files within that folder, then yes, those would appear on the host machine.

I do not think the nginx container creates any logs in the apt folder, but I may be wrong

So, this is not useful for Nginx, because the log files are already there.

nginx does create log files if they do not exist (Which is the case if you mount an empty directory), but it uses /var/log/nginx

I don’t think there is any way to mount something from inside the container to inside the host.