He is also a technical writer and works as a Node.js consultant, helping clients build and maintain their Node.js applications. If you're interested, feel free to follow along.Kelvin Omereshone Follow Kelvin is an independent software maker currently building Sailscasts, a platform to learn server-side JavaScript. In the next article, we will discuss how to split an application into multiple containers and have them work together to compose your application. Volumes will become a common practice in the upcoming chapters as we focus on data persistence. If you want to explore further, you can refer to the official documentation. I hope you have gained a good understanding. This article covered two types of volumes. With just Docker installed, the possibilities are endless! □□ Conclusion Similarly, you can compile C++ source code without installing g++, develop Java programs without installing JDK, and even use MongoDB without installing it directly on your machine. With volumes, you can avoid installing npm but still run npm init. In the above example, package.json is actually generated inside the container. Then, run npm init inside the container.Īfterward, check if package.json has been created on your machine.ĭocker run -v ~/app:/app -workdir /app node:slim npm init -y Let's see a demo:Ĭheck that there is no package.json inside ~/app on local machine.Ĭonnect the ~/app folder on your machine with the /app folder inside the container. Now, let's introduce another approach called Host Volume, which directly connects a specific folder on your machine with a folder inside the container. The previous approach, where you create a volume before using it, is called a Named Volume. This proves that even when a container is stopped, the data is still preserved in the volume, and the next container can successfully access the data left by the previous container. It's worth noting that these three commands are executed in different containers. Then, create a file named "file" inside the container.ĭocker run -v db-data:/db/data -it ubuntu ls -la /db/dataĭocker run -v db-data:/db/data -it ubuntu touch /db/data/file Initially, confirm that there are no files in /db/data. Alternatively, you can substitute it with the path where your database stores data. In this example, we are assigning the db-data volume to the /db/data location within the container. Docker has now added a folder on your machine for the volume. After completion, you will see an additional volume. We first create a volume called "db-data". How to use named volumn Step 1 - Creating a named volume This allows us to preserve the container's data. Importantly, even if the container is deleted, the data remains intact in the volume. As they are connected, any changes made to the folder within the container will reflect in the local folder. When you use a volume, Docker creates a local storage area on your machine, usually under /var, and establishes a connection between this folder and a specific folder inside the container. Let's take a look at the following diagram provided. To address this issue, we can utilize volumes to ensure data persistence.īasically, Volume is utilized to store data within containers. This means that if we need to upgrade the version of MySQL and decide to shut down the running mysql:5.5 container to start a new mysql:5.7 container, the data stored in the database would be lost. If you haven't read the previous article, " Pushing Your Docker Images to Docker Hub: A Step-by-Step Guide", I recommend taking a look at it first.Īs I mentioned before, each container operates in isolation. This is the third article in the Docker Series. You can find the Chinese version at Docker 實戰:使用 Volume 保存容器內的數據.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |