提交 4de5721e 编写于 作者: J Jeff Fox

Persisting our DB

上级 6d63d026
......@@ -53,3 +53,89 @@ docker tag getting-started YOUR-USER-NAME/getting-started
```bash
docker push YOUR-USER-NAME/getting-started
```
### Persisting our Todo Data
- Create a volume by using the docker volume create command.
```bash
docker volume create todo-db
```
- Start the todo app container, but add the -v flag to specify a volume mount. We will use the named volume and mount it to /etc/todos, which will capture all files created at the path.
```bash
docker run -dp 3000:3000 -v todo-db:/etc/todos getting-started
```
### Diving into our Volume
A lot of people frequently ask "Where is Docker actually storing my data when I use a named volume?" If you want to know, you can use the docker volume inspect command.
```js
docker volume inspect todo-db
[
{
"CreatedAt": "2019-09-26T02:18:36Z",
"Driver": "local",
"Labels": {},
"Mountpoint": "/var/lib/docker/volumes/todo-db/_data",
"Name": "todo-db",
"Options": {},
"Scope": "local"
}
]
```
The Mountpoint is the actual location on the disk where the data is stored. Note that on most machines, you will need to have root access to access this directory from the host. But, that's where it is!
### Starting a Dev-Mode Container
To run our container to support a development workflow, we will do the following:
- Mount our source code into the container
- Install all dependencies, including the "dev" dependencies
- Start nodemon to watch for filesystem changes
So, let's do it!
1. Make sure you don't have any previous getting-started containers running.
2. Run the following command. We'll explain what's going on afterwards:
```bs
docker run -dp 3000:3000 \
-w /app -v "$(pwd):/app" \
node:12-alpine \
sh -c "yarn install && yarn run dev"
```
If you are using PowerShell then use this command.
```bs
docker run -dp 3000:3000 `
-w /app -v "$(pwd):/app" `
node:12-alpine `
sh -c "yarn install && yarn run dev"
```
- -dp 3000:3000 - same as before. Run in detached (background) mode and create a port mapping
- -w /app - sets the "working directory" or the current directory that the command will run from
- -v "$(pwd):/app" - bind mount the current directory from the host in the container into the /app directory
- node:12-alpine - the image to use. Note that this is the base image for our app from the Dockerfile
- sh -c "yarn install && yarn run dev" - the command. We're starting a shell using sh (alpine doesn't have bash) and running yarn install to install all dependencies and then running yarn run dev. If we look in the package.json, we'll see that the dev script is starting nodemon.
3. You can watch the logs using docker logs -f \<container-id>. You'll know you're ready to go when you see this...
```bs
docker logs -f <container-id>
$ nodemon src/index.js
[nodemon] 1.19.2
[nodemon] to restart at any time, enter `rs`
[nodemon] watching dir(s): *.*
[nodemon] starting `node src/index.js`
Using sqlite database at /etc/todos/todo.db
Listening on port 3000
```
When you're done watching the logs, exit out by hitting Ctrl+C.
......@@ -53,7 +53,7 @@ function TodoListCard() {
<React.Fragment>
<AddItemForm onNewItem={onNewItem} />
{items.length === 0 && (
<p className="text-center">No items yet! Add one above!</p>
<p className="text-center">You have no todo items yet! Add one above!</p>
)}
{items.map(item => (
<ItemDisplay
......@@ -106,7 +106,7 @@ function AddItemForm({ onNewItem }) {
disabled={!newItem.length}
className={submitting ? 'disabled' : ''}
>
{submitting ? 'Adding...' : 'Add Item'}
{submitting ? 'Adding...' : 'Add'}
</Button>
</InputGroup.Append>
</InputGroup>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册