Skip to content

Commit

Permalink
Merge branch 'master' into docs-wording-improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanScherer authored Feb 10, 2022
2 parents ad6f32f + 8725352 commit 5b6733e
Show file tree
Hide file tree
Showing 12 changed files with 2,365 additions and 2,998 deletions.
14 changes: 9 additions & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,24 @@ WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt

# Run tests to validate app
FROM node:12-alpine AS app-base
RUN apk add --no-cache python g++ make
WORKDIR /app
COPY app/package.json app/yarn.lock ./
RUN yarn install
COPY app/spec ./spec
COPY app/src ./src

# Run tests to validate app
FROM app-base AS test
RUN apk add --no-cache python3 g++ make
RUN yarn install
RUN yarn test

# Clear out the node_modules and create the zip
FROM app-base AS app-zip-creator
RUN rm -rf node_modules && \
apk add zip && \
COPY app/package.json app/yarn.lock ./
COPY app/spec ./spec
COPY app/src ./src
RUN apk add zip && \
zip -r /app.zip /app

# Dev-ready container - actual files will be mounted in
Expand Down
10 changes: 6 additions & 4 deletions app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@
"dev": "nodemon src/index.js"
},
"dependencies": {
"body-parser": "^1.19.0",
"express": "^4.17.1",
"mysql": "^2.17.1",
"sqlite3": "^4.1.0",
"sqlite3": "^5.0.0",
"uuid": "^3.3.3",
"wait-port": "^0.2.2"
},
"resolutions": {
"ansi-regex": "5.0.1"
},
"prettier": {
"trailingComma": "all",
"tabWidth": 4,
Expand All @@ -24,8 +26,8 @@
"singleQuote": true
},
"devDependencies": {
"jest": "^24.9.0",
"nodemon": "^1.19.2",
"jest": "^27.2.5",
"nodemon": "^2.0.13",
"prettier": "^1.18.2"
}
}
5 changes: 3 additions & 2 deletions app/spec/persistence/sqlite.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const db = require('../../src/persistence/sqlite');
const fs = require('fs');
const location = process.env.SQLITE_DB_LOCATION || '/etc/todos/todo.db';

const ITEM = {
id: '7aef3d7c-d301-4846-8358-2a91ec9d6be3',
Expand All @@ -8,8 +9,8 @@ const ITEM = {
};

beforeEach(() => {
if (fs.existsSync('/etc/todos/todo.db')) {
fs.unlinkSync('/etc/todos/todo.db');
if (fs.existsSync(location)) {
fs.unlinkSync(location);
}
});

Expand Down
2 changes: 1 addition & 1 deletion app/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const addItem = require('./routes/addItem');
const updateItem = require('./routes/updateItem');
const deleteItem = require('./routes/deleteItem');

app.use(require('body-parser').json());
app.use(express.json());
app.use(express.static(__dirname + '/static'));

app.get('/items', getItems);
Expand Down
5,237 changes: 2,281 additions & 2,956 deletions app/yarn.lock

Large diffs are not rendered by default.

36 changes: 33 additions & 3 deletions docs/tutorial/multi-container-apps/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ application stack. The following question often arises - "Where will MySQL run?
container or run it separately?" In general, **each container should do one thing and do it well.** A few
reasons:

- There's a good chance you'd have to scale APIs and front-ends differently than databases
- Separate containers let you version and update versions in isolation
- There's a good chance you'd have to scale APIs and front-ends differently than databases.
- Separate containers let you version and update versions in isolation.
- While you may use a container for the database locally, you may want to use a managed service
for the database in production. You don't want to ship your database engine with your app then.
- Running multiple processes will require a process manager (the container only starts one process),
which adds complexity to container startup/shutdown
which adds complexity to container startup/shutdown.

And there are more reasons. So, we will update our application to work like this:

Expand Down Expand Up @@ -66,6 +66,20 @@ For now, we will create the network first and attach the MySQL container at star
You'll notice we're using a volume named `todo-mysql-data` here and mounting it at `/var/lib/mysql`, which is
where MySQL stores its data. However, we never ran a `docker volume create` command. Docker recognizes we want
to use a named volume and creates one automatically for us.
!!! info "Troubleshooting"
If you see a `docker: no matching manifest` error, it's because you're trying to run the container in a different
architecture than amd64, which is the only supported architecture for the mysql image at the moment. To solve this
add the flag `--platform linux/amd64` in the previous command. So your new command should look like this:
```bash
docker run -d \
--network todo-app --network-alias mysql --platform linux/amd64 \
-v todo-mysql-data:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=secret \
-e MYSQL_DATABASE=todos \
mysql:5.7
```
1. To confirm we have the database up and running, connect to the database and verify it connects.
Expand Down Expand Up @@ -96,6 +110,8 @@ For now, we will create the network first and attach the MySQL container at star
```
Hooray! We have our `todos` database and it's ready for us to use!

To exit the sql terminal type `exit` in the terminal.


## Connecting to MySQL
Expand Down Expand Up @@ -189,6 +205,20 @@ With all of that explained, let's start our dev-ready container!
node:12-alpine \
sh -c "yarn install && yarn run dev"
```

If you updated your docker file in the Bind Mount section of the tutorial use the updated command:

```bash hl_lines="3 4 5 6 7"
docker run -dp 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:12-alpine \
sh -c "apk --no-cache --virtual build-dependencies add python2 make g++ && yarn install && yarn run dev"
```

If you are using PowerShell then use this command.

Expand Down
2 changes: 2 additions & 0 deletions docs/tutorial/our-application/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ see a few flaws in the Dockerfile below. But, don't worry! We'll go over them.

```dockerfile
FROM node:12-alpine
# Adding build tools to make yarn install work on Apple silicon / arm64 machines
RUN apk add --no-cache python2 g++ make
WORKDIR /app
COPY . .
RUN yarn install --production
Expand Down
9 changes: 6 additions & 3 deletions docs/tutorial/persisting-our-data/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,10 @@ What you'll see is that the files created in one container aren't available in a
And look! There's no `data.txt` file there! That's because it was written to the scratch space for
only the first container.
1. Go ahead and remove the first container using the `docker rm -f` command.
1. Go ahead and remove the first container using the `docker rm -f <container-id>` command.
```bash
docker rm -f <container-id>
```
## Container Volumes
Expand Down Expand Up @@ -91,7 +94,7 @@ Every time you use the volume, Docker will make sure the correct data is provide
docker volume create todo-db
```

1. Stop the todo app container once again in the Dashboard (or with `docker rm -f <id>`), as it is still running without using the persistent volume.
1. Stop the todo app container once again in the Dashboard (or with `docker rm -f <container-id>`), as it is still running without using the persistent volume.

1. Start the todo app container, but add the `-v` flag to specify a volume mount. We will use the named volume and mount
it to `/etc/todos`, which will capture all files created at the path.
Expand All @@ -105,7 +108,7 @@ Every time you use the volume, Docker will make sure the correct data is provide
![Items added to todo list](items-added.png){: style="width: 55%; " }
{: .text-center }

1. Remove the container for the todo app. Use the Dashboard or `docker ps` to get the ID and then `docker rm -f <id>` to remove it.
1. Remove the container for the todo app. Use the Dashboard or `docker ps` to get the ID and then `docker rm -f <container-id>` to remove it.

1. Start a new container using the same command from above.

Expand Down
4 changes: 2 additions & 2 deletions docs/tutorial/sharing-our-app/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ an example command that you will need to run to push to this repo.
Now that our image has been built and pushed into a registry, let's try running our app on a brand
new instance that has never seen this container image! To do this, we will use Play with Docker.
1. Open your browser to [Play with Docker](http://play-with-docker.com).
1. Open your browser to [Play with Docker](https://labs.play-with-docker.com/).
1. Log in with your Docker Hub account.
Expand Down Expand Up @@ -89,4 +89,4 @@ can use the latest version of the image.
Now that we have that figured out, let's circle back around to what we noticed at the end of the last
section. As a reminder, we noticed that when we restarted the app, we lost all of our todo list items.
That's obviously not a great user experience, so let's learn how we can persist the data across
restarts!
restarts!
10 changes: 5 additions & 5 deletions docs/tutorial/using-bind-mounts/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ changes and then restart the application. There are equivalent tools in most oth
## Quick Volume Type Comparisons

Bind mounts and named volumes are the two main types of volumes that come with the Docker engine. However, additional
volume drivers are available to support other uses cases ([SFTP](https://github.com/vieux/docker-volume-sshfs), [Ceph](https://ceph.com/geen-categorie/getting-started-with-the-docker-rbd-volume-plugin/), [NetApp](https://netappdvp.readthedocs.io/en/stable/), [S3](https://github.com/elementar/docker-s3-volume), and more).
volume drivers are available to support other use cases ([SFTP](https://github.com/vieux/docker-volume-sshfs), [Ceph](https://ceph.com/geen-categorie/getting-started-with-the-docker-rbd-volume-plugin/), [NetApp](https://netappdvp.readthedocs.io/en/stable/), [S3](https://github.com/elementar/docker-s3-volume), and more).

| | Named Volumes | Bind Mounts |
| - | ------------- | ----------- |
Expand All @@ -36,7 +36,7 @@ So, let's do it!

1. Make sure you don't have any previous `getting-started` containers running.

1. Run the following command. We'll explain what's going on afterwards:
1. Run the following command from the source code folder. We'll explain what's going on afterwards:

```bash
docker run -dp 3000:3000 \
Expand All @@ -55,8 +55,8 @@ So, let's do it!
```

- `-dp 3000:3000` - same as before. Run in detached (background) mode and create a port mapping
- `-w /app` - sets the "working directory" or the current directory that the command will run from
- `-v "$(pwd):/app"` - bind mount the current directory from the host in the container into the `/app` directory
- `-w /app` - sets the container's present working directory where the command will run from
- `-v "$(pwd):/app"` - bind mount (link) the host's present working directory to the container's `/app` directory
- `node:12-alpine` - the image to use. Note that this is the base image for our app from the Dockerfile
- `sh -c "yarn install && yarn run dev"` - the command. We're starting a shell using `sh` (alpine doesn't have `bash`) and
running `yarn install` to install _all_ dependencies and then running `yarn run dev`. If we look in the `package.json`,
Expand All @@ -78,7 +78,7 @@ So, let's do it!
When you're done watching the logs, exit out by hitting `Ctrl`+`C`.
1. Now, let's make a change to the app. In the `src/static/js/app.js` file, let's change the "Add Item" button to simply say
"Add". This change will be on line 109.
"Add". This change will be on line 109 - remember to save the file.
```diff
- {submitting ? 'Adding...' : 'Add Item'}
Expand Down
30 changes: 15 additions & 15 deletions docs/tutorial/using-docker-compose/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ docker-compose version
for the current schema versions and the compatibility matrix.

```yaml
version: "3.7"
version: "3.8"
```
1. Next, we'll define the list of services (or containers) we want to run as part of our application.
```yaml hl_lines="3"
version: "3.7"
version: "3.8"

services:
```
Expand Down Expand Up @@ -81,7 +81,7 @@ docker run -dp 3000:3000 `
The name will automatically become a network alias, which will be useful when defining our MySQL service.

```yaml hl_lines="4 5"
version: "3.7"
version: "3.8"

services:
app:
Expand All @@ -92,7 +92,7 @@ docker run -dp 3000:3000 `
So, let's go ahead and move that into our file.

```yaml hl_lines="6"
version: "3.7"
version: "3.8"
services:
app:
Expand All @@ -102,11 +102,11 @@ docker run -dp 3000:3000 `


1. Let's migrate the `-p 3000:3000` part of the command by defining the `ports` for the service. We will use the
[short syntax](https://docs.docker.com/compose/compose-file/#short-syntax-1) here, but there is also a more verbose
[long syntax](https://docs.docker.com/compose/compose-file/#long-syntax-1) available as well.
[short syntax](https://docs.docker.com/compose/compose-file/compose-file-v3/#short-syntax-1) here, but there is also a more verbose
[long syntax](https://docs.docker.com/compose/compose-file/compose-file-v3/#long-syntax-1) available as well.

```yaml hl_lines="7 8"
version: "3.7"
version: "3.8"
services:
app:
Expand All @@ -117,12 +117,12 @@ docker run -dp 3000:3000 `
```

1. Next, we'll migrate both the working directory (`-w /app`) and the volume mapping (`-v "$(pwd):/app"`) by using
the `working_dir` and `volumes` definitions. Volumes also has a [short](https://docs.docker.com/compose/compose-file/#short-syntax-3) and [long](https://docs.docker.com/compose/compose-file/#long-syntax-3) syntax.
the `working_dir` and `volumes` definitions. Volumes also has a [short](https://docs.docker.com/compose/compose-file/compose-file-v3/#short-syntax-3) and [long](https://docs.docker.com/compose/compose-file/compose-file-v3/#long-syntax-3) syntax.

One advantage of Docker Compose volume definitions is we can use relative paths from the current directory.

```yaml hl_lines="9 10 11"
version: "3.7"
version: "3.8"
services:
app:
Expand All @@ -138,7 +138,7 @@ docker run -dp 3000:3000 `
1. Finally, we need to migrate the environment variable definitions using the `environment` key.

```yaml hl_lines="12 13 14 15 16"
version: "3.7"
version: "3.8"
services:
app:
Expand Down Expand Up @@ -185,7 +185,7 @@ docker run -d `
go ahead and specify the image to use as well.

```yaml hl_lines="6 7"
version: "3.7"
version: "3.8"

services:
app:
Expand All @@ -197,10 +197,10 @@ docker run -d `
1. Next, we'll define the volume mapping. When we ran the container with `docker run`, the named volume was created
automatically. However, that doesn't happen when running with Compose. We need to define the volume in the top-level
`volumes:` section and then specify the mountpoint in the service config. By simply providing only the volume name,
the default options are used. There are [many more options available](https://docs.docker.com/compose/compose-file/#volume-configuration-reference) though.
the default options are used. There are [many more options available](https://docs.docker.com/compose/compose-file/compose-file-v3/#volume-configuration-reference) though.

```yaml hl_lines="8 9 10 11 12"
version: "3.7"
version: "3.8"
services:
app:
Expand All @@ -217,7 +217,7 @@ docker run -d `
1. Finally, we only need to specify the environment variables.

```yaml hl_lines="10 11 12"
version: "3.7"
version: "3.8"
services:
app:
Expand All @@ -238,7 +238,7 @@ At this point, our complete `docker-compose.yml` should look like this:


```yaml
version: "3.7"
version: "3.8"
services:
app:
Expand Down
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
mkdocs==1.0.4
mkdocs==1.2.3
mkdocs-material==4.6.3
mkdocs-minify-plugin==0.2.3
pygments==2.6.1
pygments==2.7.4
pymdown-extensions==7.0

0 comments on commit 5b6733e

Please sign in to comment.