mirror of https://github.com/docker/docs.git
docs freshness: get-started (#19574)
* docs freshness: get-started Signed-off-by: Craig Osterhout <craig.osterhout@docker.com>
This commit is contained in:
parent
f828147958
commit
686725111b
|
@ -16,63 +16,43 @@ changes won't be seen in another container, even if they're using the same image
|
|||
|
||||
### See this in practice
|
||||
|
||||
To see this in action, you're going to start two containers and create a file in each.
|
||||
What you'll see is that the files created in one container aren't available in another.
|
||||
To see this in action, you're going to start two containers. In one container, you'll create a file. In the other container, you'll verify the file exists.
|
||||
What you'll see is that the file created in one container isn't available in another.
|
||||
|
||||
> **Note**
|
||||
>
|
||||
> If you use Windows and want to use Git Bash to run Docker commands, see [Working with Git Bash](../desktop/troubleshoot/topics.md#working-with-git-bash) for syntax differences.
|
||||
|
||||
1. Start an `ubuntu` container that will create a file named `/data.txt` with a random number
|
||||
between 1 and 10000.
|
||||
1. Start an Alpine container and access its shell.
|
||||
|
||||
```console
|
||||
$ docker run -d ubuntu bash -c "shuf -i 1-10000 -n 1 -o /data.txt && tail -f /dev/null"
|
||||
$ docker run -ti --name=mytest alpine
|
||||
```
|
||||
|
||||
In case you're curious about the command, you're starting a bash shell and invoking two
|
||||
commands (why you have the `&&`). The first portion picks a single random number and writes
|
||||
it to `/data.txt`. The second command is simply watching a file to keep the container running.
|
||||
|
||||
2. Validate that you can see the output by accessing the terminal in the container. To do so, you can use the CLI or Docker Desktop's graphical interface.
|
||||
|
||||
{{< tabs group="ui" >}}
|
||||
{{< tab name="CLI" >}}
|
||||
|
||||
On the command line, use the `docker exec` command to access the container. You need to get the
|
||||
container's ID (use `docker ps` to get it). In your Mac or Linux terminal, or in Windows Command Prompt or PowerShell, get the content with the following command.
|
||||
|
||||
```console
|
||||
$ docker exec <container-id> cat /data.txt
|
||||
```
|
||||
|
||||
{{< /tab >}}
|
||||
{{< tab name="Docker Desktop" >}}
|
||||
|
||||
In Docker Desktop, go to **Containers**, hover over the container running the **ubuntu** image, and select the **Show container actions** menu. From the drop-down menu, select **Open in terminal**.
|
||||
|
||||
You will see a terminal that is running a shell in the Ubuntu container. Run the following command to see the content of the `/data.txt` file. Close this terminal afterwards again.
|
||||
|
||||
```console
|
||||
$ cat /data.txt
|
||||
```
|
||||
|
||||
{{< /tab >}}
|
||||
{{< /tabs >}}
|
||||
|
||||
You should see a random number.
|
||||
|
||||
3. Now, start another `ubuntu` container (the same image) and you'll see you don't have the same file. In your Mac or Linux terminal, or in Windows Command Prompt or PowerShell, get the content with the following command.
|
||||
2. In the container, create a `greeting.txt` file with `hello` inside.
|
||||
|
||||
```console
|
||||
$ docker run -it ubuntu ls /
|
||||
/ # echo "hello" > greeting.txt
|
||||
```
|
||||
|
||||
In this case the command lists the files in the root directory of the container.
|
||||
Look, there's no `data.txt` file there! That's because it was written to the scratch space for
|
||||
only the first container.
|
||||
3. Exit the container.
|
||||
|
||||
```console
|
||||
/ # exit
|
||||
```
|
||||
|
||||
4. Run a new Alpine container and use the `cat` command to verify that the
|
||||
file does not exist.
|
||||
|
||||
```console
|
||||
$ docker run alpine cat greetings.txt
|
||||
```
|
||||
|
||||
You should see output similiar to the following that indicates the file does not exist in the new container.
|
||||
|
||||
```console
|
||||
cat: can't open 'greetings.txt': No such file or directory
|
||||
```
|
||||
|
||||
5. Go ahead and remove the containers using `docker ps --all` to get the IDs,
|
||||
and then `docker rm -f <container-id>` to remove the containers.
|
||||
|
||||
4. Go ahead and remove the first container using the `docker rm -f <container-id>` command.
|
||||
|
||||
## Container volumes
|
||||
|
||||
|
@ -106,7 +86,7 @@ name of the volume.
|
|||
|
||||
You can create the volume and start the container using the CLI or Docker Desktop's graphical interface.
|
||||
|
||||
{{< tabs group="ui" >}}
|
||||
{{< tabs >}}
|
||||
{{< tab name="CLI" >}}
|
||||
|
||||
1. Create a volume by using the `docker volume create` command.
|
||||
|
@ -115,15 +95,29 @@ You can create the volume and start the container using the CLI or Docker Deskto
|
|||
$ docker volume create todo-db
|
||||
```
|
||||
|
||||
2. Stop and remove the todo app container once again with `docker rm -f <id>`, as it is still running without using the persistent volume.
|
||||
2. Stop and remove the todo app container once again with `docker rm -f <id>`,
|
||||
as it is still running without using the persistent volume.
|
||||
|
||||
3. Start the todo app container, but add the `--mount` option to specify a volume mount. Give the volume a name, and mount
|
||||
it to `/etc/todos` in the container, which captures all files created at the path. In your Mac or Linux terminal, or in Windows Command Prompt or PowerShell, run the following command:
|
||||
3. Start the todo app container, but add the `--mount` option to specify a
|
||||
volume mount. Give the volume a name, and mount it to `/etc/todos` in the
|
||||
container, which captures all files created at the path.
|
||||
|
||||
```console
|
||||
$ docker run -dp 127.0.0.1:3000:3000 --mount type=volume,src=todo-db,target=/etc/todos getting-started
|
||||
```
|
||||
|
||||
> **Note**
|
||||
>
|
||||
> If you're using Git Bash, you must use different syntax for this command.
|
||||
>
|
||||
> ```console
|
||||
> $ docker run -dp 127.0.0.1:3000:3000 --mount type=volume,src=todo-db, target=//etc/todos getting-started
|
||||
> ```
|
||||
>
|
||||
> For more details about Git Bash's syntax differences, see
|
||||
> [Working with Git Bash](../desktop/troubleshoot/topics/#working-with-git-bash).
|
||||
|
||||
|
||||
{{< /tab >}}
|
||||
{{< tab name="Docker Desktop" >}}
|
||||
|
||||
|
@ -212,4 +206,4 @@ Related information:
|
|||
|
||||
Next, you'll learn how you can develop your app more efficiently using bind mounts.
|
||||
|
||||
{{< button text="Use bind mounts" url="06_bind_mounts.md" >}}
|
||||
{{< button text="Use bind mounts" url="06_bind_mounts.md" >}}
|
|
@ -1,9 +1,6 @@
|
|||
---
|
||||
title: Use bind mounts
|
||||
keywords: 'get started, setup, orientation, quickstart, intro, concepts, containers,
|
||||
docker desktop
|
||||
|
||||
'
|
||||
keywords: 'get started, setup, orientation, quickstart, intro, concepts, containers, docker desktop'
|
||||
description: Using bind mounts in our application
|
||||
---
|
||||
|
||||
|
@ -25,13 +22,17 @@ frameworks.
|
|||
|
||||
## Quick volume type comparisons
|
||||
|
||||
The following are examples of a named volume and a bind mount using `--mount`:
|
||||
|
||||
- Named volume: `type=volume,src=my-volume,target=/usr/local/data`
|
||||
- Bind mount: `type=bind,src=/path/to/data,target=/usr/local/data`
|
||||
|
||||
The following table outlines the main differences between volume mounts and bind
|
||||
mounts.
|
||||
|
||||
| | Named volumes | Bind mounts |
|
||||
| -------------------------------------------- | -------------------------------------------------- | ---------------------------------------------------- |
|
||||
| Host location | Docker chooses | You decide |
|
||||
| Mount example (using `--mount`) | `type=volume,src=my-volume,target=/usr/local/data` | `type=bind,src=/path/to/data,target=/usr/local/data` |
|
||||
| Populates new volume with container contents | Yes | No |
|
||||
| Supports Volume Drivers | Yes | No |
|
||||
|
||||
|
@ -41,10 +42,6 @@ Before looking at how you can use bind mounts for developing your application,
|
|||
you can run a quick experiment to get a practical understanding of how bind mounts
|
||||
work.
|
||||
|
||||
> **Note**
|
||||
>
|
||||
> If you use Windows and want to use Git Bash to run Docker commands, see [Working with Git Bash](../desktop/troubleshoot/topics.md#working-with-git-bash) for syntax differences.
|
||||
|
||||
1. Verify that your `getting-started-app` directory is in a directory defined in
|
||||
Docker Desktop's file sharing setting. This setting defines which parts of your
|
||||
filesystem you can share with containers. For details about accessing the
|
||||
|
@ -59,28 +56,26 @@ setting, see the topic for [Mac](../desktop/settings/mac.md/#file-sharing),
|
|||
bind mount.
|
||||
|
||||
{{< tabs >}}
|
||||
{{< tab name="Mac / Linux" >}}
|
||||
{{< tab name="Mac / Linux / PowerShell" >}}
|
||||
|
||||
```console
|
||||
$ docker run -it --mount type=bind,src="$(pwd)",target=/src ubuntu bash
|
||||
```
|
||||
|
||||
{{< /tab >}}
|
||||
{{< tab name="Windows (PowerShell)" >}}
|
||||
|
||||
|
||||
```powershell
|
||||
$ docker run -it --mount "type=bind,src=$pwd,target=/src" ubuntu bash
|
||||
```
|
||||
|
||||
{{< /tab >}}
|
||||
{{< tab name="Windows (Command Prompt)" >}}
|
||||
|
||||
{{< tab name="Command Prompt" >}}
|
||||
|
||||
```console
|
||||
$ docker run -it --mount "type=bind,src=%cd%,target=/src" ubuntu bash
|
||||
```
|
||||
|
||||
{{< /tab >}}
|
||||
{{< tab name="Git Bash" >}}
|
||||
|
||||
```console
|
||||
$ docker run -it --mount type=bind,src="/$(pwd)",target=/src ubuntu bash
|
||||
```
|
||||
|
||||
{{< /tab >}}
|
||||
{{< /tabs >}}
|
||||
|
||||
|
@ -164,7 +159,7 @@ mount that does the following:
|
|||
You can use the CLI or Docker Desktop to run your container with a bind mount.
|
||||
|
||||
{{< tabs >}}
|
||||
{{< tab name="CLI (Mac / Linux)" >}}
|
||||
{{< tab name="Mac / Linux CLI" >}}
|
||||
|
||||
1. Make sure you don't have any `getting-started` containers currently running.
|
||||
|
||||
|
@ -210,15 +205,12 @@ You can use the CLI or Docker Desktop to run your container with a bind mount.
|
|||
When you're done watching the logs, exit out by hitting `Ctrl`+`C`.
|
||||
|
||||
{{< /tab >}}
|
||||
{{< tab name="CLI (Windows)" >}}
|
||||
{{< tab name="PowerShell CLI" >}}
|
||||
|
||||
1. Make sure you don't have any `getting-started` containers currently running.
|
||||
|
||||
2. Run the following command from the `getting-started-app` directory.
|
||||
|
||||
Run this command in PowerShell.
|
||||
|
||||
|
||||
```powershell
|
||||
$ docker run -dp 127.0.0.1:3000:3000 `
|
||||
-w /app --mount "type=bind,src=$pwd,target=/app" `
|
||||
|
@ -258,6 +250,98 @@ You can use the CLI or Docker Desktop to run your container with a bind mount.
|
|||
|
||||
When you're done watching the logs, exit out by hitting `Ctrl`+`C`.
|
||||
|
||||
{{< /tab >}}
|
||||
{{< tab name="Command Prompt CLI" >}}
|
||||
|
||||
1. Make sure you don't have any `getting-started` containers currently running.
|
||||
|
||||
2. Run the following command from the `getting-started-app` directory.
|
||||
|
||||
```console
|
||||
$ docker run -dp 127.0.0.1:3000:3000 ^
|
||||
-w /app --mount "type=bind,src=%cd%,target=/app" ^
|
||||
node:18-alpine ^
|
||||
sh -c "yarn install && yarn run dev"
|
||||
```
|
||||
|
||||
The following is a breakdown of the command:
|
||||
- `-dp 127.0.0.1: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
|
||||
- `--mount "type=bind,src=%cd%,target=/app"` - bind mount the current
|
||||
directory from the host into the `/app` directory in the container
|
||||
- `node:18-alpine` - the image to use. Note that this is the base image for
|
||||
your app from the Dockerfile
|
||||
- `sh -c "yarn install && yarn run dev"` - the command. You're starting a
|
||||
shell using `sh` (alpine doesn't have `bash`) and running `yarn install` to
|
||||
install packages and then running `yarn run dev` to start the development
|
||||
server. If you look in the `package.json`, you'll see that the `dev` script
|
||||
starts `nodemon`.
|
||||
|
||||
3. You can watch the logs using `docker logs <container-id>`. You'll know you're
|
||||
ready to go when you see this:
|
||||
|
||||
```console
|
||||
$ docker logs -f <container-id>
|
||||
nodemon -L src/index.js
|
||||
[nodemon] 2.0.20
|
||||
[nodemon] to restart at any time, enter `rs`
|
||||
[nodemon] watching path(s): *.*
|
||||
[nodemon] watching extensions: js,mjs,json
|
||||
[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`.
|
||||
|
||||
{{< /tab >}}
|
||||
{{< tab name="Git Bash CLI" >}}
|
||||
|
||||
1. Make sure you don't have any `getting-started` containers currently running.
|
||||
|
||||
2. Run the following command from the `getting-started-app` directory.
|
||||
|
||||
```console
|
||||
$ docker run -dp 127.0.0.1:3000:3000 \
|
||||
-w //app --mount type=bind,src="/$(pwd)",target=/app \
|
||||
node:18-alpine \
|
||||
sh -c "yarn install && yarn run dev"
|
||||
```
|
||||
|
||||
The following is a breakdown of the command:
|
||||
- `-dp 127.0.0.1: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
|
||||
- `--mount type=bind,src="/$(pwd)",target=/app` - bind mount the current
|
||||
directory from the host into the `/app` directory in the container
|
||||
- `node:18-alpine` - the image to use. Note that this is the base image for
|
||||
your app from the Dockerfile
|
||||
- `sh -c "yarn install && yarn run dev"` - the command. You're starting a
|
||||
shell using `sh` (alpine doesn't have `bash`) and running `yarn install` to
|
||||
install packages and then running `yarn run dev` to start the development
|
||||
server. If you look in the `package.json`, you'll see that the `dev` script
|
||||
starts `nodemon`.
|
||||
|
||||
3. You can watch the logs using `docker logs <container-id>`. You'll know you're
|
||||
ready to go when you see this:
|
||||
|
||||
```console
|
||||
$ docker logs -f <container-id>
|
||||
nodemon -L src/index.js
|
||||
[nodemon] 2.0.20
|
||||
[nodemon] to restart at any time, enter `rs`
|
||||
[nodemon] watching path(s): *.*
|
||||
[nodemon] watching extensions: js,mjs,json
|
||||
[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`.
|
||||
|
||||
{{< /tab >}}
|
||||
{{< tab name="Docker Desktop" >}}
|
||||
|
||||
|
|
|
@ -44,7 +44,7 @@ In the following steps, you'll create the network first and then attach the MySQ
|
|||
database will use to initialize the database. To learn more about the MySQL environment variables, see the "Environment Variables" section in the [MySQL Docker Hub listing](https://hub.docker.com/_/mysql/).
|
||||
|
||||
{{< tabs >}}
|
||||
{{< tab name="Mac / Linux" >}}
|
||||
{{< tab name="Mac / Linux / Git Bash" >}}
|
||||
|
||||
```console
|
||||
$ docker run -d \
|
||||
|
@ -56,7 +56,7 @@ In the following steps, you'll create the network first and then attach the MySQ
|
|||
```
|
||||
|
||||
{{< /tab >}}
|
||||
{{< tab name="Windows (PowerShell)" >}}
|
||||
{{< tab name="PowerShell" >}}
|
||||
|
||||
```powershell
|
||||
$ docker run -d `
|
||||
|
@ -68,7 +68,7 @@ In the following steps, you'll create the network first and then attach the MySQ
|
|||
```
|
||||
|
||||
{{< /tab >}}
|
||||
{{< tab name="Windows (Command Prompt)" >}}
|
||||
{{< tab name="Command Prompt" >}}
|
||||
|
||||
```console
|
||||
$ docker run -d ^
|
||||
|
@ -219,7 +219,7 @@ You can now start your dev-ready container.
|
|||
```
|
||||
|
||||
{{< /tab >}}
|
||||
{{< tab name="Windows (PowerShell)" >}}
|
||||
{{< tab name="PowerShell" >}}
|
||||
In Windows, run this command in PowerShell.
|
||||
|
||||
```powershell
|
||||
|
@ -235,7 +235,7 @@ You can now start your dev-ready container.
|
|||
```
|
||||
|
||||
{{< /tab >}}
|
||||
{{< tab name="Windows (Command Prompt)" >}}
|
||||
{{< tab name="Command Prompt" >}}
|
||||
In Windows, run this command in Command Prompt.
|
||||
|
||||
```console
|
||||
|
@ -250,6 +250,21 @@ You can now start your dev-ready container.
|
|||
sh -c "yarn install && yarn run dev"
|
||||
```
|
||||
|
||||
{{< /tab >}}
|
||||
{{< tab name="Git Bash" >}}
|
||||
|
||||
```console
|
||||
$ docker run -dp 127.0.0.1:3000:3000 \
|
||||
-w //app -v "/$(pwd):/app" \
|
||||
--network todo-app \
|
||||
-e MYSQL_HOST=mysql \
|
||||
-e MYSQL_USER=root \
|
||||
-e MYSQL_PASSWORD=secret \
|
||||
-e MYSQL_DB=todos \
|
||||
node:18-alpine \
|
||||
sh -c "yarn install && yarn run dev"
|
||||
```
|
||||
|
||||
{{< /tab >}}
|
||||
{{< /tabs >}}
|
||||
|
||||
|
|
Loading…
Reference in New Issue