理解Dockerfile volume与docker-compose.yml volume

  • Dockerfile vs. docker-compose.yml

    1. Dockerfile reference
    2. Compose file version 3 reference

First we should know, Dockerfile is not docker-compose.yml.

Dockerfile is the file run by docker build -t image:tag . for making a docker image.

But the docker-compose.yml is the file run by docker-compose up for start a docker container or several containers.

They are both files that collecting many orders, which make the building process clearly and organized.

  • Volumes in docker

    From Volumes and Dockerfiles Don’t Mix , Volumes with Docker are a popular topic.

    1. Don’t Create Volumes Inside a Dockerfile

      The practice which Specifying a Volume through creating an image, has no upside, and lots of problems that it creates:

      • Issue#1 Anonymous Volumes

        Defining a volume inside the image tells docker to store this data separate from the rest of the container, even if you don’t define the volume when you spin up the container.

        Docker’s way of storing this data is to create a local volume without a name.

        The name itself is a long unique id string that contains no reference to the image or container it’s attached to.

        And unless you explictly tell docker to remove volumes when you remove the container, these volumes remain ,unlikely to ever be used again:

        # 查看dockerfile定义的volume
        docker volume ls
        # 删除dockerfile定义的volume
        
      • Issue#2 You Cannot Modify a Volume Once Created, Sometimes
      • Issue#3 Derived Images Cannot Modify the Volume Either
    2. You can Use Volumes Without Defining Them Inside the Image

      All of these issues begs the question:

      “If I shouldn’t define volumes inside the Dockerfile, then how can I use volumes in docker?”

      "use the docker-compose.yml or docker run "

      From docker-compse.yml or docker run methods, you can not only define your volume, but you can give it a name.

  • docker-compose.yml

    From Frequently asked questions on docker docs, The volume (docker-compose.yml) overrides the directory contents of the image.

  • References

  1. Dockerfile vs. docker-compose VOLUME
  2. What is the difference between YAML and JSON?

你可能感兴趣的:(Docker)