Merge pull request #317 from docker/update-to-latest-versions-of-everything

This commit is contained in:
Michael Irwin 2022-11-24 22:45:46 -05:00 committed by GitHub
commit d7d4b09c4f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
24 changed files with 1788 additions and 2676 deletions

View File

@ -1 +1,2 @@
node_modules node_modules
app/Dockerfile

1
.gitignore vendored
View File

@ -1 +1,2 @@
node_modules node_modules
app/Dockerfile

View File

@ -5,7 +5,7 @@ WORKDIR /app
COPY requirements.txt . COPY requirements.txt .
RUN pip install -r requirements.txt RUN pip install -r requirements.txt
FROM node:12-alpine AS app-base FROM node:18-alpine AS app-base
WORKDIR /app WORKDIR /app
COPY app/package.json app/yarn.lock ./ COPY app/package.json app/yarn.lock ./
COPY app/spec ./spec COPY app/spec ./spec
@ -13,13 +13,12 @@ COPY app/src ./src
# Run tests to validate app # Run tests to validate app
FROM app-base AS test FROM app-base AS test
RUN apk add --no-cache python3 g++ make
RUN yarn install RUN yarn install
RUN yarn test RUN yarn test
# Clear out the node_modules and create the zip # Clear out the node_modules and create the zip
FROM app-base AS app-zip-creator 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/spec ./spec
COPY app/src ./src COPY app/src ./src
RUN apk add zip && \ RUN apk add zip && \

View File

@ -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. local machine and help you see changes instantly.
```bash ```bash
docker-compose up docker compose up
``` ```
## Contributing ## Contributing

View File

@ -9,11 +9,11 @@
"dev": "nodemon src/index.js" "dev": "nodemon src/index.js"
}, },
"dependencies": { "dependencies": {
"express": "^4.17.1", "express": "^4.18.2",
"mysql": "^2.17.1", "mysql2": "^2.3.3",
"sqlite3": "^5.0.0", "sqlite3": "^5.1.2",
"uuid": "^3.3.3", "uuid": "^9.0.0",
"wait-port": "^0.2.2" "wait-port": "^1.0.4"
}, },
"resolutions": { "resolutions": {
"ansi-regex": "5.0.1" "ansi-regex": "5.0.1"
@ -26,8 +26,8 @@
"singleQuote": true "singleQuote": true
}, },
"devDependencies": { "devDependencies": {
"jest": "^27.2.5", "jest": "^29.3.1",
"nodemon": "^2.0.13", "nodemon": "^2.0.20",
"prettier": "^1.18.2" "prettier": "^2.7.1"
} }
} }

View File

@ -1,9 +1,9 @@
const db = require('../../src/persistence'); const db = require('../../src/persistence');
const addItem = require('../../src/routes/addItem'); const addItem = require('../../src/routes/addItem');
const ITEM = { id: 12345 }; 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', () => ({ jest.mock('../../src/persistence', () => ({
removeItem: jest.fn(), removeItem: jest.fn(),

View File

@ -1,6 +1,6 @@
const waitPort = require('wait-port'); const waitPort = require('wait-port');
const fs = require('fs'); const fs = require('fs');
const mysql = require('mysql'); const mysql = require('mysql2');
const { const {
MYSQL_HOST: HOST, MYSQL_HOST: HOST,

View File

@ -1,5 +1,5 @@
const db = require('../persistence'); const db = require('../persistence');
const uuid = require('uuid/v4'); const {v4 : uuid} = require('uuid');
module.exports = async (req, res) => { module.exports = async (req, res) => {
const item = { const item = {

File diff suppressed because it is too large Load Diff

View File

@ -58,19 +58,19 @@ command, you can see the command that was used to create each layer within an im
```plaintext ```plaintext
IMAGE CREATED CREATED BY SIZE COMMENT IMAGE CREATED CREATED BY SIZE COMMENT
a78a40cbf866 18 seconds ago /bin/sh -c #(nop) CMD ["node" "src/index.j… 0B 05bd8640b718 53 minutes ago CMD ["node" "src/index.js"] 0B buildkit.dockerfile.v0
f1d1808565d6 19 seconds ago /bin/sh -c yarn install --production 85.4MB <missing> 53 minutes ago RUN /bin/sh -c yarn install --production # b… 83.3MB buildkit.dockerfile.v0
a2c054d14948 36 seconds ago /bin/sh -c #(nop) COPY dir:5dc710ad87c789593… 198kB <missing> 53 minutes ago COPY . . # buildkit 4.59MB buildkit.dockerfile.v0
9577ae713121 37 seconds ago /bin/sh -c #(nop) WORKDIR /app 0B <missing> 55 minutes ago WORKDIR /app 0B buildkit.dockerfile.v0
b95baba1cfdb 13 days ago /bin/sh -c #(nop) CMD ["node"] 0B <missing> 10 days ago /bin/sh -c #(nop) CMD ["node"] 0B
<missing> 13 days ago /bin/sh -c #(nop) ENTRYPOINT ["docker-entry… 0B <missing> 10 days ago /bin/sh -c #(nop) ENTRYPOINT ["docker-entry… 0B
<missing> 13 days ago /bin/sh -c #(nop) COPY file:238737301d473041… 116B <missing> 10 days ago /bin/sh -c #(nop) COPY file:4d192565a7220e13… 388B
<missing> 13 days ago /bin/sh -c apk add --no-cache --virtual .bui… 5.35MB <missing> 10 days ago /bin/sh -c apk add --no-cache --virtual .bui… 7.85MB
<missing> 13 days ago /bin/sh -c #(nop) ENV YARN_VERSION=1.21.1 0B <missing> 10 days ago /bin/sh -c #(nop) ENV YARN_VERSION=1.22.19 0B
<missing> 13 days ago /bin/sh -c addgroup -g 1000 node && addu… 74.3MB <missing> 10 days ago /bin/sh -c addgroup -g 1000 node && addu… 152MB
<missing> 13 days ago /bin/sh -c #(nop) ENV NODE_VERSION=12.14.1 0B <missing> 10 days ago /bin/sh -c #(nop) ENV NODE_VERSION=18.12.1 0B
<missing> 13 days ago /bin/sh -c #(nop) CMD ["/bin/sh"] 0B <missing> 11 days ago /bin/sh -c #(nop) CMD ["/bin/sh"] 0B
<missing> 13 days ago /bin/sh -c #(nop) ADD file:e69d441d729412d24… 5.59MB <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 Each of the lines represents a layer in the image. The display here shows the base at the bottom with
@ -95,7 +95,7 @@ times for your container images.
Let's look at the Dockerfile we were using one more time... Let's look at the Dockerfile we were using one more time...
```dockerfile ```dockerfile
FROM node:12-alpine FROM node:18-alpine
WORKDIR /app WORKDIR /app
COPY . . COPY . .
RUN yarn install --production RUN yarn install --production
@ -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. 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" ```dockerfile hl_lines="3 4 5"
FROM node:12-alpine FROM node:18-alpine
WORKDIR /app WORKDIR /app
COPY package.json yarn.lock ./ COPY package.json yarn.lock ./
RUN yarn install --production RUN yarn install --production
@ -146,34 +146,23 @@ a change to the `package.json`. Make sense?
You should see output like this... You should see output like this...
```plaintext ```plaintext
Sending build context to Docker daemon 219.1kB [+] Building 16.1s (10/10) FINISHED
Step 1/6 : FROM node:12-alpine => [internal] load build definition from Dockerfile 0.0s
---> b0dc3a5e5e9e => => transferring dockerfile: 175B 0.0s
Step 2/6 : WORKDIR /app => [internal] load .dockerignore 0.0s
---> Using cache => => transferring context: 2B 0.0s
---> 9577ae713121 => [internal] load metadata for docker.io/library/node:18-alpine 0.0s
Step 3/6 : COPY package.json yarn.lock ./ => [internal] load build context 0.8s
---> bd5306f49fc8 => => transferring context: 53.37MB 0.8s
Step 4/6 : RUN yarn install --production => [1/5] FROM docker.io/library/node:18-alpine 0.0s
---> Running in d53a06c9e4c2 => CACHED [2/5] WORKDIR /app 0.0s
yarn install v1.17.3 => [3/5] COPY package.json yarn.lock ./ 0.2s
[1/4] Resolving packages... => [4/5] RUN yarn install --production 14.0s
[2/4] Fetching packages... => [5/5] COPY . . 0.5s
info fsevents@1.2.9: The platform "linux" is incompatible with this module. => exporting to image 0.6s
info "fsevents@1.2.9" is an optional dependency and failed compatibility check. Excluding it from installation. => => exporting layers 0.6s
[3/4] Linking dependencies... => => writing image sha256:d6f819013566c54c50124ed94d5e66c452325327217f4f04399b45f94e37d25 0.0s
[4/4] Building fresh packages... => => naming to docker.io/library/getting-started 0.0s
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
``` ```
You'll see that all layers were rebuilt. Perfectly fine since we changed the Dockerfile quite a bit. You'll see that all layers were rebuilt. Perfectly fine since we changed the Dockerfile quite a bit.
@ -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. 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" ```plaintext hl_lines="10 11 12"
Sending build context to Docker daemon 219.1kB [+] Building 1.2s (10/10) FINISHED
Step 1/6 : FROM node:12-alpine => [internal] load build definition from Dockerfile 0.0s
---> b0dc3a5e5e9e => => transferring dockerfile: 37B 0.0s
Step 2/6 : WORKDIR /app => [internal] load .dockerignore 0.0s
---> Using cache => => transferring context: 2B 0.0s
---> 9577ae713121 => [internal] load metadata for docker.io/library/node:18-alpine 0.0s
Step 3/6 : COPY package.json yarn.lock ./ => [internal] load build context 0.2s
---> Using cache => => transferring context: 450.43kB 0.2s
---> bd5306f49fc8 => [1/5] FROM docker.io/library/node:18-alpine 0.0s
Step 4/6 : RUN yarn install --production => CACHED [2/5] WORKDIR /app 0.0s
---> Using cache => CACHED [3/5] COPY package.json yarn.lock ./ 0.0s
---> 4e68fbc2d704 => CACHED [4/5] RUN yarn install --production 0.0s
Step 5/6 : COPY . . => [5/5] COPY . . 0.5s
---> cccde25a3d9a => exporting to image 0.3s
Step 6/6 : CMD ["node", "src/index.js"] => => exporting layers 0.3s
---> Running in 2be75662c150 => => writing image sha256:91790c87bcb096a83c2bd4eb512bc8b134c757cda0bdee4038187f98148e2eda 0.0s
Removing intermediate container 2be75662c150 => => naming to docker.io/library/getting-started 0.0s
---> 458e5c6f080c
Successfully built 458e5c6f080c
Successfully tagged getting-started:latest
``` ```
First off, you should notice that the build was MUCH faster! And, you'll see that steps 1-4 all have First off, you should notice that the build was MUCH faster! And, you'll see that several steps are using
`Using cache`. So, hooray! We're using the build cache. Pushing and pulling this image and updates to it 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! will be much faster as well. Hooray!
@ -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? for our production build. Why not ship the static resources in a static nginx container?
```dockerfile ```dockerfile
FROM node:12 AS build FROM node:18 AS build
WORKDIR /app WORKDIR /app
COPY package* yarn.lock ./ COPY package* yarn.lock ./
RUN yarn install RUN yarn install
@ -258,7 +244,7 @@ FROM nginx:alpine
COPY --from=build /app/build /usr/share/nginx/html 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? into an nginx container. Cool, huh?

Binary file not shown.

Before

Width:  |  Height:  |  Size: 121 KiB

After

Width:  |  Height:  |  Size: 280 KiB

View File

@ -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 \ -v todo-mysql-data:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=secret \ -e MYSQL_ROOT_PASSWORD=secret \
-e MYSQL_DATABASE=todos \ -e MYSQL_DATABASE=todos \
mysql:5.7 mysql:8.0
``` ```
If you are using PowerShell then use this command. If you are using PowerShell then use this command.
@ -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 ` -v todo-mysql-data:/var/lib/mysql `
-e MYSQL_ROOT_PASSWORD=secret ` -e MYSQL_ROOT_PASSWORD=secret `
-e MYSQL_DATABASE=todos ` -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. You'll also see we specified the `--network-alias` flag. We'll come back to that in just a moment.
@ -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 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. 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. 1. To confirm we have the database up and running, connect to the database and verify it connects.
```bash ```bash
@ -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... And you'll get an output like this...
```text ```text
; <<>> DiG 9.14.1 <<>> mysql ; <<>> DiG 9.18.8 <<>> mysql
;; global options: +cmd ;; global options: +cmd
;; Got answer: ;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 32162 ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 32162
@ -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 !!! warning Setting Connection Settings via Env Vars
While using env vars to set connection settings is generally ok for development, it is **HIGHLY DISCOURAGED** 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/) [wrote a fantastic blog post](https://diogomonica.com/2017/03/27/why-you-shouldnt-use-env-variables-for-secret-data/)
explaining why. explaining why.
@ -202,24 +188,10 @@ With all of that explained, let's start our dev-ready container!
-e MYSQL_USER=root \ -e MYSQL_USER=root \
-e MYSQL_PASSWORD=secret \ -e MYSQL_PASSWORD=secret \
-e MYSQL_DB=todos \ -e MYSQL_DB=todos \
node:12-alpine \ node:18-alpine \
sh -c "yarn install && yarn run dev" 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. If you are using PowerShell then use this command.
```powershell hl_lines="3 4 5 6 7" ```powershell hl_lines="3 4 5 6 7"
@ -230,7 +202,7 @@ With all of that explained, let's start our dev-ready container!
-e MYSQL_USER=root ` -e MYSQL_USER=root `
-e MYSQL_PASSWORD=secret ` -e MYSQL_PASSWORD=secret `
-e MYSQL_DB=todos ` -e MYSQL_DB=todos `
node:12-alpine ` node:18-alpine `
sh -c "yarn install && yarn run dev" sh -c "yarn install && yarn run dev"
``` ```
@ -240,9 +212,10 @@ With all of that explained, let's start our dev-ready container!
```plaintext hl_lines="7" ```plaintext hl_lines="7"
# Previous log messages omitted # Previous log messages omitted
$ nodemon src/index.js $ nodemon src/index.js
[nodemon] 1.19.2 [nodemon] 2.0.20
[nodemon] to restart at any time, enter `rs` [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` [nodemon] starting `node src/index.js`
Connected to mysql db at host mysql Connected to mysql db at host mysql
Listening on port 3000 Listening on port 3000

Binary file not shown.

Before

Width:  |  Height:  |  Size: 113 KiB

After

Width:  |  Height:  |  Size: 244 KiB

View File

@ -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. 1. Create a file named `Dockerfile` in the same folder as the file `package.json` with the following contents.
```dockerfile ```dockerfile
FROM node:12-alpine FROM node:18-alpine
# Adding build tools to make yarn install work on Apple silicon / arm64 machines
RUN apk add --no-cache python2 g++ make
WORKDIR /app WORKDIR /app
COPY . . COPY . .
RUN yarn install --production RUN yarn install --production
@ -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 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 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. 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 After the image was downloaded, we copied in our application and used `yarn` to

Binary file not shown.

Before

Width:  |  Height:  |  Size: 166 KiB

After

Width:  |  Height:  |  Size: 308 KiB

View File

@ -24,7 +24,7 @@ What you'll see is that the files created in one container aren't available in a
commands (why we have the `&&`). The first portion picks a single random number and writes commands (why we have the `&&`). The first portion picks a single random number and writes
it to `/data.txt`. The second command is simply watching a file to keep the container running. it to `/data.txt`. The second command is simply watching a file to keep the container running.
1. Validate we can see the output by `exec`'ing into the container. To do so, open the Dashboard and click the first action of the container that is running the `ubuntu` image. 1. Validate we can see the output by `exec`'ing into the container. To do so, open the Dashboard, find your Ubuntu container, click on the "triple dot" menu to get additional actions, and click on the "Open in terminal" menu item.
![Dashboard open CLI into ubuntu container](dashboard-open-cli-ubuntu.png){: style=width:75% } ![Dashboard open CLI into ubuntu container](dashboard-open-cli-ubuntu.png){: style=width:75% }
{: .text-center } {: .text-center }

View File

@ -36,7 +36,8 @@ an example command that you will need to run to push to this repo.
To fix this, we need to "tag" our existing image we've built to give it another name. To fix this, we need to "tag" our existing image we've built to give it another name.
1. Login to the Docker Hub using the command `docker login -u YOUR-USER-NAME`. 1. Login to Docker Hub by either clicking on the "Sign In" button in Docker Desktop or using the
command `docker login -u YOUR-USER-NAME`.
1. Use the `docker tag` command to give the `getting-started` image a new name. Be sure to swap out 1. Use the `docker tag` command to give the `getting-started` image a new name. Be sure to swap out
`YOUR-USER-NAME` with your Docker ID. `YOUR-USER-NAME` with your Docker ID.

Binary file not shown.

Before

Width:  |  Height:  |  Size: 117 KiB

After

Width:  |  Height:  |  Size: 278 KiB

View File

@ -47,7 +47,7 @@ So, let's do it!
```bash ```bash
docker run -dp 3000:3000 \ docker run -dp 3000:3000 \
-w /app -v "$(pwd):/app" \ -w /app -v "$(pwd):/app" \
node:12-alpine \ node:18-alpine \
sh -c "yarn install && yarn run dev" sh -c "yarn install && yarn run dev"
``` ```
@ -56,23 +56,14 @@ So, let's do it!
```powershell ```powershell
docker run -dp 3000:3000 ` docker run -dp 3000:3000 `
-w /app -v "$(pwd):/app" ` -w /app -v "$(pwd):/app" `
node:12-alpine ` node:18-alpine `
sh -c "yarn install && yarn run dev" sh -c "yarn install && yarn run dev"
``` ```
If you are using an Apple Silicon Mac or another ARM64 device then use this command.
```bash
docker run -dp 3000:3000 \
-w /app -v "$(pwd):/app" \
node:12-alpine \
sh -c "apk add --no-cache python2 g++ make && yarn install && yarn run dev"
```
- `-dp 3000:3000` - same as before. Run in detached (background) mode and create a port mapping - `-dp 3000:3000` - same as before. Run in detached (background) mode and create a port mapping
- `-w /app` - sets the container's present working directory where the command will run from - `-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 `getting-started/app` directory to the container's `/app` directory. Note: Docker requires absolute paths for binding mounts, so in this example we use `pwd` for printing the absolute path of the working directory, i.e. the `app` directory, instead of typing it manually - `-v "$(pwd):/app"` - bind mount (link) the host's present `getting-started/app` directory to the container's `/app` directory. Note: Docker requires absolute paths for binding mounts, so in this example we use `pwd` for printing the absolute path of the working directory, i.e. the `app` directory, instead of typing it manually
- `node:12-alpine` - the image to use. Note that this is the base image for our app from the Dockerfile - `node:18-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 - `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`, running `yarn install` to install _all_ dependencies and then running `yarn run dev`. If we look in the `package.json`,
we'll see that the `dev` script is starting `nodemon`. we'll see that the `dev` script is starting `nodemon`.
@ -82,9 +73,10 @@ So, let's do it!
```bash ```bash
docker logs -f <container-id> docker logs -f <container-id>
$ nodemon src/index.js $ nodemon src/index.js
[nodemon] 1.19.2 [nodemon] 2.0.20
[nodemon] to restart at any time, enter `rs` [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` [nodemon] starting `node src/index.js`
Using sqlite database at /etc/todos/todo.db Using sqlite database at /etc/todos/todo.db
Listening on port 3000 Listening on port 3000

Binary file not shown.

Before

Width:  |  Height:  |  Size: 105 KiB

After

Width:  |  Height:  |  Size: 238 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 126 KiB

After

Width:  |  Height:  |  Size: 282 KiB

View File

@ -12,35 +12,18 @@ So, how do we get started?
## Installing Docker Compose ## Installing Docker Compose
If you installed Docker Desktop/Toolbox for either Windows or Mac, you already have Docker Compose! If you installed Docker Desktop for Windows, Mac, or Linux you already have Docker Compose!
Play-with-Docker instances already have Docker Compose installed as well. If you are on Play-with-Docker instances already have Docker Compose installed as well. If you are on
a Linux machine, you will need to install Docker Compose using another system, you can install Docker Compose using [the instructions here](https://docs.docker.com/compose/install/).
[the instructions here](https://docs.docker.com/compose/install/).
After installation, you should be able to run the following and see version information.
```bash
docker-compose version
```
## Creating our Compose File ## Creating our Compose File
1. At the root of the app project, create a file named `docker-compose.yml`. 1. At the root of the app project, create a file named `docker-compose.yml`.
1. In the compose file, we'll start off by defining the schema version. In most cases, it's best to use 1. In the compose file, we'll start off by defining a list of services (or containers) we want to run as part of our application.
the latest supported version. You can look at the [Compose file reference](https://docs.docker.com/compose/compose-file/compose-file-v3/)
for the current schema versions and the compatibility matrix.
```yaml ```yaml
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.8"
services: services:
``` ```
@ -59,74 +42,52 @@ docker run -dp 3000:3000 \
-e MYSQL_USER=root \ -e MYSQL_USER=root \
-e MYSQL_PASSWORD=secret \ -e MYSQL_PASSWORD=secret \
-e MYSQL_DB=todos \ -e MYSQL_DB=todos \
node:12-alpine \ node:18-alpine \
sh -c "yarn install && yarn run dev"
```
If you are using PowerShell then use this command.
```powershell
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 "yarn install && yarn run dev" sh -c "yarn install && yarn run dev"
``` ```
1. First, let's define the service entry and the image for the container. We can pick any name for the service. 1. First, let's define the service entry and the image for the container. We can pick any name for the service.
The name will automatically become a network alias, which will be useful when defining our MySQL service. The name will automatically become a network alias, which will be useful when defining our MySQL service.
```yaml hl_lines="4 5" ```yaml hl_lines="2 3"
version: "3.8"
services: services:
app: app:
image: node:12-alpine image: node:18-alpine
``` ```
1. Typically, you will see the command close to the `image` definition, although there is no requirement on ordering. 1. Typically, you will see the command close to the `image` definition, although there is no requirement on ordering.
So, let's go ahead and move that into our file. So, let's go ahead and move that into our file.
```yaml hl_lines="6" ```yaml hl_lines="4"
version: "3.8"
services: services:
app: app:
image: node:12-alpine image: node:18-alpine
command: sh -c "yarn install && yarn run dev" command: sh -c "yarn install && yarn run dev"
``` ```
1. Let's migrate the `-p 3000:3000` part of the command by defining the `ports` for the service. We will use the 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/compose-file-v3/#short-syntax-1) here, but there is also a more verbose [short syntax](https://docs.docker.com/compose/compose-file/#short-syntax-2) 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. [long syntax](https://docs.docker.com/compose/compose-file/#long-syntax-2) available as well.
```yaml hl_lines="7 8"
version: "3.8"
```yaml hl_lines="5 6"
services: services:
app: app:
image: node:12-alpine image: node:18-alpine
command: sh -c "yarn install && yarn run dev" command: sh -c "yarn install && yarn run dev"
ports: ports:
- 3000:3000 - 3000:3000
``` ```
1. Next, we'll migrate both the working directory (`-w /app`) and the volume mapping (`-v "$(pwd):/app"`) by using 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/compose-file-v3/#short-syntax-3) and [long](https://docs.docker.com/compose/compose-file/compose-file-v3/#long-syntax-3) syntax. the `working_dir` and `volumes` definitions. Volumes also has a [short](https://docs.docker.com/compose/compose-file/#short-syntax-4) and [long](https://docs.docker.com/compose/compose-file/#long-syntax-4) syntax.
One advantage of Docker Compose volume definitions is we can use relative paths from the current directory. One advantage of Docker Compose volume definitions is we can use relative paths from the current directory.
```yaml hl_lines="9 10 11" ```yaml hl_lines="7 8 9"
version: "3.8"
services: services:
app: app:
image: node:12-alpine image: node:18-alpine
command: sh -c "yarn install && yarn run dev" command: sh -c "yarn install && yarn run dev"
ports: ports:
- 3000:3000 - 3000:3000
@ -137,12 +98,10 @@ docker run -dp 3000:3000 `
1. Finally, we need to migrate the environment variable definitions using the `environment` key. 1. Finally, we need to migrate the environment variable definitions using the `environment` key.
```yaml hl_lines="12 13 14 15 16" ```yaml hl_lines="10 11 12 13 14"
version: "3.8"
services: services:
app: app:
image: node:12-alpine image: node:18-alpine
command: sh -c "yarn install && yarn run dev" command: sh -c "yarn install && yarn run dev"
ports: ports:
- 3000:3000 - 3000:3000
@ -167,46 +126,31 @@ docker run -d \
-v todo-mysql-data:/var/lib/mysql \ -v todo-mysql-data:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=secret \ -e MYSQL_ROOT_PASSWORD=secret \
-e MYSQL_DATABASE=todos \ -e MYSQL_DATABASE=todos \
mysql:5.7 mysql:8.0
```
If you are using PowerShell then use this command.
```powershell
docker run -d `
--network todo-app --network-alias mysql `
-v todo-mysql-data:/var/lib/mysql `
-e MYSQL_ROOT_PASSWORD=secret `
-e MYSQL_DATABASE=todos `
mysql:5.7
``` ```
1. We will first define the new service and name it `mysql` so it automatically gets the network alias. We'll 1. We will first define the new service and name it `mysql` so it automatically gets the network alias. We'll
go ahead and specify the image to use as well. go ahead and specify the image to use as well.
```yaml hl_lines="6 7" ```yaml hl_lines="4 5"
version: "3.8"
services: services:
app: app:
# The app service definition # The app service definition
mysql: mysql:
image: mysql:5.7 image: mysql:8.0
``` ```
1. Next, we'll define the volume mapping. When we ran the container with `docker run`, the named volume was created 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 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, `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/compose-file-v3/#volume-configuration-reference) though. the default options are used. There are [many more options available](https://docs.docker.com/compose/compose-file/#volumes-top-level-element) though.
```yaml hl_lines="8 9 10 11 12"
version: "3.8"
```yaml hl_lines="6 7 8 9 10"
services: services:
app: app:
# The app service definition # The app service definition
mysql: mysql:
image: mysql:5.7 image: mysql:8.0
volumes: volumes:
- todo-mysql-data:/var/lib/mysql - todo-mysql-data:/var/lib/mysql
@ -216,14 +160,12 @@ docker run -d `
1. Finally, we only need to specify the environment variables. 1. Finally, we only need to specify the environment variables.
```yaml hl_lines="10 11 12" ```yaml hl_lines="8 9 10"
version: "3.8"
services: services:
app: app:
# The app service definition # The app service definition
mysql: mysql:
image: mysql:5.7 image: mysql:8.0
volumes: volumes:
- todo-mysql-data:/var/lib/mysql - todo-mysql-data:/var/lib/mysql
environment: environment:
@ -238,11 +180,9 @@ At this point, our complete `docker-compose.yml` should look like this:
```yaml ```yaml
version: "3.8"
services: services:
app: app:
image: node:12-alpine image: node:18-alpine
command: sh -c "yarn install && yarn run dev" command: sh -c "yarn install && yarn run dev"
ports: ports:
- 3000:3000 - 3000:3000
@ -256,7 +196,7 @@ services:
MYSQL_DB: todos MYSQL_DB: todos
mysql: mysql:
image: mysql:5.7 image: mysql:8.0
volumes: volumes:
- todo-mysql-data:/var/lib/mysql - todo-mysql-data:/var/lib/mysql
environment: environment:
@ -274,41 +214,40 @@ Now that we have our `docker-compose.yml` file, we can start it up!
1. Make sure no other copies of the app/db are running first (`docker ps` and `docker rm -f <ids>`). 1. Make sure no other copies of the app/db are running first (`docker ps` and `docker rm -f <ids>`).
1. Start up the application stack using the `docker-compose up` command. We'll add the `-d` flag to run everything in the 1. Start up the application stack using the `docker compose up` command. We'll add the `-d` flag to run everything in the
background. background.
```bash ```bash
docker-compose up -d docker compose up -d
``` ```
When we run this, we should see output like this: When we run this, we should see output like this:
```plaintext ```plaintext
Creating network "app_default" with the default driver [+] Running 3/3
Creating volume "app_todo-mysql-data" with default driver ⠿ Network app_default Created 0.0s
Creating app_app_1 ... done ⠿ Container app-mysql-1 Started 0.4s
Creating app_mysql_1 ... done ⠿ Container app-app-1 Started 0.4s
``` ```
You'll notice that the volume was created as well as a network! By default, Docker Compose automatically creates a You'll notice that the volume was created as well as a network! By default, Docker Compose automatically creates a
network specifically for the application stack (which is why we didn't define one in the compose file). network specifically for the application stack (which is why we didn't define one in the compose file).
1. Let's look at the logs using the `docker-compose logs -f` command. You'll see the logs from each of the services interleaved 1. Let's look at the logs using the `docker compose logs -f` command. You'll see the logs from each of the services interleaved
into a single stream. This is incredibly useful when you want to watch for timing-related issues. The `-f` flag "follows" the into a single stream. This is incredibly useful when you want to watch for timing-related issues. The `-f` flag "follows" the
log, so will give you live output as it's generated. log, so will give you live output as it's generated.
If you don't already, you'll see output that looks like this... If you don't already, you'll see output that looks like this...
```plaintext ```plaintext
mysql_1 | 2019-10-03T03:07:16.083639Z 0 [Note] mysqld: ready for connections. mysql_1 | 2022-11-23T04:01:20.185015Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.31' socket: '/var/run/mysqld/mysqld.sock' port: 3306 MySQL Community Server - GPL.
mysql_1 | Version: '5.7.27' socket: '/var/run/mysqld/mysqld.sock' port: 3306 MySQL Community Server (GPL)
app_1 | Connected to mysql db at host mysql app_1 | Connected to mysql db at host mysql
app_1 | Listening on port 3000 app_1 | Listening on port 3000
``` ```
The service name is displayed at the beginning of the line (often colored) to help distinguish messages. If you want to The service name is displayed at the beginning of the line (often colored) to help distinguish messages. If you want to
view the logs for a specific service, you can add the service name to the end of the logs command (for example, view the logs for a specific service, you can add the service name to the end of the logs command (for example,
`docker-compose logs -f app`). `docker compose logs -f app`).
!!! info "Pro tip - Waiting for the DB before starting the app" !!! info "Pro tip - Waiting for the DB before starting the app"
When the app is starting up, it actually sits and waits for MySQL to be up and ready before trying to connect to it. When the app is starting up, it actually sits and waits for MySQL to be up and ready before trying to connect to it.
@ -335,16 +274,16 @@ quickly see what container is our app and which container is the mysql database.
## Tearing it All Down ## Tearing it All Down
When you're ready to tear it all down, simply run `docker-compose down` or hit the trash can on the Docker Dashboard When you're ready to tear it all down, simply run `docker compose down` or hit the trash can on the Docker Dashboard
for the entire app. The containers will stop and the network will be removed. for the entire app. The containers will stop and the network will be removed.
!!! warning "Removing Volumes" !!! warning "Removing Volumes"
By default, named volumes in your compose file are NOT removed when running `docker-compose down`. If you want to By default, named volumes in your compose file are NOT removed when running `docker compose down`. If you want to
remove the volumes, you will need to add the `--volumes` flag. remove the volumes, you will need to add the `--volumes` flag.
The Docker Dashboard does _not_ remove volumes when you delete the app stack. The Docker Dashboard does _not_ remove volumes when you delete the app stack.
Once torn down, you can switch to another project, run `docker-compose up` and be ready to contribute to that project! It really Once torn down, you can switch to another project, run `docker compose up` and be ready to contribute to that project! It really
doesn't get much simpler than that! doesn't get much simpler than that!

View File

@ -5,7 +5,7 @@ We're not going to go deep-dive here, but here are a few other areas to look at
## Container Orchestration ## Container Orchestration
Running containers in production is tough. You don't want to log into a machine and simply run a Running containers in production is tough. You don't want to log into a machine and simply run a
`docker run` or `docker-compose up`. Why not? Well, what happens if the containers die? How do you `docker run` or `docker compose up`. Why not? Well, what happens if the containers die? How do you
scale across several machines? Container orchestration solves this problem. Tools like Kubernetes, scale across several machines? Container orchestration solves this problem. Tools like Kubernetes,
Swarm, Nomad, and ECS all help solve this problem, all in slightly different ways. Swarm, Nomad, and ECS all help solve this problem, all in slightly different ways.

View File

@ -9,7 +9,7 @@ repo_url: https://github.com/docker/getting-started
edit_uri: "" edit_uri: ""
# Copyright # Copyright
copyright: 'Copyright &copy; 2020 Docker' copyright: 'Copyright &copy; 2020-2022 Docker'
# Configuration # Configuration
theme: theme: