问题
I'm new to docker-compose and after reading the docs I still have some unclear things that comes to my mind.
So far when I used docker I kept the builds in the following directory tree:
builds:
Service A:
Dockerfile
ServiceA.jar
Service B:
Dockerfile
ServiceB.jar
So when I want to run all I use some shell script, until I read about docker-compose.
I saw that there are 2 ways of creating and running a service (in manner of copying files)
- Specifying
build: path/to/my/build/directoryand linking it withvolumes:so it can see live code and refresh the service - Specifying
image:(for examplejava:8) and then use thevolumes:as above
What I want to understand is what is the best practice using docker-compose
before I dive in to it, should I create specify image for each service (and replace with the FROM inside the Dockerfile) or should I specify paths to build folders and volumes to keep live code changes, and how does volumes work and their usages when using image tag
Thanks!
回答1:
In Docker you can simply run services as containers and you can put the state of each service in a volume. This means for you:
- The service is run as a runtime container which will be started from an image.
- The service's binaries are inside an image, the service itself writes data to volumes.
- The image can be either pulled from a image repository or build on the target environment.
- Images can be build with the command
docker buildor the docker-compose build section.
In your example this means:
- Keep the directory structure.
- Use the docker-compose build section to build your immages according your Dockerfiles.
- Configure your Dockerfile to put the binaries inside the image.
- Simply start the whole stack including the build with
docker-compose up -d - Your binaries changed? Simply replace the whole stack with
docker-compose up --build --force-recreate -d. This command will rebuild all images and replace containers.
Why you do not place the binaries inside the volume:
- You lose the advantage of image versioning.
- If you replace the binaries files you cannot simply fallback to an older image version.
- You can retag images before you deploy your new version and fallback if an error occurs.
- You can tag and save running containers for fallback and error investigation.
来源:https://stackoverflow.com/questions/45786256/docker-compose-specifying-image-vs-dockerfile