Skip to content

Commit

Permalink
Merge pull request docker#317 from docker/update-to-latest-versions-o…
Browse files Browse the repository at this point in the history
…f-everything
  • Loading branch information
mikesir87 authored Nov 25, 2022
2 parents b4f92f7 + 3ad6934 commit d7d4b09
Show file tree
Hide file tree
Showing 24 changed files with 1,822 additions and 2,710 deletions.
3 changes: 2 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
node_modules
app/Dockerfile
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
app/Dockerfile
5 changes: 2 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,20 @@ WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt

FROM node:12-alpine AS app-base
FROM node:18-alpine AS app-base
WORKDIR /app
COPY app/package.json app/yarn.lock ./
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
COPY app/package.json app/yarn.lock ./
COPY --from=test /app/package.json /app/yarn.lock ./
COPY app/spec ./spec
COPY app/src ./src
RUN apk add zip && \
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ This project has a `docker-compose.yml` file, which will start the mkdocs applic
local machine and help you see changes instantly.

```bash
docker-compose up
docker compose up
```

## Contributing
Expand Down
16 changes: 8 additions & 8 deletions app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
"dev": "nodemon src/index.js"
},
"dependencies": {
"express": "^4.17.1",
"mysql": "^2.17.1",
"sqlite3": "^5.0.0",
"uuid": "^3.3.3",
"wait-port": "^0.2.2"
"express": "^4.18.2",
"mysql2": "^2.3.3",
"sqlite3": "^5.1.2",
"uuid": "^9.0.0",
"wait-port": "^1.0.4"
},
"resolutions": {
"ansi-regex": "5.0.1"
Expand All @@ -26,8 +26,8 @@
"singleQuote": true
},
"devDependencies": {
"jest": "^27.2.5",
"nodemon": "^2.0.13",
"prettier": "^1.18.2"
"jest": "^29.3.1",
"nodemon": "^2.0.20",
"prettier": "^2.7.1"
}
}
4 changes: 2 additions & 2 deletions app/spec/routes/addItem.spec.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const db = require('../../src/persistence');
const addItem = require('../../src/routes/addItem');
const ITEM = { id: 12345 };
const uuid = require('uuid/v4');
const {v4 : uuid} = require('uuid');

jest.mock('uuid/v4', () => jest.fn());
jest.mock('uuid', () => ({ v4: jest.fn() }));

jest.mock('../../src/persistence', () => ({
removeItem: jest.fn(),
Expand Down
2 changes: 1 addition & 1 deletion app/src/persistence/mysql.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const waitPort = require('wait-port');
const fs = require('fs');
const mysql = require('mysql');
const mysql = require('mysql2');

const {
MYSQL_HOST: HOST,
Expand Down
2 changes: 1 addition & 1 deletion app/src/routes/addItem.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const db = require('../persistence');
const uuid = require('uuid/v4');
const {v4 : uuid} = require('uuid');

module.exports = async (req, res) => {
const item = {
Expand Down
4,154 changes: 1,688 additions & 2,466 deletions app/yarn.lock

Large diffs are not rendered by default.

122 changes: 54 additions & 68 deletions docs/tutorial/image-building-best-practices/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,19 @@ command, you can see the command that was used to create each layer within an im

```plaintext
IMAGE CREATED CREATED BY SIZE COMMENT
a78a40cbf866 18 seconds ago /bin/sh -c #(nop) CMD ["node" "src/index.j… 0B
f1d1808565d6 19 seconds ago /bin/sh -c yarn install --production 85.4MB
a2c054d14948 36 seconds ago /bin/sh -c #(nop) COPY dir:5dc710ad87c789593… 198kB
9577ae713121 37 seconds ago /bin/sh -c #(nop) WORKDIR /app 0B
b95baba1cfdb 13 days ago /bin/sh -c #(nop) CMD ["node"] 0B
<missing> 13 days ago /bin/sh -c #(nop) ENTRYPOINT ["docker-entry… 0B
<missing> 13 days ago /bin/sh -c #(nop) COPY file:238737301d473041116B
<missing> 13 days ago /bin/sh -c apk add --no-cache --virtual .bui… 5.35MB
<missing> 13 days ago /bin/sh -c #(nop) ENV YARN_VERSION=1.21.1 0B
<missing> 13 days ago /bin/sh -c addgroup -g 1000 node && addu… 74.3MB
<missing> 13 days ago /bin/sh -c #(nop) ENV NODE_VERSION=12.14.1 0B
<missing> 13 days ago /bin/sh -c #(nop) CMD ["/bin/sh"] 0B
<missing> 13 days ago /bin/sh -c #(nop) ADD file:e69d441d729412d24… 5.59MB
05bd8640b718 53 minutes ago CMD ["node" "src/index.js"] 0B buildkit.dockerfile.v0
<missing> 53 minutes ago RUN /bin/sh -c yarn install --production # b… 83.3MB buildkit.dockerfile.v0
<missing> 53 minutes ago COPY . . # buildkit 4.59MB buildkit.dockerfile.v0
<missing> 55 minutes ago WORKDIR /app 0B buildkit.dockerfile.v0
<missing> 10 days ago /bin/sh -c #(nop) CMD ["node"] 0B
<missing> 10 days ago /bin/sh -c #(nop) ENTRYPOINT ["docker-entry… 0B
<missing> 10 days ago /bin/sh -c #(nop) COPY file:4d192565a7220e13388B
<missing> 10 days ago /bin/sh -c apk add --no-cache --virtual .bui… 7.85MB
<missing> 10 days ago /bin/sh -c #(nop) ENV YARN_VERSION=1.22.19 0B
<missing> 10 days ago /bin/sh -c addgroup -g 1000 node && addu… 152MB
<missing> 10 days ago /bin/sh -c #(nop) ENV NODE_VERSION=18.12.1 0B
<missing> 11 days ago /bin/sh -c #(nop) CMD ["/bin/sh"] 0B
<missing> 11 days ago /bin/sh -c #(nop) ADD file:57d621536158358b1… 5.29MB
```

Each of the lines represents a layer in the image. The display here shows the base at the bottom with
Expand All @@ -95,7 +95,7 @@ times for your container images.
Let's look at the Dockerfile we were using one more time...
```dockerfile
FROM node:12-alpine
FROM node:18-alpine
WORKDIR /app
COPY . .
RUN yarn install --production
Expand All @@ -114,7 +114,7 @@ a change to the `package.json`. Make sense?
1. Update the Dockerfile to copy in the `package.json` first, install dependencies, and then copy everything else in.

```dockerfile hl_lines="3 4 5"
FROM node:12-alpine
FROM node:18-alpine
WORKDIR /app
COPY package.json yarn.lock ./
RUN yarn install --production
Expand Down Expand Up @@ -146,34 +146,23 @@ a change to the `package.json`. Make sense?
You should see output like this...

```plaintext
Sending build context to Docker daemon 219.1kB
Step 1/6 : FROM node:12-alpine
---> b0dc3a5e5e9e
Step 2/6 : WORKDIR /app
---> Using cache
---> 9577ae713121
Step 3/6 : COPY package.json yarn.lock ./
---> bd5306f49fc8
Step 4/6 : RUN yarn install --production
---> Running in d53a06c9e4c2
yarn install v1.17.3
[1/4] Resolving packages...
[2/4] Fetching packages...
info [email protected]: The platform "linux" is incompatible with this module.
info "[email protected]" is an optional dependency and failed compatibility check. Excluding it from installation.
[3/4] Linking dependencies...
[4/4] Building fresh packages...
Done in 10.89s.
Removing intermediate container d53a06c9e4c2
---> 4e68fbc2d704
Step 5/6 : COPY . .
---> a239a11f68d8
Step 6/6 : CMD ["node", "src/index.js"]
---> Running in 49999f68df8f
Removing intermediate container 49999f68df8f
---> e709c03bc597
Successfully built e709c03bc597
Successfully tagged getting-started:latest
[+] Building 16.1s (10/10) FINISHED
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 175B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [internal] load metadata for docker.io/library/node:18-alpine 0.0s
=> [internal] load build context 0.8s
=> => transferring context: 53.37MB 0.8s
=> [1/5] FROM docker.io/library/node:18-alpine 0.0s
=> CACHED [2/5] WORKDIR /app 0.0s
=> [3/5] COPY package.json yarn.lock ./ 0.2s
=> [4/5] RUN yarn install --production 14.0s
=> [5/5] COPY . . 0.5s
=> exporting to image 0.6s
=> => exporting layers 0.6s
=> => writing image sha256:d6f819013566c54c50124ed94d5e66c452325327217f4f04399b45f94e37d25 0.0s
=> => naming to docker.io/library/getting-started 0.0s
```

You'll see that all layers were rebuilt. Perfectly fine since we changed the Dockerfile quite a bit.
Expand All @@ -182,31 +171,28 @@ a change to the `package.json`. Make sense?
1. Build the Docker image now using `docker build -t getting-started .` again. This time, your output should look a little different.
```plaintext hl_lines="5 8 11"
Sending build context to Docker daemon 219.1kB
Step 1/6 : FROM node:12-alpine
---> b0dc3a5e5e9e
Step 2/6 : WORKDIR /app
---> Using cache
---> 9577ae713121
Step 3/6 : COPY package.json yarn.lock ./
---> Using cache
---> bd5306f49fc8
Step 4/6 : RUN yarn install --production
---> Using cache
---> 4e68fbc2d704
Step 5/6 : COPY . .
---> cccde25a3d9a
Step 6/6 : CMD ["node", "src/index.js"]
---> Running in 2be75662c150
Removing intermediate container 2be75662c150
---> 458e5c6f080c
Successfully built 458e5c6f080c
Successfully tagged getting-started:latest
```plaintext hl_lines="10 11 12"
[+] Building 1.2s (10/10) FINISHED
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 37B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [internal] load metadata for docker.io/library/node:18-alpine 0.0s
=> [internal] load build context 0.2s
=> => transferring context: 450.43kB 0.2s
=> [1/5] FROM docker.io/library/node:18-alpine 0.0s
=> CACHED [2/5] WORKDIR /app 0.0s
=> CACHED [3/5] COPY package.json yarn.lock ./ 0.0s
=> CACHED [4/5] RUN yarn install --production 0.0s
=> [5/5] COPY . . 0.5s
=> exporting to image 0.3s
=> => exporting layers 0.3s
=> => writing image sha256:91790c87bcb096a83c2bd4eb512bc8b134c757cda0bdee4038187f98148e2eda 0.0s
=> => naming to docker.io/library/getting-started 0.0s
```
First off, you should notice that the build was MUCH faster! And, you'll see that steps 1-4 all have
`Using cache`. So, hooray! We're using the build cache. Pushing and pulling this image and updates to it
First off, you should notice that the build was MUCH faster! And, you'll see that several steps are using
previously cached layers. So, hooray! We're using the build cache. Pushing and pulling this image and updates to it
will be much faster as well. Hooray!
Expand Down Expand Up @@ -246,7 +232,7 @@ and more into static HTML, JS, and CSS. If we aren't doing server-side rendering
for our production build. Why not ship the static resources in a static nginx container?

```dockerfile
FROM node:12 AS build
FROM node:18 AS build
WORKDIR /app
COPY package* yarn.lock ./
RUN yarn install
Expand All @@ -258,7 +244,7 @@ FROM nginx:alpine
COPY --from=build /app/build /usr/share/nginx/html
```

Here, we are using a `node:12` image to perform the build (maximizing layer caching) and then copying the output
Here, we are using a `node:18` image to perform the build (maximizing layer caching) and then copying the output
into an nginx container. Cool, huh?


Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
45 changes: 9 additions & 36 deletions docs/tutorial/multi-container-apps/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ For now, we will create the network first and attach the MySQL container at star
-v todo-mysql-data:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=secret \
-e MYSQL_DATABASE=todos \
mysql:5.7
mysql:8.0
```
If you are using PowerShell then use this command.
Expand All @@ -57,7 +57,7 @@ For now, we will create the network first and attach the MySQL container at star
-v todo-mysql-data:/var/lib/mysql `
-e MYSQL_ROOT_PASSWORD=secret `
-e MYSQL_DATABASE=todos `
mysql:5.7
mysql:8.0
```
You'll also see we specified the `--network-alias` flag. We'll come back to that in just a moment.
Expand All @@ -67,20 +67,6 @@ For now, we will create the network first and attach the MySQL container at star
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.
```bash
Expand Down Expand Up @@ -139,7 +125,7 @@ which ships with a _lot_ of tools that are useful for troubleshooting or debuggi
And you'll get an output like this...
```text
; <<>> DiG 9.14.1 <<>> mysql
; <<>> DiG 9.18.8 <<>> mysql
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 32162
Expand Down Expand Up @@ -177,7 +163,7 @@ The todo app supports the setting of a few environment variables to specify MySQ

!!! warning Setting Connection Settings via Env Vars
While using env vars to set connection settings is generally ok for development, it is **HIGHLY DISCOURAGED**
when running applications in production. Diogo Monica, the former lead of security at Docker,
when running applications in production. Diogo Monica, a former lead of security at Docker,
[wrote a fantastic blog post](https://diogomonica.com/2017/03/27/why-you-shouldnt-use-env-variables-for-secret-data/)
explaining why.

Expand All @@ -202,24 +188,10 @@ With all of that explained, let's start our dev-ready container!
-e MYSQL_USER=root \
-e MYSQL_PASSWORD=secret \
-e MYSQL_DB=todos \
node:12-alpine \
node:18-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.

```powershell hl_lines="3 4 5 6 7"
Expand All @@ -230,7 +202,7 @@ With all of that explained, let's start our dev-ready container!
-e MYSQL_USER=root `
-e MYSQL_PASSWORD=secret `
-e MYSQL_DB=todos `
node:12-alpine `
node:18-alpine `
sh -c "yarn install && yarn run dev"
```

Expand All @@ -240,9 +212,10 @@ With all of that explained, let's start our dev-ready container!
```plaintext hl_lines="7"
# Previous log messages omitted
$ nodemon src/index.js
[nodemon] 1.19.2
[nodemon] 2.0.20
[nodemon] to restart at any time, enter `rs`
[nodemon] watching dir(s): *.*
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node src/index.js`
Connected to mysql db at host mysql
Listening on port 3000
Expand Down
Binary file modified docs/tutorial/our-application/dashboard-two-containers.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 2 additions & 4 deletions docs/tutorial/our-application/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ see a few flaws in the Dockerfile below. But, don't worry! We'll go over them.
1. Create a file named `Dockerfile` in the same folder as the file `package.json` with the following contents.

```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
FROM node:18-alpine
WORKDIR /app
COPY . .
RUN yarn install --production
Expand All @@ -56,7 +54,7 @@ see a few flaws in the Dockerfile below. But, don't worry! We'll go over them.
This command used the Dockerfile to build a new container image. You might
have noticed that a lot of "layers" were downloaded. This is because we instructed
the builder that we wanted to start from the `node:12-alpine` image. But, since we
the builder that we wanted to start from the `node:18-alpine` image. But, since we
didn't have that on our machine, that image needed to be downloaded.

After the image was downloaded, we copied in our application and used `yarn` to
Expand Down
Binary file modified docs/tutorial/persisting-our-data/dashboard-open-cli-ubuntu.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit d7d4b09

Please sign in to comment.