mirror of https://github.com/docker/docs.git
remove examples for ENV key value without '='
The `ENV key value` form can be ambiguous, for example, the following defines
a single env-variable (`ONE`) with value `"TWO= THREE=world"`:
ENV ONE TWO= THREE=world
While we cannot deprecate/remove that syntax (as it would break existing
Dockerfiles), we should reduce exposure of the format in our examples.
Also updating some code-blocks that were missing language-hints
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
parent
463d98bd41
commit
f6adc80d9a
|
|
@ -25,17 +25,19 @@ and a `docker-compose.yml` file. (You can use either a `.yml` or `.yaml` extensi
|
|||
|
||||
3. Add the following content to the `Dockerfile`.
|
||||
|
||||
FROM python:3
|
||||
ENV PYTHONUNBUFFERED 1
|
||||
RUN mkdir /code
|
||||
WORKDIR /code
|
||||
COPY requirements.txt /code/
|
||||
RUN pip install -r requirements.txt
|
||||
COPY . /code/
|
||||
```dockerfile
|
||||
FROM python:3
|
||||
ENV PYTHONUNBUFFERED=1
|
||||
RUN mkdir /code
|
||||
WORKDIR /code
|
||||
COPY requirements.txt /code/
|
||||
RUN pip install -r requirements.txt
|
||||
COPY . /code/
|
||||
```
|
||||
|
||||
This `Dockerfile` starts with a [Python 3 parent image](https://hub.docker.com/r/library/python/tags/3/).
|
||||
The parent image is modified by adding a new `code` directory. The parent image is further modified
|
||||
by installing the Python requirements defined in the `requirements.txt` file.
|
||||
This `Dockerfile` starts with a [Python 3 parent image](https://hub.docker.com/r/library/python/tags/3/).
|
||||
The parent image is modified by adding a new `code` directory. The parent image is further modified
|
||||
by installing the Python requirements defined in the `requirements.txt` file.
|
||||
|
||||
4. Save and close the `Dockerfile`.
|
||||
|
||||
|
|
@ -62,34 +64,34 @@ and a `docker-compose.yml` file. (You can use either a `.yml` or `.yaml` extensi
|
|||
|
||||
9. Add the following configuration to the file.
|
||||
|
||||
```none
|
||||
version: '3'
|
||||
```yaml
|
||||
version: "{{ site.compose_file_v3 }}"
|
||||
|
||||
services:
|
||||
db:
|
||||
image: postgres
|
||||
environment:
|
||||
- POSTGRES_DB=postgres
|
||||
- POSTGRES_USER=postgres
|
||||
- POSTGRES_PASSWORD=postgres
|
||||
web:
|
||||
build: .
|
||||
command: python manage.py runserver 0.0.0.0:8000
|
||||
volumes:
|
||||
- .:/code
|
||||
ports:
|
||||
- "8000:8000"
|
||||
depends_on:
|
||||
- db
|
||||
```
|
||||
services:
|
||||
db:
|
||||
image: postgres
|
||||
environment:
|
||||
- POSTGRES_DB=postgres
|
||||
- POSTGRES_USER=postgres
|
||||
- POSTGRES_PASSWORD=postgres
|
||||
web:
|
||||
build: .
|
||||
command: python manage.py runserver 0.0.0.0:8000
|
||||
volumes:
|
||||
- .:/code
|
||||
ports:
|
||||
- "8000:8000"
|
||||
depends_on:
|
||||
- db
|
||||
```
|
||||
|
||||
This file defines two services: The `db` service and the `web` service.
|
||||
This file defines two services: The `db` service and the `web` service.
|
||||
|
||||
> Note:
|
||||
>
|
||||
> This uses the build in development server to run your application
|
||||
> on port 8000. Do not use this in a production environment. For more
|
||||
> information, see [Django documentation](https://docs.djangoproject.com/en/3.1/intro/tutorial01/#the-development-server){: target="_blank" class="_”}.
|
||||
> Note:
|
||||
>
|
||||
> This uses the build in development server to run your application
|
||||
> on port 8000. Do not use this in a production environment. For more
|
||||
> information, see [Django documentation](https://docs.djangoproject.com/en/3.1/intro/tutorial01/#the-development-server){: target="_blank" class="_”}.
|
||||
|
||||
10. Save and close the `docker-compose.yml` file.
|
||||
|
||||
|
|
@ -102,7 +104,9 @@ In this step, you create a Django starter project by building the image from the
|
|||
2. Create the Django project by running
|
||||
the [docker-compose run](reference/run.md) command as follows.
|
||||
|
||||
sudo docker-compose run web django-admin startproject composeexample .
|
||||
```console
|
||||
$ sudo docker-compose run web django-admin startproject composeexample .
|
||||
```
|
||||
|
||||
This instructs Compose to run `django-admin startproject composeexample`
|
||||
in a container, using the `web` service's image and configuration. Because
|
||||
|
|
@ -116,120 +120,131 @@ the [docker-compose run](reference/run.md) command as follows.
|
|||
|
||||
3. After the `docker-compose` command completes, list the contents of your project.
|
||||
|
||||
$ ls -l
|
||||
drwxr-xr-x 2 root root composeexample
|
||||
-rw-rw-r-- 1 user user docker-compose.yml
|
||||
-rw-rw-r-- 1 user user Dockerfile
|
||||
-rwxr-xr-x 1 root root manage.py
|
||||
-rw-rw-r-- 1 user user requirements.txt
|
||||
```console
|
||||
$ ls -l
|
||||
|
||||
If you are running Docker on Linux, the files `django-admin` created are
|
||||
owned by root. This happens because the container runs as the root user.
|
||||
Change the ownership of the new files.
|
||||
drwxr-xr-x 2 root root composeexample
|
||||
-rw-rw-r-- 1 user user docker-compose.yml
|
||||
-rw-rw-r-- 1 user user Dockerfile
|
||||
-rwxr-xr-x 1 root root manage.py
|
||||
-rw-rw-r-- 1 user user requirements.txt
|
||||
```
|
||||
|
||||
sudo chown -R $USER:$USER .
|
||||
If you are running Docker on Linux, the files `django-admin` created are
|
||||
owned by root. This happens because the container runs as the root user.
|
||||
Change the ownership of the new files.
|
||||
|
||||
If you are running Docker on Mac or Windows, you should already
|
||||
have ownership of all files, including those generated by
|
||||
`django-admin`. List the files just to verify this.
|
||||
```console
|
||||
$ sudo chown -R $USER:$USER .
|
||||
```
|
||||
|
||||
$ ls -l
|
||||
total 32
|
||||
-rw-r--r-- 1 user staff 145 Feb 13 23:00 Dockerfile
|
||||
drwxr-xr-x 6 user staff 204 Feb 13 23:07 composeexample
|
||||
-rw-r--r-- 1 user staff 159 Feb 13 23:02 docker-compose.yml
|
||||
-rwxr-xr-x 1 user staff 257 Feb 13 23:07 manage.py
|
||||
-rw-r--r-- 1 user staff 16 Feb 13 23:01 requirements.txt
|
||||
If you are running Docker on Mac or Windows, you should already
|
||||
have ownership of all files, including those generated by
|
||||
`django-admin`. List the files just to verify this.
|
||||
|
||||
```console
|
||||
$ ls -l
|
||||
|
||||
total 32
|
||||
-rw-r--r-- 1 user staff 145 Feb 13 23:00 Dockerfile
|
||||
drwxr-xr-x 6 user staff 204 Feb 13 23:07 composeexample
|
||||
-rw-r--r-- 1 user staff 159 Feb 13 23:02 docker-compose.yml
|
||||
-rwxr-xr-x 1 user staff 257 Feb 13 23:07 manage.py
|
||||
-rw-r--r-- 1 user staff 16 Feb 13 23:01 requirements.txt
|
||||
```
|
||||
|
||||
|
||||
### Connect the database
|
||||
|
||||
In this section, you set up the database connection for Django.
|
||||
|
||||
1. In your project directory, edit the `composeexample/settings.py` file.
|
||||
1. In your project directory, edit the `composeexample/settings.py` file.
|
||||
|
||||
2. Replace the `DATABASES = ...` with the following:
|
||||
|
||||
# settings.py
|
||||
```python
|
||||
# settings.py
|
||||
|
||||
DATABASES = {
|
||||
'default': {
|
||||
'ENGINE': 'django.db.backends.postgresql',
|
||||
'NAME': 'postgres',
|
||||
'USER': 'postgres',
|
||||
'PASSWORD': 'postgres',
|
||||
'HOST': 'db',
|
||||
'PORT': 5432,
|
||||
}
|
||||
DATABASES = {
|
||||
'default': {
|
||||
'ENGINE': 'django.db.backends.postgresql',
|
||||
'NAME': 'postgres',
|
||||
'USER': 'postgres',
|
||||
'PASSWORD': 'postgres',
|
||||
'HOST': 'db',
|
||||
'PORT': 5432,
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
These settings are determined by the
|
||||
[postgres](https://hub.docker.com/_/postgres) Docker image
|
||||
specified in `docker-compose.yml`.
|
||||
|
||||
3. Save and close the file.
|
||||
3. Save and close the file.
|
||||
|
||||
4. Run the [docker-compose up](reference/up.md) command from the top level directory for your project.
|
||||
4. Run the [docker-compose up](reference/up.md) command from the top level directory for your project.
|
||||
|
||||
```none
|
||||
$ docker-compose up
|
||||
djangosample_db_1 is up-to-date
|
||||
Creating djangosample_web_1 ...
|
||||
Creating djangosample_web_1 ... done
|
||||
Attaching to djangosample_db_1, djangosample_web_1
|
||||
db_1 | The files belonging to this database system will be owned by user "postgres".
|
||||
db_1 | This user must also own the server process.
|
||||
db_1 |
|
||||
db_1 | The database cluster will be initialized with locale "en_US.utf8".
|
||||
db_1 | The default database encoding has accordingly been set to "UTF8".
|
||||
db_1 | The default text search configuration will be set to "english".
|
||||
```console
|
||||
$ docker-compose up
|
||||
|
||||
. . .
|
||||
djangosample_db_1 is up-to-date
|
||||
Creating djangosample_web_1 ...
|
||||
Creating djangosample_web_1 ... done
|
||||
Attaching to djangosample_db_1, djangosample_web_1
|
||||
db_1 | The files belonging to this database system will be owned by user "postgres".
|
||||
db_1 | This user must also own the server process.
|
||||
db_1 |
|
||||
db_1 | The database cluster will be initialized with locale "en_US.utf8".
|
||||
db_1 | The default database encoding has accordingly been set to "UTF8".
|
||||
db_1 | The default text search configuration will be set to "english".
|
||||
|
||||
web_1 | July 30, 2020 - 18:35:38
|
||||
web_1 | Django version 3.0.8, using settings 'composeexample.settings'
|
||||
web_1 | Starting development server at http://0.0.0.0:8000/
|
||||
web_1 | Quit the server with CONTROL-C.
|
||||
```
|
||||
. . .
|
||||
|
||||
At this point, your Django app should be running at port `8000` on
|
||||
your Docker host. On Docker Desktop for Mac and Docker Desktop for Windows, go
|
||||
to `http://localhost:8000` on a web browser to see the Django
|
||||
welcome page. If you are using [Docker Machine](../machine/overview.md),
|
||||
then `docker-machine ip MACHINE_VM` returns the Docker host IP
|
||||
address, to which you can append the port (`<Docker-Host-IP>:8000`).
|
||||
web_1 | July 30, 2020 - 18:35:38
|
||||
web_1 | Django version 3.0.8, using settings 'composeexample.settings'
|
||||
web_1 | Starting development server at http://0.0.0.0:8000/
|
||||
web_1 | Quit the server with CONTROL-C.
|
||||
```
|
||||
|
||||

|
||||
At this point, your Django app should be running at port `8000` on
|
||||
your Docker host. On Docker Desktop for Mac and Docker Desktop for Windows, go
|
||||
to `http://localhost:8000` on a web browser to see the Django
|
||||
welcome page. If you are using [Docker Machine](../machine/overview.md),
|
||||
then `docker-machine ip MACHINE_VM` returns the Docker host IP
|
||||
address, to which you can append the port (`<Docker-Host-IP>:8000`).
|
||||
|
||||
> Note:
|
||||
>
|
||||
> On certain platforms (Windows 10), you might need to
|
||||
edit `ALLOWED_HOSTS` inside `settings.py` and add your Docker host name
|
||||
or IP address to the list. For demo purposes, you can set the value to:
|
||||
>
|
||||
> ALLOWED_HOSTS = ['*']
|
||||
>
|
||||
> This value is **not** safe for production usage. Refer to the
|
||||
[Django documentation](https://docs.djangoproject.com/en/1.11/ref/settings/#allowed-hosts) for more information.
|
||||

|
||||
|
||||
5. List running containers.
|
||||
> Note:
|
||||
>
|
||||
> On certain platforms (Windows 10), you might need to edit `ALLOWED_HOSTS`
|
||||
> inside `settings.py` and add your Docker host name or IP address to the list.
|
||||
> For demo purposes, you can set the value to:
|
||||
>
|
||||
> ALLOWED_HOSTS = ['*']
|
||||
>
|
||||
> This value is **not** safe for production usage. Refer to the
|
||||
> [Django documentation](https://docs.djangoproject.com/en/1.11/ref/settings/#allowed-hosts) for more information.
|
||||
|
||||
In another terminal window, list the running Docker processes with the `docker container ls` command.
|
||||
5. List running containers.
|
||||
|
||||
```none
|
||||
$ docker ps
|
||||
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
|
||||
def85eff5f51 django_web "python3 manage.py..." 10 minutes ago Up 9 minutes 0.0.0.0:8000->8000/tcp django_web_1
|
||||
678ce61c79cc postgres "docker-entrypoint..." 20 minutes ago Up 9 minutes 5432/tcp django_db_1
|
||||
In another terminal window, list the running Docker processes with the `docker container ls` command.
|
||||
|
||||
```
|
||||
```console
|
||||
$ docker ps
|
||||
|
||||
6. Shut down services and clean up by using either of these methods:
|
||||
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
|
||||
def85eff5f51 django_web "python3 manage.py..." 10 minutes ago Up 9 minutes 0.0.0.0:8000->8000/tcp django_web_1
|
||||
678ce61c79cc postgres "docker-entrypoint..." 20 minutes ago Up 9 minutes 5432/tcp django_db_1
|
||||
```
|
||||
|
||||
6. Shut down services and clean up by using either of these methods:
|
||||
|
||||
* Stop the application by typing `Ctrl-C`
|
||||
in the same shell in where you started it:
|
||||
|
||||
```none
|
||||
```console
|
||||
Gracefully stopping... (press Ctrl+C again to force)
|
||||
Killing test_web_1 ... done
|
||||
Killing test_db_1 ... done
|
||||
|
|
@ -237,9 +252,10 @@ def85eff5f51 django_web "python3 manage.py..." 10 minutes ago
|
|||
|
||||
* Or, for a more elegant shutdown, switch to a different shell, and run [docker-compose down](reference/down.md) from the top level of your Django sample project directory.
|
||||
|
||||
```none
|
||||
```console
|
||||
vmb at mymachine in ~/sandbox/django
|
||||
$ docker-compose down
|
||||
|
||||
Stopping django_web_1 ... done
|
||||
Stopping django_db_1 ... done
|
||||
Removing django_web_1 ... done
|
||||
|
|
|
|||
|
|
@ -19,59 +19,61 @@ both are provided by Docker images.
|
|||
|
||||
Define the application dependencies.
|
||||
|
||||
1. Create a directory for the project:
|
||||
1. Create a directory for the project:
|
||||
|
||||
$ mkdir composetest
|
||||
$ cd composetest
|
||||
```console
|
||||
$ mkdir composetest
|
||||
$ cd composetest
|
||||
```
|
||||
|
||||
2. Create a file called `app.py` in your project directory and paste this in:
|
||||
2. Create a file called `app.py` in your project directory and paste this in:
|
||||
|
||||
import time
|
||||
```python
|
||||
import time
|
||||
|
||||
import redis
|
||||
from flask import Flask
|
||||
import redis
|
||||
from flask import Flask
|
||||
|
||||
app = Flask(__name__)
|
||||
cache = redis.Redis(host='redis', port=6379)
|
||||
app = Flask(__name__)
|
||||
cache = redis.Redis(host='redis', port=6379)
|
||||
|
||||
def get_hit_count():
|
||||
retries = 5
|
||||
while True:
|
||||
try:
|
||||
return cache.incr('hits')
|
||||
except redis.exceptions.ConnectionError as exc:
|
||||
if retries == 0:
|
||||
raise exc
|
||||
retries -= 1
|
||||
time.sleep(0.5)
|
||||
|
||||
def get_hit_count():
|
||||
retries = 5
|
||||
while True:
|
||||
try:
|
||||
return cache.incr('hits')
|
||||
except redis.exceptions.ConnectionError as exc:
|
||||
if retries == 0:
|
||||
raise exc
|
||||
retries -= 1
|
||||
time.sleep(0.5)
|
||||
@app.route('/')
|
||||
def hello():
|
||||
count = get_hit_count()
|
||||
return 'Hello World! I have been seen {} times.\n'.format(count)
|
||||
```
|
||||
|
||||
In this example, `redis` is the hostname of the redis container on the
|
||||
application's network. We use the default port for Redis, `6379`.
|
||||
|
||||
@app.route('/')
|
||||
def hello():
|
||||
count = get_hit_count()
|
||||
return 'Hello World! I have been seen {} times.\n'.format(count)
|
||||
> Handling transient errors
|
||||
>
|
||||
> Note the way the `get_hit_count` function is written. This basic retry
|
||||
> loop lets us attempt our request multiple times if the redis service is
|
||||
> not available. This is useful at startup while the application comes
|
||||
> online, but also makes our application more resilient if the Redis
|
||||
> service needs to be restarted anytime during the app's lifetime. In a
|
||||
> cluster, this also helps handling momentary connection drops between
|
||||
> nodes.
|
||||
|
||||
3. Create another file called `requirements.txt` in your project directory and
|
||||
paste this in:
|
||||
|
||||
In this example, `redis` is the hostname of the redis container on the
|
||||
application's network. We use the default port for Redis, `6379`.
|
||||
|
||||
> Handling transient errors
|
||||
>
|
||||
> Note the way the `get_hit_count` function is written. This basic retry
|
||||
> loop lets us attempt our request multiple times if the redis service is
|
||||
> not available. This is useful at startup while the application comes
|
||||
> online, but also makes our application more resilient if the Redis
|
||||
> service needs to be restarted anytime during the app's lifetime. In a
|
||||
> cluster, this also helps handling momentary connection drops between
|
||||
> nodes.
|
||||
|
||||
|
||||
3. Create another file called `requirements.txt` in your project directory and
|
||||
paste this in:
|
||||
|
||||
flask
|
||||
redis
|
||||
```text
|
||||
flask
|
||||
redis
|
||||
```
|
||||
|
||||
## Step 2: Create a Dockerfile
|
||||
|
||||
|
|
@ -82,16 +84,18 @@ itself.
|
|||
In your project directory, create a file named `Dockerfile` and paste the
|
||||
following:
|
||||
|
||||
FROM python:3.7-alpine
|
||||
WORKDIR /code
|
||||
ENV FLASK_APP app.py
|
||||
ENV FLASK_RUN_HOST 0.0.0.0
|
||||
RUN apk add --no-cache gcc musl-dev linux-headers
|
||||
COPY requirements.txt requirements.txt
|
||||
RUN pip install -r requirements.txt
|
||||
EXPOSE 5000
|
||||
COPY . .
|
||||
CMD ["flask", "run"]
|
||||
```dockerfile
|
||||
FROM python:3.7-alpine
|
||||
WORKDIR /code
|
||||
ENV FLASK_APP=app.py
|
||||
ENV FLASK_RUN_HOST=0.0.0.0
|
||||
RUN apk add --no-cache gcc musl-dev linux-headers
|
||||
COPY requirements.txt requirements.txt
|
||||
RUN pip install -r requirements.txt
|
||||
EXPOSE 5000
|
||||
COPY . .
|
||||
CMD ["flask", "run"]
|
||||
```
|
||||
|
||||
This tells Docker to:
|
||||
|
||||
|
|
@ -114,14 +118,16 @@ and the [Dockerfile reference](/engine/reference/builder/).
|
|||
Create a file called `docker-compose.yml` in your project directory and paste
|
||||
the following:
|
||||
|
||||
version: '3'
|
||||
services:
|
||||
web:
|
||||
build: .
|
||||
ports:
|
||||
- "5000:5000"
|
||||
redis:
|
||||
image: "redis:alpine"
|
||||
```yaml
|
||||
version: "{{ site.compose_file_v3 }}"
|
||||
services:
|
||||
web:
|
||||
build: .
|
||||
ports:
|
||||
- "5000:5000"
|
||||
redis:
|
||||
image: "redis:alpine"
|
||||
```
|
||||
|
||||
This Compose file defines two services: `web` and `redis`.
|
||||
|
||||
|
|
@ -138,99 +144,102 @@ image pulled from the Docker Hub registry.
|
|||
|
||||
## Step 4: Build and run your app with Compose
|
||||
|
||||
1. From your project directory, start up your application by running `docker-compose up`.
|
||||
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
|
||||
```
|
||||
```console
|
||||
$ docker-compose up
|
||||
|
||||
Compose pulls a Redis image, builds an image for your code, and starts the
|
||||
services you defined. In this case, the code is statically copied into the image at build time.
|
||||
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
|
||||
```
|
||||
|
||||
2. Enter http://localhost:5000/ in a browser to see the application running.
|
||||
Compose pulls a Redis image, builds an image for your code, and starts the
|
||||
services you defined. In this case, the code is statically copied into the image at build time.
|
||||
|
||||
If you're using Docker natively on Linux, Docker Desktop for Mac, or Docker Desktop 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://127.0.0.1:5000.
|
||||
2. Enter http://localhost:5000/ in a browser to see the application running.
|
||||
|
||||
If you're using Docker Machine on a Mac or Windows, 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.
|
||||
If you're using Docker natively on Linux, Docker Desktop for Mac, or Docker Desktop 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://127.0.0.1:5000.
|
||||
|
||||
You should see a message in your browser saying:
|
||||
If you're using Docker Machine on a Mac or Windows, 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.
|
||||
|
||||
```
|
||||
Hello World! I have been seen 1 times.
|
||||
```
|
||||
You should see a message in your browser saying:
|
||||
|
||||

|
||||
```console
|
||||
Hello World! I have been seen 1 times.
|
||||
```
|
||||
|
||||
3. Refresh the page.
|
||||

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

|
||||
```console
|
||||
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`.
|
||||
4. Switch to another terminal window, and type `docker image ls` to list local images.
|
||||
|
||||
```
|
||||
$ 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
|
||||
```
|
||||
Listing images at this point should return `redis` and `web`.
|
||||
|
||||
You can inspect images with `docker inspect <tag or id>`.
|
||||
```console
|
||||
$ docker image ls
|
||||
|
||||
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.
|
||||
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](../storage/bind-mounts.md) for the `web` service:
|
||||
|
||||
version: '3'
|
||||
services:
|
||||
web:
|
||||
build: .
|
||||
ports:
|
||||
- "5000:5000"
|
||||
volumes:
|
||||
- .:/code
|
||||
environment:
|
||||
FLASK_ENV: development
|
||||
redis:
|
||||
image: "redis:alpine"
|
||||
```yaml
|
||||
version: "{{ site.compose_file_v3 }}"
|
||||
services:
|
||||
web:
|
||||
build: .
|
||||
ports:
|
||||
- "5000:5000"
|
||||
volumes:
|
||||
- .:/code
|
||||
environment:
|
||||
FLASK_ENV: development
|
||||
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
|
||||
|
|
@ -242,8 +251,9 @@ mode and reload the code on change. This mode should only be used in development
|
|||
|
||||
From your project directory, type `docker-compose up` to build the app with the updated Compose file, and run it.
|
||||
|
||||
```
|
||||
```console
|
||||
$ docker-compose up
|
||||
|
||||
Creating network "composetest_default" with the default driver
|
||||
Creating composetest_web_1 ...
|
||||
Creating composetest_redis_1 ...
|
||||
|
|
@ -282,16 +292,17 @@ 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.
|
||||
|
||||
1. Change the greeting in `app.py` and save it. For example, change the `Hello World!` message to `Hello from Docker!`:
|
||||
Change the greeting in `app.py` and save it. For example, change the `Hello World!`
|
||||
message to `Hello from Docker!`:
|
||||
|
||||
```
|
||||
return 'Hello from Docker! I have been seen {} times.\n'.format(count)
|
||||
```
|
||||
```python
|
||||
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.
|
||||
Refresh the app in your browser. The greeting should be updated, and the
|
||||
counter should still be incrementing.
|
||||
|
||||

|
||||

|
||||
|
||||
## Step 8: Experiment with some other commands
|
||||
|
||||
|
|
@ -299,34 +310,44 @@ 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
|
||||
see what is currently running:
|
||||
|
||||
$ docker-compose up -d
|
||||
Starting composetest_redis_1...
|
||||
Starting composetest_web_1...
|
||||
```console
|
||||
$ docker-compose up -d
|
||||
|
||||
$ docker-compose ps
|
||||
Name Command State Ports
|
||||
-------------------------------------------------------------------
|
||||
composetest_redis_1 /usr/local/bin/run Up
|
||||
composetest_web_1 /bin/sh -c python app.py Up 5000->5000/tcp
|
||||
Starting composetest_redis_1...
|
||||
Starting composetest_web_1...
|
||||
|
||||
$ docker-compose ps
|
||||
|
||||
Name Command State Ports
|
||||
-------------------------------------------------------------------
|
||||
composetest_redis_1 /usr/local/bin/run Up
|
||||
composetest_web_1 /bin/sh -c python app.py Up 5000->5000/tcp
|
||||
```
|
||||
|
||||
The `docker-compose run` command allows you to run one-off commands for your
|
||||
services. For example, to see what environment variables are available to the
|
||||
`web` service:
|
||||
|
||||
$ docker-compose run web env
|
||||
```console
|
||||
$ docker-compose run web env
|
||||
```
|
||||
|
||||
See `docker-compose --help` to see other available commands. You can also install [command completion](completion.md) for the bash and zsh shell, which also shows you available commands.
|
||||
|
||||
If you started Compose with `docker-compose up -d`, stop
|
||||
your services once you've finished with them:
|
||||
|
||||
$ docker-compose stop
|
||||
```console
|
||||
$ docker-compose stop
|
||||
```
|
||||
|
||||
You can bring everything down, removing the containers entirely, with the `down`
|
||||
command. Pass `--volumes` to also remove the data volume used by the Redis
|
||||
container:
|
||||
|
||||
$ docker-compose down --volumes
|
||||
```console
|
||||
$ docker-compose down --volumes
|
||||
```
|
||||
|
||||
At this point, you have seen the basics of how Compose works.
|
||||
|
||||
|
|
|
|||
|
|
@ -631,7 +631,7 @@ the recipient container back to the source (ie, `MYSQL_PORT_3306_TCP`).
|
|||
|
||||
To make new software easier to run, you can use `ENV` to update the
|
||||
`PATH` environment variable for the software your container installs. For
|
||||
example, `ENV PATH /usr/local/nginx/bin:$PATH` ensures that `CMD ["nginx"]`
|
||||
example, `ENV PATH=/usr/local/nginx/bin:$PATH` ensures that `CMD ["nginx"]`
|
||||
just works.
|
||||
|
||||
The `ENV` instruction is also useful for providing required environment
|
||||
|
|
@ -642,10 +642,10 @@ Lastly, `ENV` can also be used to set commonly used version numbers so that
|
|||
version bumps are easier to maintain, as seen in the following example:
|
||||
|
||||
```dockerfile
|
||||
ENV PG_MAJOR 9.3
|
||||
ENV PG_VERSION 9.3.4
|
||||
ENV PG_MAJOR=9.3
|
||||
ENV PG_VERSION=9.3.4
|
||||
RUN curl -SL http://example.com/postgres-$PG_VERSION.tar.xz | tar -xJC /usr/src/postgress && …
|
||||
ENV PATH /usr/local/postgres-$PG_MAJOR/bin:$PATH
|
||||
ENV PATH=/usr/local/postgres-$PG_MAJOR/bin:$PATH
|
||||
```
|
||||
|
||||
Similar to having constant variables in a program (as opposed to hard-coding
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ RUN sed -i 's/.*PermitRootLogin.*/PermitRootLogin yes/' /etc/ssh/sshd_config
|
|||
# SSH login fix. Otherwise user is kicked off after login
|
||||
RUN sed 's@session\s*required\s*pam_loginuid.so@session optional pam_loginuid.so@g' -i /etc/pam.d/sshd
|
||||
|
||||
ENV NOTVISIBLE "in users profile"
|
||||
ENV NOTVISIBLE="in users profile"
|
||||
RUN echo "export VISIBLE=now" >> /etc/profile
|
||||
|
||||
EXPOSE 22
|
||||
|
|
|
|||
|
|
@ -30,7 +30,7 @@ RUN sed -i 's/#*PermitRootLogin prohibit-password/PermitRootLogin yes/g' /etc/ss
|
|||
# SSH login fix. Otherwise user is kicked off after login
|
||||
RUN sed -i 's@session\s*required\s*pam_loginuid.so@session optional pam_loginuid.so@g' /etc/pam.d/sshd
|
||||
|
||||
ENV NOTVISIBLE "in users profile"
|
||||
ENV NOTVISIBLE="in users profile"
|
||||
RUN echo "export VISIBLE=now" >> /etc/profile
|
||||
|
||||
EXPOSE 22
|
||||
|
|
|
|||
|
|
@ -61,8 +61,8 @@ instead.
|
|||
|
||||
| Variable | Dockerfile example | `docker run` Example |
|
||||
|:--------------|:--------------------------------------------------|:----------------------------------------------------|
|
||||
| `HTTP_PROXY` | `ENV HTTP_PROXY "http://127.0.0.1:3001"` | `--env HTTP_PROXY="http://127.0.0.1:3001"` |
|
||||
| `HTTPS_PROXY` | `ENV HTTPS_PROXY "https://127.0.0.1:3001"` | `--env HTTPS_PROXY="https://127.0.0.1:3001"` |
|
||||
| `FTP_PROXY` | `ENV FTP_PROXY "ftp://127.0.0.1:3001"` | `--env FTP_PROXY="ftp://127.0.0.1:3001"` |
|
||||
| `NO_PROXY` | `ENV NO_PROXY "*.test.example.com,.example2.com"` | `--env NO_PROXY="*.test.example.com,.example2.com"` |
|
||||
| `HTTP_PROXY` | `ENV HTTP_PROXY="http://127.0.0.1:3001"` | `--env HTTP_PROXY="http://127.0.0.1:3001"` |
|
||||
| `HTTPS_PROXY` | `ENV HTTPS_PROXY="https://127.0.0.1:3001"` | `--env HTTPS_PROXY="https://127.0.0.1:3001"` |
|
||||
| `FTP_PROXY` | `ENV FTP_PROXY="ftp://127.0.0.1:3001"` | `--env FTP_PROXY="ftp://127.0.0.1:3001"` |
|
||||
| `NO_PROXY` | `ENV NO_PROXY="*.test.example.com,.example2.com"` | `--env NO_PROXY="*.test.example.com,.example2.com"` |
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue