Add compose samples from docs (#305)
Signed-off-by: Stefan Scherer <stefan.scherer@docker.com> Signed-off-by: Stefan Scherer <stefan.scherer@docker.com> Signed-off-by: Andrew Maguire <andrewm4894@gmail.com>pull/307/head
parent
36fedc4a51
commit
586e1c3bcc
@ -0,0 +1,28 @@ |
||||
# Sample apps with Compose |
||||
|
||||
The following samples show the various aspects of how to work with Docker |
||||
Compose. As a prerequisite, be sure to [install Docker Compose](https://docs.docker.com/compose/install/) |
||||
if you have not already done so. |
||||
|
||||
## Key concepts these samples cover |
||||
|
||||
The samples should help you to: |
||||
|
||||
- define services based on Docker images using |
||||
[Compose files](https://docs.docker.com/compose/compose-file/) `docker-compose.yml` files |
||||
- understand the relationship between `docker-compose.yml` and |
||||
[Dockerfiles](https://docs.docker.com/engine/reference/builder/) |
||||
- learn how to make calls to your application services from Compose files |
||||
|
||||
## Samples tailored to demo Compose |
||||
|
||||
These samples focus specifically on Docker Compose: |
||||
|
||||
- [Quickstart: Compose and Django](./django/README.md) - Shows how to use Docker Compose to set up and run a simple Django/PostgreSQL app. |
||||
|
||||
- [Quickstart: Compose and Rails](./rails/README.md) - Shows how to use |
||||
Docker Compose to set up and run a Rails/PostgreSQL app. |
||||
|
||||
- [Quickstart: Compose and WordPress](./wordpress/README.md) - Shows how to |
||||
use Docker Compose to set up and run WordPress in an isolated environment |
||||
with Docker containers. |
@ -0,0 +1,286 @@ |
||||
# Quickstart: Compose and Django |
||||
|
||||
This quick-start guide demonstrates how to use Docker Compose to set up and run a simple Django/PostgreSQL app. Before starting, |
||||
[install Compose](https://docs.docker.com/compose/install/). |
||||
|
||||
## Define the project components |
||||
|
||||
For this project, you need to create a Dockerfile, a Python dependencies file, |
||||
and a `docker-compose.yml` file. (You can use either a `.yml` or `.yaml` extension for this file.) |
||||
|
||||
1. Create an empty project directory. |
||||
|
||||
You can name the directory something easy for you to remember. This directory is the context for your application image. The directory should only contain resources to build that image. |
||||
|
||||
2. Create a new file called `Dockerfile` in your project directory. |
||||
|
||||
The Dockerfile defines an application's image content via one or more build |
||||
commands that configure that image. Once built, you can run the image in a |
||||
container. For more information on `Dockerfile`, see the [Docker user guide](https://docs.docker.com/get-started/) |
||||
and the [Dockerfile reference](https://docs.docker.com/engine/reference/builder/). |
||||
|
||||
3. Add the following content to the `Dockerfile`. |
||||
|
||||
```dockerfile |
||||
# syntax=docker/dockerfile:1 |
||||
FROM python:3 |
||||
ENV PYTHONDONTWRITEBYTECODE=1 |
||||
ENV PYTHONUNBUFFERED=1 |
||||
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. |
||||
|
||||
4. Save and close the `Dockerfile`. |
||||
|
||||
5. Create a `requirements.txt` in your project directory. |
||||
|
||||
This file is used by the `RUN pip install -r requirements.txt` command in your `Dockerfile`. |
||||
|
||||
6. Add the required software in the file. |
||||
|
||||
```python |
||||
Django>=3.0,<4.0 |
||||
psycopg2>=2.8 |
||||
``` |
||||
|
||||
7. Save and close the `requirements.txt` file. |
||||
|
||||
8. Create a file called `docker-compose.yml` in your project directory. |
||||
|
||||
The `docker-compose.yml` file describes the services that make your app. In |
||||
this example those services are a web server and database. The compose file |
||||
also describes which Docker images these services use, how they link |
||||
together, any volumes they might need to be mounted inside the containers. |
||||
Finally, the `docker-compose.yml` file describes which ports these services |
||||
expose. See the [`docker-compose.yml` reference](https://docs.docker.com/compose/compose-file/) for more |
||||
information on how this file works. |
||||
|
||||
9. Add the following configuration to the file. |
||||
|
||||
```yaml |
||||
services: |
||||
db: |
||||
image: postgres |
||||
volumes: |
||||
- ./data/db:/var/lib/postgresql/data |
||||
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" |
||||
environment: |
||||
- POSTGRES_NAME=postgres |
||||
- POSTGRES_USER=postgres |
||||
- POSTGRES_PASSWORD=postgres |
||||
depends_on: |
||||
- db |
||||
``` |
||||
|
||||
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" rel="noopener" class="_”}. |
||||
|
||||
10. Save and close the `docker-compose.yml` file. |
||||
|
||||
## Create a Django project |
||||
|
||||
In this step, you create a Django starter project by building the image from the build context defined in the previous procedure. |
||||
|
||||
1. Change to the root of your project directory. |
||||
|
||||
2. Create the Django project by running the [docker compose run](https://docs.docker.com/engine/reference/commandline/compose_run/) |
||||
command as follows. |
||||
|
||||
```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 |
||||
the `web` image doesn't exist yet, Compose builds it from the current |
||||
directory, as specified by the `build: .` line in `docker-compose.yml`. |
||||
|
||||
Once the `web` service image is built, Compose runs it and executes the |
||||
`django-admin startproject` command in the container. This command |
||||
instructs Django to create a set of files and directories representing a |
||||
Django project. |
||||
|
||||
3. After the `docker compose` command completes, list the contents of your project. |
||||
|
||||
```console |
||||
$ ls -l |
||||
|
||||
drwxr-xr-x 2 root root composeexample |
||||
drwxr-xr-x 3 root root data |
||||
-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 |
||||
``` |
||||
|
||||
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. |
||||
|
||||
Do not change the permission of the data folder where Postgres has its file, otherwise Postgres will not be able to start due to permission issues. |
||||
|
||||
```console |
||||
sudo chown -R $USER:$USER composeexample manage.py |
||||
``` |
||||
|
||||
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. |
||||
|
||||
2. Replace the `DATABASES = ...` with the following: |
||||
|
||||
```python |
||||
# settings.py |
||||
|
||||
import os |
||||
|
||||
[...] |
||||
|
||||
DATABASES = { |
||||
'default': { |
||||
'ENGINE': 'django.db.backends.postgresql', |
||||
'NAME': os.environ.get('POSTGRES_NAME'), |
||||
'USER': os.environ.get('POSTGRES_USER'), |
||||
'PASSWORD': os.environ.get('POSTGRES_PASSWORD'), |
||||
'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. |
||||
|
||||
4. Run the [docker compose up](https://docs.docker.com/engine/reference/commandline/compose_up/) command from the top level directory for your project. |
||||
|
||||
```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. |
||||
|
||||
![Django example](images/django-it-worked.png) |
||||
|
||||
> 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: |
||||
> |
||||
> ```python |
||||
> 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. |
||||
|
||||
In another terminal window, list the running Docker processes with the `docker ps` or `docker container ls` command. |
||||
|
||||
```console |
||||
$ 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 |
||||
``` |
||||
|
||||
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: |
||||
|
||||
```console |
||||
Gracefully stopping... (press Ctrl+C again to force) |
||||
Killing test_web_1 ... done |
||||
Killing test_db_1 ... done |
||||
``` |
||||
|
||||
* Or, for a more elegant shutdown, switch to a different shell, and run |
||||
[docker compose down](https://docs.docker.com/engine/reference/commandline/compose_down/) from the top level of your |
||||
Django sample project directory. |
||||
|
||||
```console |
||||
$ docker compose down |
||||
|
||||
Stopping django_web_1 ... done |
||||
Stopping django_db_1 ... done |
||||
Removing django_web_1 ... done |
||||
Removing django_web_run_1 ... done |
||||
Removing django_db_1 ... done |
||||
Removing network django_default |
||||
``` |
||||
|
||||
Once you've shut down the app, you can safely remove the Django project directory (for example, `rm -rf django`). |
||||
|
||||
## More Compose documentation |
||||
|
||||
* [Docker Compose overview](https://docs.docker.com/compose/) |
||||
* [Install Docker Compose](https://docs.docker.com/compose/install/) |
||||
* [Getting Started with Docker Compose](https://docs.docker.com/compose/gettingstarted/) |
||||
* [Docker Compose Command line reference](https://docs.docker.com/compose/reference/) |
||||
* [Compose file reference](https://docs.docker.com/compose/compose-file/) |
||||
* [Awesome Compose Django sample application](../../django/README.md) |
After Width: | Height: | Size: 18 KiB |
@ -0,0 +1,272 @@ |
||||
# Quickstart: Compose and Rails |
||||
|
||||
This Quickstart guide shows you how to use Docker Compose to set up and run |
||||
a Rails/PostgreSQL app. Before starting, [install Compose](https://docs.docker.com/compose/install/). |
||||
|
||||
## Define the project |
||||
|
||||
Start by setting up the files needed to build the app. The app will run inside a |
||||
Docker container containing its dependencies. Defining dependencies is done using |
||||
a file called `Dockerfile`. To begin with, the Dockerfile consists of: |
||||
|
||||
```dockerfile |
||||
# syntax=docker/dockerfile:1 |
||||
FROM ruby:2.5 |
||||
RUN apt-get update -qq && apt-get install -y nodejs postgresql-client |
||||
WORKDIR /myapp |
||||
COPY Gemfile /myapp/Gemfile |
||||
COPY Gemfile.lock /myapp/Gemfile.lock |
||||
RUN bundle install |
||||
|
||||
# Add a script to be executed every time the container starts. |
||||
COPY entrypoint.sh /usr/bin/ |
||||
RUN chmod +x /usr/bin/entrypoint.sh |
||||
ENTRYPOINT ["entrypoint.sh"] |
||||
EXPOSE 3000 |
||||
|
||||
# Configure the main process to run when running the image |
||||
CMD ["rails", "server", "-b", "0.0.0.0"] |
||||
``` |
||||
|
||||
That'll put your application code inside an image that builds a container |
||||
with Ruby, Bundler and all your dependencies inside it. For more information on |
||||
how to write Dockerfiles, see the [Docker user guide](https://docs.docker.com/get-started/) |
||||
and the [Dockerfile reference](https://docs.docker.com/engine/reference/builder/). |
||||
|
||||
Next, open an editor and create a bootstrap `Gemfile` which just loads Rails. This will be overwritten in a moment by `rails new`. |
||||
|
||||
```ruby |
||||
source 'https://rubygems.org' |
||||
gem 'rails', '~>5' |
||||
``` |
||||
|
||||
Create an empty `Gemfile.lock` file to build our `Dockerfile`. |
||||
|
||||
```console |
||||
$ touch Gemfile.lock |
||||
``` |
||||
|
||||
Next, provide an entrypoint script to fix a Rails-specific issue that |
||||
prevents the server from restarting when a certain `server.pid` file pre-exists. |
||||
This script will be executed every time the container gets started. |
||||
`entrypoint.sh` consists of: |
||||
|
||||
```bash |
||||
#!/bin/bash |
||||
set -e |
||||
|
||||
# Remove a potentially pre-existing server.pid for Rails. |
||||
rm -f /myapp/tmp/pids/server.pid |
||||
|
||||
# Then exec the container's main process (what's set as CMD in the Dockerfile). |
||||
exec "$@" |
||||
``` |
||||
|
||||
Finally, `docker-compose.yml` is where the magic happens. This file describes |
||||
the services that comprise your app (a database and a web app), how to get each |
||||
one's Docker image (the database just runs on a pre-made PostgreSQL image, and |
||||
the web app is built from the current directory), and the configuration needed |
||||
to link them together and expose the web app's port. |
||||
|
||||
```yaml |
||||
services: |
||||
db: |
||||
image: postgres |
||||
volumes: |
||||
- ./tmp/db:/var/lib/postgresql/data |
||||
environment: |
||||
POSTGRES_PASSWORD: password |
||||
web: |
||||
build: . |
||||
command: bash -c "rm -f tmp/pids/server.pid && bundle exec rails s -p 3000 -b '0.0.0.0'" |
||||
volumes: |
||||
- .:/myapp |
||||
ports: |
||||
- "3000:3000" |
||||
depends_on: |
||||
- db |
||||
``` |
||||
|
||||
> **Tip** |
||||
> |
||||
> You can use either a `.yml` or `.yaml` extension for this file. |
||||
|
||||
### Build the project |
||||
|
||||
With those files in place, you can now generate the Rails skeleton app |
||||
using [docker compose run](https://docs.docker.com/engine/reference/commandline/compose_run/): |
||||
|
||||
```console |
||||
$ docker compose run --no-deps web rails new . --force --database=postgresql |
||||
``` |
||||
|
||||
First, Compose builds the image for the `web` service using the `Dockerfile`. |
||||
The `--no-deps` tells Compose not to start linked services. Then it runs |
||||
`rails new` inside a new container, using that image. Once it's done, you |
||||
should have generated a fresh app. |
||||
|
||||
List the files. |
||||
|
||||
```console |
||||
$ ls -l |
||||
|
||||
total 64 |
||||
-rw-r--r-- 1 vmb staff 222 Jun 7 12:05 Dockerfile |
||||
-rw-r--r-- 1 vmb staff 1738 Jun 7 12:09 Gemfile |
||||
-rw-r--r-- 1 vmb staff 4297 Jun 7 12:09 Gemfile.lock |
||||
-rw-r--r-- 1 vmb staff 374 Jun 7 12:09 README.md |
||||
-rw-r--r-- 1 vmb staff 227 Jun 7 12:09 Rakefile |
||||
drwxr-xr-x 10 vmb staff 340 Jun 7 12:09 app |
||||
drwxr-xr-x 8 vmb staff 272 Jun 7 12:09 bin |
||||
drwxr-xr-x 14 vmb staff 476 Jun 7 12:09 config |
||||
-rw-r--r-- 1 vmb staff 130 Jun 7 12:09 config.ru |
||||
drwxr-xr-x 3 vmb staff 102 Jun 7 12:09 db |
||||
-rw-r--r-- 1 vmb staff 211 Jun 7 12:06 docker-compose.yml |
||||
-rw-r--r-- 1 vmb staff 184 Jun 7 12:08 entrypoint.sh |
||||
drwxr-xr-x 4 vmb staff 136 Jun 7 12:09 lib |
||||
drwxr-xr-x 3 vmb staff 102 Jun 7 12:09 log |
||||
-rw-r--r-- 1 vmb staff 63 Jun 7 12:09 package.json |
||||
drwxr-xr-x 9 vmb staff 306 Jun 7 12:09 public |
||||
drwxr-xr-x 9 vmb staff 306 Jun 7 12:09 test |
||||
drwxr-xr-x 4 vmb staff 136 Jun 7 12:09 tmp |
||||
drwxr-xr-x 3 vmb staff 102 Jun 7 12:09 vendor |
||||
``` |
||||
|
||||
If you are running Docker on Linux, the files `rails new` created are owned by |
||||
root. This happens because the container runs as the root user. If this is the |
||||
case, change the ownership of the new files. |
||||
|
||||
```console |
||||
$ sudo chown -R $USER:$USER . |
||||
``` |
||||
|
||||
If you are running Docker on Mac or Windows, you should already have ownership |
||||
of all files, including those generated by `rails new`. |
||||
|
||||
Now that you’ve got a new Gemfile, you need to build the image again. (This, and |
||||
changes to the `Gemfile` or the Dockerfile, should be the only times you’ll need |
||||
to rebuild.) |
||||
|
||||
```console |
||||
$ docker compose build |
||||
``` |
||||
|
||||
### Connect the database |
||||
|
||||
The app is now bootable, but you're not quite there yet. By default, Rails |
||||
expects a database to be running on `localhost` - so you need to point it at the |
||||
`db` container instead. You also need to change the database and username to |
||||
align with the defaults set by the `postgres` image. |
||||
|
||||
Replace the contents of `config/database.yml` with the following: |
||||
|
||||
```yaml |
||||
default: &default |
||||
adapter: postgresql |
||||
encoding: unicode |
||||
host: db |
||||
username: postgres |
||||
password: password |
||||
pool: 5 |
||||
|
||||
development: |
||||
<<: *default |
||||
database: myapp_development |
||||
|
||||
|
||||
test: |
||||
<<: *default |
||||
database: myapp_test |
||||
``` |
||||
|
||||
You can now boot the app with [docker compose up](https://docs.docker.com/engine/reference/commandline/compose_up/). |
||||
If all is well, you should see some PostgreSQL output: |
||||
|
||||
```console |
||||
$ docker compose up |
||||
|
||||
rails_db_1 is up-to-date |
||||
Creating rails_web_1 ... done |
||||
Attaching to rails_db_1, rails_web_1 |
||||
db_1 | PostgreSQL init process complete; ready for start up. |
||||
db_1 | |
||||
db_1 | 2018-03-21 20:18:37.437 UTC [1] LOG: listening on IPv4 address "0.0.0.0", port 5432 |
||||
db_1 | 2018-03-21 20:18:37.437 UTC [1] LOG: listening on IPv6 address "::", port 5432 |
||||
db_1 | 2018-03-21 20:18:37.443 UTC [1] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432" |
||||
db_1 | 2018-03-21 20:18:37.726 UTC [55] LOG: database system was shut down at 2018-03-21 20:18:37 UTC |
||||
db_1 | 2018-03-21 20:18:37.772 UTC [1] LOG: database system is ready to accept connections |
||||
``` |
||||
|
||||
Finally, you need to create the database. In another terminal, run: |
||||
|
||||
```console |
||||
$ docker compose run web rake db:create |
||||
Starting rails_db_1 ... done |
||||
Created database 'myapp_development' |
||||
Created database 'myapp_test' |
||||
``` |
||||
|
||||
### View the Rails welcome page! |
||||
|
||||
That's it. Your app should now be running on port 3000 on your Docker daemon. |
||||
|
||||
On Docker Desktop for Mac and Docker Desktop for Windows, go to `http://localhost:3000` on a web |
||||
browser to see the Rails Welcome. |
||||
|
||||
![Rails example](images/rails-welcome.png) |
||||
|
||||
### Stop the application |
||||
|
||||
To stop the application, run [docker compose down](https://docs.docker.com/engine/reference/commandline/compose_down/) in |
||||
your project directory. You can use the same terminal window in which you |
||||
started the database, or another one where you have access to a command prompt. |
||||
This is a clean way to stop the application. |
||||
|
||||
```console |
||||
$ docker compose down |
||||
|
||||
Stopping rails_web_1 ... done |
||||
Stopping rails_db_1 ... done |
||||
Removing rails_web_run_1 ... done |
||||
Removing rails_web_1 ... done |
||||
Removing rails_db_1 ... done |
||||
Removing network rails_default |
||||
|
||||
``` |
||||
|
||||
### Restart the application |
||||
|
||||
To restart the application run `docker compose up` in the project directory. |
||||
|
||||
### Rebuild the application |
||||
|
||||
If you make changes to the Gemfile or the Compose file to try out some different |
||||
configurations, you need to rebuild. Some changes require only |
||||
`docker compose up --build`, but a full rebuild requires a re-run of |
||||
`docker compose run web bundle install` to sync changes in the `Gemfile.lock` to |
||||
the host, followed by `docker compose up --build`. |
||||
|
||||
Here is an example of the first case, where a full rebuild is not necessary. |
||||
Suppose you simply want to change the exposed port on the local host from `3000` |
||||
in our first example to `3001`. Make the change to the Compose file to expose |
||||
port `3000` on the container through a new port, `3001`, on the host, and save |
||||
the changes: |
||||
|
||||
```yaml |
||||
ports: |
||||
- "3001:3000" |
||||
``` |
||||
|
||||
Now, rebuild and restart the app with `docker compose up --build`. |
||||
|
||||
Inside the container, your app is running on the same port as before `3000`, but |
||||
the Rails Welcome is now available on `http://localhost:3001` on your local |
||||
host. |
||||
|
||||
## More Compose documentation |
||||
|
||||
* [Docker Compose overview](https://docs.docker.com/compose/) |
||||
* [Install Docker Compose](https://docs.docker.com/compose/install/) |
||||
* [Getting Started with Docker Compose](https://docs.docker.com/compose/gettingstarted/) |
||||
* [Docker Compose Command line reference](https://docs.docker.com/compose/reference/) |
||||
* [Compose file reference](https://docs.docker.com/compose/compose-file/) |
After Width: | Height: | Size: 334 KiB |
@ -0,0 +1,147 @@ |
||||
# Quickstart: Compose and WordPress |
||||
|
||||
You can use Docker Compose to easily run WordPress in an isolated environment |
||||
built with Docker containers. This quick-start guide demonstrates how to use |
||||
Compose to set up and run WordPress. Before starting, make sure you have |
||||
[Compose installed](https://docs.docker.com/compose/install/). |
||||
|
||||
## Define the project |
||||
|
||||
1. Create an empty project directory. |
||||
|
||||
You can name the directory something easy for you to remember. |
||||
This directory is the context for your application image. The |
||||
directory should only contain resources to build that image. |
||||
|
||||
This project directory contains a `docker-compose.yml` file which |
||||
is complete in itself for a good starter wordpress project. |
||||
|
||||
>**Tip**: You can use either a `.yml` or `.yaml` extension for |
||||
this file. They both work. |
||||
|
||||
2. Change into your project directory. |
||||
|
||||
For example, if you named your directory `my_wordpress`: |
||||
|
||||
```console |
||||
$ cd my_wordpress/ |
||||
``` |
||||
|
||||
3. Create a `docker-compose.yml` file that starts your |
||||
`WordPress` blog and a separate `MySQL` instance with volume |
||||
mounts for data persistence: |
||||
|
||||
```yaml |
||||
services: |
||||
db: |
||||
# We use a mariadb image which supports both amd64 & arm64 architecture |
||||
image: mariadb:10.6.4-focal |
||||
# If you really want to use MySQL, uncomment the following line |
||||
#image: mysql:8.0.27 |
||||
command: '--default-authentication-plugin=mysql_native_password' |
||||
volumes: |
||||
- db_data:/var/lib/mysql |
||||
restart: always |
||||
environment: |
||||
- MYSQL_ROOT_PASSWORD=somewordpress |
||||
- MYSQL_DATABASE=wordpress |
||||
- MYSQL_USER=wordpress |
||||
- MYSQL_PASSWORD=wordpress |
||||
expose: |
||||
- 3306 |
||||
- 33060 |
||||
wordpress: |
||||
image: wordpress:latest |
||||
volumes: |
||||
- wp_data:/var/www/html |
||||
ports: |
||||
- 80:80 |
||||
restart: always |
||||
environment: |
||||
- WORDPRESS_DB_HOST=db |
||||
- WORDPRESS_DB_USER=wordpress |
||||
- WORDPRESS_DB_PASSWORD=wordpress |
||||
- WORDPRESS_DB_NAME=wordpress |
||||
volumes: |
||||
db_data: |
||||
wp_data: |
||||
``` |
||||
|
||||
> **Notes**: |
||||
> |
||||
* The docker volumes `db_data` and `wordpress_data` persists updates made by WordPress |
||||
to the database, as well as the installed themes and plugins. [Learn more about docker volumes](https://docs.docker.com/storage/volumes/) |
||||
> |
||||
* WordPress Multisite works only on ports `80` and `443`. |
||||
{: .note-vanilla} |
||||
|
||||
### Build the project |
||||
|
||||
Now, run `docker compose up -d` from your project directory. |
||||
|
||||
This runs [`docker compose up`](https://docs.docker.com/engine/reference/commandline/compose_up/) in detached mode, pulls |
||||
the needed Docker images, and starts the wordpress and database containers, as shown in |
||||
the example below. |
||||
|
||||
```console |
||||
$ docker compose up -d |
||||
|
||||
Creating network "my_wordpress_default" with the default driver |
||||
Pulling db (mysql:5.7)... |
||||
5.7: Pulling from library/mysql |
||||
efd26ecc9548: Pull complete |
||||
a3ed95caeb02: Pull complete |
||||
<...> |
||||
Digest: sha256:34a0aca88e85f2efa5edff1cea77cf5d3147ad93545dbec99cfe705b03c520de |
||||
Status: Downloaded newer image for mysql:5.7 |
||||
Pulling wordpress (wordpress:latest)... |
||||
latest: Pulling from library/wordpress |
||||
efd26ecc9548: Already exists |
||||
a3ed95caeb02: Pull complete |
||||
589a9d9a7c64: Pull complete |
||||
<...> |
||||
Digest: sha256:ed28506ae44d5def89075fd5c01456610cd6c64006addfe5210b8c675881aff6 |
||||
Status: Downloaded newer image for wordpress:latest |
||||
Creating my_wordpress_db_1 |
||||
Creating my_wordpress_wordpress_1 |
||||
``` |
||||
|
||||
> **Note**: WordPress Multisite works only on ports `80` and/or `443`. |
||||
If you get an error message about binding `0.0.0.0` to port `80` or `443` |
||||
(depending on which one you specified), it is likely that the port you |
||||
configured for WordPress is already in use by another service. |
||||
|
||||
### Bring up WordPress in a web browser |
||||
|
||||
At this point, WordPress should be running on port `80` of your Docker Host, |
||||
and you can complete the "famous five-minute installation" as a WordPress |
||||
administrator. |
||||
|
||||
> **Note**: The WordPress site is not immediately available on port `80` |
||||
because the containers are still being initialized and may take a couple of |
||||
minutes before the first load. |
||||
|
||||
If you are using Docker Desktop for Mac or Docker Desktop for Windows, you can use |
||||
`http://localhost` as the IP address, and open `http://localhost:80` in a web |
||||
browser. |
||||
|
||||
![Choose language for WordPress install](images/wordpress-lang.png) |
||||
|
||||
![WordPress Welcome](images/wordpress-welcome.png) |
||||
|
||||
### Shutdown and cleanup |
||||
|
||||
The command [`docker compose down`](https://docs.docker.com/engine/reference/commandline/compose_down/) removes the |
||||
containers and default network, but preserves your WordPress database. |
||||
|
||||
The command `docker compose down --volumes` removes the containers, default |
||||
network, and the WordPress database. |
||||
|
||||
## More Compose documentation |
||||
|
||||
* [Docker Compose overview](https://docs.docker.com/compose/) |
||||
* [Install Docker Compose](https://docs.docker.com/compose/install/) |
||||
* [Getting Started with Docker Compose](https://docs.docker.com/compose/gettingstarted/) |
||||
* [Docker Compose Command line reference](https://docs.docker.com/compose/reference/) |
||||
* [Compose file reference](https://docs.docker.com/compose/compose-file/) |
||||
* [Awesome Compose WordPress sample](../../wordpress-mysql/README.md) |
After Width: | Height: | Size: 29 KiB |
After Width: | Height: | Size: 60 KiB |
Loading…
Reference in new issue