Git on docker volume

What are the options for running Git inside a Docker volume. I am not asking about running Git in the host directory. This is the commonly used way.

Do I need additional tools for that?

A named volume by itself is just (file) storage managed by the docker engine. A named volume consist of its declaration and a _data folder. In case of a local volume the data is stored in /var/lib/docker/volumes/${volume name}/_data and exist regardless whether the container is started or stopped. If the named volume is backed by a remote file share, the _data folder will be its mount point - it will be mounted when the container starts and unmounted when it stops.

At the end a named volume is also bind mounted into the container, the same way a volume-bind is. The differences are, that named volumes are listed by docker volume ls, will take care of the permissions for local volumes. If the volume is empty, prexisting data from the container target folder will be copied back into the volume, before bind mounted into the target folder. The volume will eclipse the target folder, so that only the data of the volume will be accessible in the target folder.

With this new information in mind, can you rephrase your question?

I think I understand, but I don’t know how in the /var/lib/docker/volumes/docker_wp_data/_data directory I want to initialize git init. How to execute a Git version repository in it?

I sense a lot of confusion here…

At the risk of repeating myself, but a volume is really just file storage supposed to be used by a container. It does not run any processes.

Also the _data folders are not mend for direct user interaction on the host. I just wanted to give you the full information. I realize I shouldn’t have done that. A container is supposed to interact with it. When you create a container you map the volume into a container path and indirectly access the volume using the container path. Everything else depends on your container or your interactions inside the container.

If the picture is still ambiguous. I would highly suggest exercising this free self-paced docker training: Introduction to Containers. It will give you a solid understanding of docker concepts and how things are done.

I understand you and I know what you wrote about.

This I will ask directly. How to initialize Git in Docker git init?

Due to a lack of a big picture, I can not answer your simple question. You never shared how your workflow is supposed to look like and how you intend to work with it.

I have no idea whether you use vscode and want to use it to initialize and checkout code in a development container, or whether you intend to work in the terminal of the container (and then of course have to install git inside the container) or something entirely different.

You do not forgive me :-). Here’s my docker-compose.yml

version: '3.8'

name: test

services:
  db:
    image: mariadb:latest
    volumes:
      - wordpress_baza:/var/lib/mysql
    environment:
      - MYSQL_ROOT_PASSWORD=somewordpress
      - MYSQL_DATABASE=wordpress
      - MYSQL_USER=wordpress
      - MYSQL_PASSWORD=wordpress
  wordpress:
    image: wordpress:latest
    ports:
      - 8081:80
    environment:
      - WORDPRESS_DB_HOST=db
      - WORDPRESS_DB_USER=wordpress
      - WORDPRESS_DB_PASSWORD=wordpress
      - WORDPRESS_DB_NAME=wordpress
    volumes:
      - wordpress_pliki:/var/www/html

volumes:
  wordpress_db:
  wordpress_files:

This is a WordPress installation. I want to write a plugin. In the /public_html/wp-content/plugins/test directory, I want to initialize git init. Help me understand how to accomplish this?

Thanks for sharing your compose file, even though it is not what I was asking for. I was trying to understand how your workflow looks like. Give by your answer, I understand you have no workflow.

You might want to check Developing inside a Container using Visual Studio Code Remote Development, if using your WordPress container as development container suits your needs. It opens vscode with the work context inside the container, so you can directly edit container files in the vscode ui, use vscode it for git interactions and if wanted to access the containers terminal.

If this is not what you want, then you can still docker exec into your wordpress container and use git on the terminal. It might be necessary to install git, which depending on the image might be as easy as using the os’s package manager to install it, or might require you to create your own image based on the wordpress image.

This folder is not mapped as volume, writes to that folder will write directly into the container file system. You on the other hand only mapped a volume to /var/www/html. If you want things to be written into your volume, the files must be inside this folder or any of its subfolders OR add another volume to /publlic_html or one of it’s subfolders.

Are you sure you are not just looking to bind an existing host folder into a container folder? This way everything would happen on the host, git operations and file editing. The container would just see the content of the host folder you mapped to a container folder.

Thank you for your comprehensive answers. I just wanted to use without linking containers. From what I’ve learned, it’s best to link the /public_html/wp-content/plugins/test directory and run with git locally.

My question was related to the capabilities of Docker with git. I use Visual Code Studio, but wanted to know if it was possible to work directly in the CLI.

Since I see that you still use some terms in a confusing way, I would like to recommend the training that @meyay already recommended.

If you use the proper words you will be able to get answers much faster. Linking containers for example has nothing to do with volumes.

You can use git

  • in existing containers using docker exec
  • during the build (in Dockerfile)
  • on the host and just mount the folder from the host to the container when you create it

Each of the above ways could be required sometimes. It depends on your exact usecase. If you are still learning, mounting files could be easier, however, it can be problematic when you have to write and read the same files from the host and from the container as well.

If you have a “local volume”, not a “bind mount”, you can

  • use docker exec
    • install git if it was not installed during the build (Dockerfile)
    • go to the destination folder of the volume inside the container
    • and use git there.
  • or run an other container from an image which already contains git and mount the same volume to this temporary container as you mounted to your wordpress (or any application) container.

The first is easier, but the second can be better since if your application does not need git in the container, you don’t have to install and maintain it.