Open uniquejava opened 4 years ago
github: https://github.com/BretFisher/docker-mastery-for-nodejs
# Run through simple compose commands
$ cd sample-02
$ docker-compose up
$ ctrl-c (same as docker-compose stop)
$ docker-compose down
$ docker-compose up -d
$ docker-compose ps
$ docker-compose logs
# While app is running detached...
$ docker-compose exec web sh
$ curl localhost
$ exit
# edit Dockerfile, add curl with apk
$ RUN apk add --update curl
$ docker-compose up -d
# curl还是不会安装, 因为已经build过一次, 重新build需指定--build
$ docker-compose up -d --build
# Now try curl again
$ docker-compose exec web sh
$ curl localhost
$ exit
# Cleanup(Inside sample-02 directory)
$ docker-compose down
see: https://stackoverflow.com/a/47152658/2497876
We basically have 3 types of volumes or mounts for persistent data:
Bind mounts
Named volumes
Volumes in dockerfiles
Bind mounts are basically just binding a certain directory or file from the host inside the container (docker run -v /hostdir:/containerdir IMAGE_NAME
)
Named volumes are volumes which you create manually with docker volume create VOLUME_NAME
. They are created in /var/lib/docker/volumes
and can be referenced to by only their name. Let's say you create a volume called "mysql_data", you can just reference to it like this docker run -v mysql_data:/containerdir IMAGE_NAME
.
And then there's volumes in dockerfiles, which are created by the VOLUME
instruction. These volumes are also created under /var/lib/docker/volumes
but don't have a certain name. Their "name" is just some kind of hash. The volume gets created when running the container and are handy to save persistent data, whether you start the container with -v
or not. The developer gets to say where the important data is and what should be persistent.
What should I use?
What you want to use comes mostly down to either preference or your management. If you want to keep everything in the "docker area" (/var/lib/docker
) you can use volumes. If you want to keep your own directory-structure, you can use binds.
Docker recommends the use of volumes over the use of binds, as volumes are created and managed by docker and binds have a lot more potential of failure (also due to layer 8 problems).
If you use binds and want to transfer your containers/applications on another host, you have to rebuild your directory-structure, where as volumes are more uniform on every host.
First glance
v2 vs v3
docker-compose.yml (默认文件名)
node.js
mysql
docker-compose up
--build
to always builddocker-compose down
-v
to delete volumesdocker-compose build/stop
docker-componse build --no-cache
重新执行Dockerfile中的每一行命令