mirror of https://github.com/docker/docs.git
Update Compose quick-start to show static loaded image, then volume mount (#4255)
* re-org sample to show static loaded image, then using a volume mounte Signed-off-by: Victoria Bialas <victoria.bialas@docker.com> * review comments from mstanleyjones and joaofnfernandes Signed-off-by: Victoria Bialas <victoria.bialas@docker.com> * added a sub-step to make the workflow and output make sense Signed-off-by: Victoria Bialas <victoria.bialas@docker.com> * combined notes about shared drives and volumes, reorganized topic order Signed-off-by: Victoria Bialas <victoria.bialas@docker.com>
This commit is contained in:
parent
bf61cd2f16
commit
0f802e1b76
|
@ -88,8 +88,6 @@ the following:
|
|||
build: .
|
||||
ports:
|
||||
- "5000:5000"
|
||||
volumes:
|
||||
- .:/code
|
||||
redis:
|
||||
image: "redis:alpine"
|
||||
|
||||
|
@ -98,83 +96,164 @@ This Compose file defines two services, `web` and `redis`. The web service:
|
|||
* Uses an image that's built from the `Dockerfile` in the current directory.
|
||||
* Forwards the exposed port 5000 on the container to port 5000 on the host
|
||||
machine. We use the default port for the Flask web server, `5000`.
|
||||
* Mounts the project directory on the host to `/code` inside the container,
|
||||
allowing you to modify the code without having to rebuild the image.
|
||||
|
||||
The `redis` service uses a public
|
||||
[Redis](https://registry.hub.docker.com/_/redis/) image pulled from the Docker
|
||||
Hub registry.
|
||||
|
||||
>**Tip**: If your project is outside of the `Users` directory (`cd ~`), then you
|
||||
## Step 4: Build and run your app with Compose
|
||||
|
||||
1. From your project directory, start up your application by running `docker-compose up`.
|
||||
|
||||
```
|
||||
$ docker-compose up
|
||||
Creating network "composetest_default" with the default driver
|
||||
Creating composetest_web_1 ...
|
||||
Creating composetest_redis_1 ...
|
||||
Creating composetest_web_1
|
||||
Creating composetest_redis_1 ... done
|
||||
Attaching to composetest_web_1, composetest_redis_1
|
||||
web_1 | * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
|
||||
redis_1 | 1:C 17 Aug 22:11:10.480 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
|
||||
redis_1 | 1:C 17 Aug 22:11:10.480 # Redis version=4.0.1, bits=64, commit=00000000, modified=0, pid=1, just started
|
||||
redis_1 | 1:C 17 Aug 22:11:10.480 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
|
||||
web_1 | * Restarting with stat
|
||||
redis_1 | 1:M 17 Aug 22:11:10.483 * Running mode=standalone, port=6379.
|
||||
redis_1 | 1:M 17 Aug 22:11:10.483 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
|
||||
web_1 | * Debugger is active!
|
||||
redis_1 | 1:M 17 Aug 22:11:10.483 # Server initialized
|
||||
redis_1 | 1:M 17 Aug 22:11:10.483 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
|
||||
web_1 | * Debugger PIN: 330-787-903
|
||||
redis_1 | 1:M 17 Aug 22:11:10.483 * Ready to accept connections
|
||||
```
|
||||
|
||||
Compose pulls a Redis image, builds an image for your code, and start the
|
||||
services you defined. In this case, the code is statically copied into the image at build time.
|
||||
|
||||
2. Enter `http://0.0.0.0:5000/` in a browser to see the application running.
|
||||
|
||||
If you're using Docker natively on Linux, Docker for Mac, or Docker
|
||||
for Windows, then the web app should now be listening on port 5000
|
||||
on your Docker daemon host. Point your web browser to `http://localhost:5000` to find the `Hello World` message. If this
|
||||
doesn't resolve, you can also try `http://0.0.0.0:5000`.
|
||||
|
||||
If you're using Docker Machine on a Mac, use `docker-machine ip
|
||||
MACHINE_VM` to get the IP address of your Docker host. Then, open
|
||||
`http://MACHINE_VM_IP:5000` in a browser.
|
||||
|
||||
You should see a message in your browser saying:
|
||||
|
||||
```
|
||||
Hello World! I have been seen 1 times.
|
||||
```
|
||||
|
||||

|
||||
|
||||
3. Refresh the page.
|
||||
|
||||
The number should increment.
|
||||
|
||||
```
|
||||
Hello World! I have been seen 2 times.
|
||||
```
|
||||
|
||||

|
||||
|
||||
4. Switch to another terminal window, and type `docker image ls` to
|
||||
list local images.
|
||||
|
||||
Listing images at this point should return `redis` and `web`.
|
||||
|
||||
```
|
||||
$ docker image ls
|
||||
REPOSITORY TAG IMAGE ID CREATED SIZE
|
||||
composetest_web latest e2c21aa48cc1 4 minutes ago 93.8MB
|
||||
python 3.4-alpine 84e6077c7ab6 7 days ago 82.5MB
|
||||
redis alpine 9d8fa9aa0e5b 3 weeks ago 27.5MB
|
||||
```
|
||||
|
||||
You can inspect images with `docker inspect <tag or id>`.
|
||||
|
||||
5. Stop the application, either by running `docker-compose down`
|
||||
from within your project directory in the second terminal, or by
|
||||
hitting CTRL+C in the original terminal where you started the app.
|
||||
|
||||
## Step 5: Edit the Compose file to add a bind mount
|
||||
|
||||
Edit `docker-compose.yml` in your project directory to add a [bind mount](/engine/admin/volumes/bind-mounts.md) for the `web` service:
|
||||
|
||||
version: '3'
|
||||
services:
|
||||
web:
|
||||
build: .
|
||||
ports:
|
||||
- "5000:5000"
|
||||
volumes:
|
||||
- .:/code
|
||||
redis:
|
||||
image: "redis:alpine"
|
||||
|
||||
The new `volumes` key mounts the project directory (current directory) on the
|
||||
host to `/code` inside the container, allowing you to modify the code on the
|
||||
fly, without having to rebuild the image.
|
||||
|
||||
## Step 6: Re-build and run the app with Compose
|
||||
|
||||
From your project directory, type `docker-compose up` to build the app with the updated Compose file, and run it.
|
||||
|
||||
```
|
||||
$ docker-compose up
|
||||
Creating network "composetest_default" with the default driver
|
||||
Creating composetest_web_1 ...
|
||||
Creating composetest_redis_1 ...
|
||||
Creating composetest_web_1
|
||||
Creating composetest_redis_1 ... done
|
||||
Attaching to composetest_web_1, composetest_redis_1
|
||||
web_1 | * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)
|
||||
...
|
||||
```
|
||||
|
||||
Check the `Hello World` message in a web browser again, and refresh to see the
|
||||
count increment.
|
||||
|
||||
> Shared folders, volumes, and bind mounts
|
||||
>
|
||||
> * If your project is outside of the `Users` directory (`cd ~`), then you
|
||||
need to share the drive or location of the Dockerfile and volume you are using.
|
||||
If you get runtime errors indicating an application file is not found, a volume
|
||||
mount is denied, or a service cannot start, try enabling file or drive sharing.
|
||||
Volume mounting requires shared drives for projects that live outside of
|
||||
`C:\Users` (Windows) or `/Users` (Mac), and is required for _any_ project on
|
||||
Docker for Windows that uses [Linux
|
||||
containers](/docker-for-windows/#switch-between-windows-and-linux-containers-beta-feature).
|
||||
For more information, see [Shared
|
||||
Drives](../docker-for-windows/#shared-drives) on Docker for Windows,
|
||||
[File sharing](../docker-for-mac/#file-sharing) on Docker for Mac, and
|
||||
the general examples on how to [Manage data in
|
||||
containers](/docker-for-windows/#switch-between-windows-and-linux-containers-beta-feature). For more information, see [Shared Drives](../docker-for-windows/#shared-drives)
|
||||
on Docker for Windows, [File sharing](../docker-for-mac/#file-sharing) on Docker
|
||||
for Mac, and the general examples on how to [Manage data in
|
||||
containers](../engine/tutorials/dockervolumes.md).
|
||||
>
|
||||
> * If you are using Oracle VirtualBox on an older Windows OS, you might encounter an issue with shared folders as described in this [VB trouble
|
||||
ticket](https://www.virtualbox.org/ticket/14920). Newer Windows systems meet the
|
||||
requirements for [Docker for Windows](/docker-for-windows/install.md) and do not
|
||||
need VirtualBox.
|
||||
{: .important}
|
||||
|
||||
## Step 4: Build and run your app with Compose
|
||||
## Step 7: Update the application
|
||||
|
||||
1. From your project directory, start up your application.
|
||||
Because the application code is now mounted into the container using a volume,
|
||||
you can make changes to its code and see the changes instantly, without having
|
||||
to rebuild the image.
|
||||
|
||||
$ docker-compose up
|
||||
Pulling image redis...
|
||||
Building web...
|
||||
Starting composetest_redis_1...
|
||||
Starting composetest_web_1...
|
||||
redis_1 | [8] 02 Jan 18:43:35.576 # Server started, Redis version 2.8.3
|
||||
web_1 | * Running on http://0.0.0.0:5000/
|
||||
web_1 | * Restarting with stat
|
||||
1. Change the greeting in `app.py` and save it. For example, change the `Hello World!` message to `Hello from Docker!`:
|
||||
|
||||
Compose pulls a Redis image, builds an image for your code, and start the
|
||||
services you defined.
|
||||
|
||||
2. Enter `http://0.0.0.0:5000/` in a browser to see the application running.
|
||||
|
||||
If you're using Docker on Linux natively, then the web app should now be
|
||||
listening on port 5000 on your Docker daemon host. If `http://0.0.0.0:5000`
|
||||
doesn't resolve, you can also try `http://localhost:5000`.
|
||||
|
||||
If you're using Docker Machine on a Mac, use `docker-machine ip MACHINE_VM` to get
|
||||
the IP address of your Docker host. Then, open `http://MACHINE_VM_IP:5000` in a
|
||||
browser.
|
||||
|
||||
You should see a message in your browser saying:
|
||||
|
||||
`Hello World! I have been seen 1 times.`
|
||||
|
||||
3. Refresh the page.
|
||||
|
||||
The number should increment.
|
||||
|
||||
>**Tip**: You can list local images with `docker image ls` and inspect them with `docker inspect <tag or id>`. Listing images at this point should return `redis` and `web`.
|
||||
|
||||
|
||||
## Step 5: Update the application
|
||||
|
||||
Because the application code is mounted into the container using a volume, you
|
||||
can make changes to its code and see the changes instantly, without having to
|
||||
rebuild the image.
|
||||
|
||||
1. Change the greeting in `app.py` and save it. For example:
|
||||
|
||||
return 'Hello from Docker! I have been seen {} times.\n'.format(count)
|
||||
```
|
||||
return 'Hello from Docker! I have been seen {} times.\n'.format(count)
|
||||
```
|
||||
|
||||
2. Refresh the app in your browser. The greeting should be updated, and the
|
||||
counter should still be incrementing.
|
||||
|
||||
>**Note:** If you are using Oracle VirtualBox on an older Windows OS, you might encounter an issue with shared folders as described in this [VB trouble
|
||||
ticket](https://www.virtualbox.org/ticket/14920). Newer Windows systems meet the
|
||||
requirements for [Docker for Windows](/docker-for-windows/install.md) and do not
|
||||
need VirtualBox.
|
||||

|
||||
|
||||
## Step 6: Experiment with some other commands
|
||||
## Step 8: Experiment with some other commands
|
||||
|
||||
If you want to run your services in the background, you can pass the `-d` flag
|
||||
(for "detached" mode) to `docker-compose up` and use `docker-compose ps` to
|
||||
|
@ -215,6 +294,7 @@ At this point, you have seen the basics of how Compose works.
|
|||
## Where to go next
|
||||
|
||||
- Next, try the quick start guide for [Django](django.md),
|
||||
[Rails](rails.md), or [WordPress](/samples/library/wordpress/).
|
||||
[Rails](rails.md), or [WordPress](/samples/library/wordpress/)
|
||||
- [Explore the full list of Compose commands](./reference/)
|
||||
- [Compose configuration file reference](compose-file/)
|
||||
- To learn more about volumes and bind mounts, see [Manage data in Docker](/engine/admin/volumes/index.md)
|
||||
|
|
Binary file not shown.
After Width: | Height: | Size: 29 KiB |
Binary file not shown.
After Width: | Height: | Size: 37 KiB |
Binary file not shown.
After Width: | Height: | Size: 39 KiB |
Loading…
Reference in New Issue