简体   繁体   中英

Can not find path to host volume in docker

I am working on an application in spring boot in which photos will be uploaded to the server and then read from the server using the url. The application is on the docker and tomcat server. From what I have learned the best way would be to store photos on docker volume using the host. So using the docker-compose.yml file I create the volume and give the path to the host. File fragment:

version: '3.8'
services:
  my-app:
    image: app
    environment:
      - _JAVA_OPTIONS=-Xmx512m -Xms256m
      - SPRING_PROFILES_ACTIVE=prod,api-docs
      .
      .
      .
    ports:
      - 8085:8085
    networks:
      default: 
        ipv4_address: 172.19.0.2
    volumes:
       - ./var/home/my-app:/var/lib/docker/containers/docker_my-app  //"path to the host:path to the docker container"

From what I read on the Docker website there , this is how you should create a path to the host where the photos will be stored, ie on the left the path to the host, and on the right the path to the container. The problem is that no path is created or is, but I can't find it. I also have a method that runs every time it checks to see if a path exists:

@PostConstruct
public void init() {
    if (!Files.exists(Paths.get("./var/home/my-app"))) {
        try {
            Files.createDirectory(root);
        } catch (IOException e) {
            throw new StorageException("Error initializing directory: " + e.getMessage());
        } 
}

And this throws that path does not exists.

So, I have questions:

  • Am I creating a good path to the host?
  • Or should the path to the host be different?
  • If host will be with read write permissions should I add something in docker-compose.yml file?

You're currently mounting the folder /var/home/my-app from your host machine to the folder /var/lib/docker/containers/docker_my-app inside the container.

As your application is running inside a container the method you've written

@PostConstruct
public void init() {
    if (!Files.exists(Paths.get("./var/home/my-app"))) {
        try {
            Files.createDirectory(root);
        } catch (IOException e) {
            throw new StorageException("Error initializing directory: " + e.getMessage());
        } 
}

creates the directory structure /var/home/my-app inside the container filesystem.

If your Java code is written to save data under the folder /var/home/my-app you should create your volume in the following way:

volumes:
   - /var/home/my-app:/var/home/my-app

Assuming your check-method is inside your Spring application, ie, inside the my-app container, you need to check for the

/var/lib/docker/containers/docker_my-app

folder, instead of the

./var/home/my-app

folder:

@PostConstruct
public void init() {
  if (!Files.exists(Paths.get("/var/lib/docker/containers/docker_my-app"))) {
    try {
        Files.createDirectory(root);
    } catch (IOException e) {
        throw new StorageException("Error initializing directory: " + e.getMessage());
    } 
}

On the first use of the volume-folder, the respective folder on the host should be created (as you are using the short-notation for the volume definition). The folder that should be created would be alongside your docker-compose.yml file, named var/home/my-app

If that is what you intended, then I would say, you created a good path to the host.

Regarding your third question:

If host will be with read write permissions should I add something in docker-compose.yml file?

That depends on whether you defined a user to isolate your container. Have a look here: https://docs.docker.com/engine/security/userns-remap/ Unless you did something like this, your process inside the container is running as root and can then write on your host regardless of missing permissions.

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