mirror of https://github.com/docker/docs.git
update get started app
This commit is contained in:
parent
ba1ecb30b3
commit
fdc72f8a89
|
|
@ -77,9 +77,7 @@ In order to build the [container image](../get-started/overview.md/#docker-objec
|
||||||
2. Using a text editor or code editor, add the following contents to the Dockerfile:
|
2. Using a text editor or code editor, add the following contents to the Dockerfile:
|
||||||
|
|
||||||
```dockerfile
|
```dockerfile
|
||||||
# syntax=docker/dockerfile:1
|
FROM node:18-alpine
|
||||||
FROM node:12-alpine
|
|
||||||
RUN apk add --no-cache python2 g++ make
|
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
COPY . .
|
COPY . .
|
||||||
RUN yarn install --production
|
RUN yarn install --production
|
||||||
|
|
|
||||||
|
|
@ -47,32 +47,23 @@ So, let's do it!
|
||||||
```console
|
```console
|
||||||
$ 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 Windows, then use the following command in PowerShell.
|
If you are using Windows, then use the following command in PowerShell.
|
||||||
|
|
||||||
```powershell
|
```powershell
|
||||||
PS> 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 the following command.
|
|
||||||
|
|
||||||
```console
|
|
||||||
$ 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 "working directory" or the current directory that the command will run from
|
- `-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 into the `/app` directory in the container
|
- `-v "$(pwd):/app"` - bind mount the current directory from the host into the `/app` directory in the container
|
||||||
- `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,7 +73,7 @@ So, let's do it!
|
||||||
```console
|
```console
|
||||||
$ 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 dir(s): *.*
|
||||||
[nodemon] starting `node src/index.js`
|
[nodemon] starting `node src/index.js`
|
||||||
|
|
|
||||||
|
|
@ -53,30 +53,18 @@ 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 an ARM based chip, e.g. Macbook M1 Chips / Apple Silicon, then use this command.
|
|
||||||
|
|
||||||
```console
|
|
||||||
$ 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
|
|
||||||
```
|
```
|
||||||
|
|
||||||
If you are using Windows then use this command in PowerShell.
|
If you are using Windows then use this command in PowerShell.
|
||||||
|
|
||||||
```powershell
|
```powershell
|
||||||
PS> docker run -d `
|
$ docker run -d `
|
||||||
--network todo-app --network-alias mysql `
|
--network todo-app --network-alias mysql `
|
||||||
-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.
|
||||||
|
|
@ -145,7 +133,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
|
||||||
|
|
@ -183,7 +171,7 @@ The todo app supports the setting of a few environment variables to specify MySQ
|
||||||
> **Setting Connection Settings via Env Vars**
|
> **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/){:target="_blank" rel="noopener" class="_"}
|
> [wrote a fantastic blog post](https://diogomonica.com/2017/03/27/why-you-shouldnt-use-env-variables-for-secret-data/){:target="_blank" rel="noopener" class="_"}
|
||||||
> explaining why.
|
> explaining why.
|
||||||
>
|
>
|
||||||
|
|
@ -212,34 +200,21 @@ 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 are using an ARM based chip, e.g. Macbook M1 Chips / Apple Silicon, then use this command.
|
|
||||||
|
|
||||||
```console
|
|
||||||
$ 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 add --no-cache python2 g++ make && yarn install && yarn run dev"
|
|
||||||
```
|
|
||||||
|
|
||||||
If you are using Windows then use this command in PowerShell.
|
If you are using Windows then use this command in PowerShell.
|
||||||
|
|
||||||
```powershell
|
```powershell
|
||||||
PS> docker run -dp 3000:3000 `
|
$ docker run -dp 3000:3000 `
|
||||||
-w /app -v "$(pwd):/app" `
|
-w /app -v "$(pwd):/app" `
|
||||||
--network todo-app `
|
--network todo-app `
|
||||||
-e MYSQL_HOST=mysql `
|
-e MYSQL_HOST=mysql `
|
||||||
-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"
|
||||||
```
|
```
|
||||||
3. If we look at the logs for the container (`docker logs <container-id>`), we should see a message indicating it's
|
3. If we look at the logs for the container (`docker logs <container-id>`), we should see a message indicating it's
|
||||||
|
|
@ -247,7 +222,7 @@ With all of that explained, let's start our dev-ready container!
|
||||||
|
|
||||||
```console
|
```console
|
||||||
$ 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 dir(s): *.*
|
||||||
[nodemon] starting `node src/index.js`
|
[nodemon] starting `node src/index.js`
|
||||||
|
|
|
||||||
|
|
@ -31,19 +31,9 @@ $ docker compose version
|
||||||
|
|
||||||
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`.
|
||||||
|
|
||||||
2. In the compose file, we'll start off by defining the schema version. In most cases, it's best to use
|
2. In the compose file, we'll start off by defining the 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](../compose/compose-file/index.md)
|
|
||||||
for the current schema versions and the compatibility matrix.
|
|
||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
version: "3.7"
|
|
||||||
```
|
|
||||||
|
|
||||||
3. Next, we'll define the list of services (or containers) we want to run as part of our application.
|
|
||||||
|
|
||||||
```yaml
|
|
||||||
version: "3.7"
|
|
||||||
|
|
||||||
services:
|
services:
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
@ -61,21 +51,7 @@ $ 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
|
|
||||||
PS> 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"
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
@ -83,22 +59,19 @@ PS> docker run -dp 3000:3000 `
|
||||||
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
|
```yaml
|
||||||
version: "3.7"
|
|
||||||
|
|
||||||
services:
|
services:
|
||||||
app:
|
app:
|
||||||
image: node:12-alpine
|
image: node:18-alpine
|
||||||
```
|
```
|
||||||
|
|
||||||
2. Typically, you will see the `command` close to the `image` definition, although there is no requirement on ordering.
|
2. 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
|
```yaml
|
||||||
version: "3.7"
|
|
||||||
|
|
||||||
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"
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
@ -108,11 +81,9 @@ PS> docker run -dp 3000:3000 `
|
||||||
[long syntax](../compose/compose-file/compose-file-v3.md#long-syntax-1) available as well.
|
[long syntax](../compose/compose-file/compose-file-v3.md#long-syntax-1) available as well.
|
||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
version: "3.7"
|
|
||||||
|
|
||||||
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
|
||||||
|
|
@ -124,11 +95,9 @@ PS> docker run -dp 3000:3000 `
|
||||||
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
|
```yaml
|
||||||
version: "3.7"
|
|
||||||
|
|
||||||
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
|
||||||
|
|
@ -140,11 +109,9 @@ PS> docker run -dp 3000:3000 `
|
||||||
5. Finally, we need to migrate the environment variable definitions using the `environment` key.
|
5. Finally, we need to migrate the environment variable definitions using the `environment` key.
|
||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
version: "3.7"
|
|
||||||
|
|
||||||
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
|
||||||
|
|
@ -168,31 +135,19 @@ $ 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
|
|
||||||
PS> 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
|
```yaml
|
||||||
version: "3.7"
|
|
||||||
|
|
||||||
services:
|
services:
|
||||||
app:
|
app:
|
||||||
# The app service definition
|
# The app service definition
|
||||||
mysql:
|
mysql:
|
||||||
image: mysql:5.7
|
image: mysql:8.0
|
||||||
```
|
```
|
||||||
|
|
||||||
2. Next, we'll define the volume mapping. When we ran the container with `docker run`, the named volume was created
|
2. Next, we'll define the volume mapping. When we ran the container with `docker run`, the named volume was created
|
||||||
|
|
@ -201,13 +156,11 @@ PS> docker run -d `
|
||||||
the default options are used. There are [many more options available](../compose/compose-file/compose-file-v3.md#volume-configuration-reference) though.
|
the default options are used. There are [many more options available](../compose/compose-file/compose-file-v3.md#volume-configuration-reference) though.
|
||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
version: "3.7"
|
|
||||||
|
|
||||||
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
|
||||||
|
|
||||||
|
|
@ -218,13 +171,11 @@ PS> docker run -d `
|
||||||
3. Finally, we only need to specify the environment variables.
|
3. Finally, we only need to specify the environment variables.
|
||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
version: "3.7"
|
|
||||||
|
|
||||||
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:
|
||||||
|
|
@ -239,11 +190,10 @@ At this point, our complete `docker-compose.yml` should look like this:
|
||||||
|
|
||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
version: "3.7"
|
|
||||||
|
|
||||||
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
|
||||||
|
|
@ -257,7 +207,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:
|
||||||
|
|
@ -300,7 +250,7 @@ Now that we have our `docker-compose.yml` file, we can start it up!
|
||||||
|
|
||||||
```plaintext
|
```plaintext
|
||||||
mysql_1 | 2019-10-03T03:07:16.083639Z 0 [Note] mysqld: ready for connections.
|
mysql_1 | 2019-10-03T03:07:16.083639Z 0 [Note] mysqld: ready for connections.
|
||||||
mysql_1 | Version: '5.7.27' socket: '/var/run/mysqld/mysqld.sock' port: 3306 MySQL Community Server (GPL)
|
mysql_1 | Version: '8.0.31' 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
|
||||||
```
|
```
|
||||||
|
|
|
||||||
|
|
@ -105,9 +105,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
|
||||||
# syntax=docker/dockerfile:1
|
FROM node:18-alpine
|
||||||
FROM node:12-alpine
|
|
||||||
RUN apk add --no-cache python2 g++ make
|
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
COPY . .
|
COPY . .
|
||||||
RUN yarn install --production
|
RUN yarn install --production
|
||||||
|
|
@ -126,9 +124,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
|
```dockerfile
|
||||||
# syntax=docker/dockerfile:1
|
FROM node:18-alpine
|
||||||
FROM node:12-alpine
|
|
||||||
RUN apk add --no-cache python2 g++ make
|
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
COPY package.json yarn.lock ./
|
COPY package.json yarn.lock ./
|
||||||
RUN yarn install --production
|
RUN yarn install --production
|
||||||
|
|
@ -160,34 +156,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
|
||||||
---> b0dc3a5e5e9e
|
=> => transferring dockerfile: 175B
|
||||||
Step 2/6 : WORKDIR /app
|
=> [internal] load .dockerignore
|
||||||
---> Using cache
|
=> => transferring context: 2B
|
||||||
---> 9577ae713121
|
=> [internal] load metadata for docker.io/library/node:18-alpine
|
||||||
Step 3/6 : COPY package.json yarn.lock ./
|
=> [internal] load build context
|
||||||
---> bd5306f49fc8
|
=> => transferring context: 53.37MB
|
||||||
Step 4/6 : RUN yarn install --production
|
=> [1/5] FROM docker.io/library/node:18-alpine
|
||||||
---> Running in d53a06c9e4c2
|
=> CACHED [2/5] WORKDIR /app
|
||||||
yarn install v1.17.3
|
=> [3/5] COPY package.json yarn.lock ./
|
||||||
[1/4] Resolving packages...
|
=> [4/5] RUN yarn install --production
|
||||||
[2/4] Fetching packages...
|
=> [5/5] COPY . .
|
||||||
info fsevents@1.2.9: The platform "linux" is incompatible with this module.
|
=> exporting to image
|
||||||
info "fsevents@1.2.9" is an optional dependency and failed compatibility check. Excluding it from installation.
|
=> => exporting layers
|
||||||
[3/4] Linking dependencies...
|
=> => writing image sha256:d6f819013566c54c50124ed94d5e66c452325327217f4f04399b45f94e37d25
|
||||||
[4/4] Building fresh packages...
|
=> => naming to docker.io/library/getting-started
|
||||||
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.
|
||||||
|
|
@ -197,30 +182,26 @@ a change to the `package.json`. Make sense?
|
||||||
5. Build the Docker image now using `docker build -t getting-started .` again. This time, your output should look a little different.
|
5. Build the Docker image now using `docker build -t getting-started .` again. This time, your output should look a little different.
|
||||||
|
|
||||||
```plaintext
|
```plaintext
|
||||||
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
|
||||||
---> b0dc3a5e5e9e
|
=> => transferring dockerfile: 37B
|
||||||
Step 2/6 : WORKDIR /app
|
=> [internal] load .dockerignore
|
||||||
---> Using cache
|
=> => transferring context: 2B
|
||||||
---> 9577ae713121
|
=> [internal] load metadata for docker.io/library/node:18-alpine
|
||||||
Step 3/6 : COPY package.json yarn.lock ./
|
=> [internal] load build context
|
||||||
---> Using cache
|
=> => transferring context: 450.43kB
|
||||||
---> bd5306f49fc8
|
=> [1/5] FROM docker.io/library/node:18-alpine
|
||||||
Step 4/6 : RUN yarn install --production
|
=> CACHED [2/5] WORKDIR /app
|
||||||
---> Using cache
|
=> CACHED [3/5] COPY package.json yarn.lock ./
|
||||||
---> 4e68fbc2d704
|
=> CACHED [4/5] RUN yarn install --production
|
||||||
Step 5/6 : COPY . .
|
=> [5/5] COPY . .
|
||||||
---> cccde25a3d9a
|
=> exporting to image
|
||||||
Step 6/6 : CMD ["node", "src/index.js"]
|
=> => exporting layers
|
||||||
---> Running in 2be75662c150
|
=> => writing image sha256:91790c87bcb096a83c2bd4eb512bc8b134c757cda0bdee4038187f98148e2eda
|
||||||
Removing intermediate container 2be75662c150
|
=> => naming to docker.io/library/getting-started
|
||||||
---> 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 previously cached layers. So, hooray! We're using the build cache. Pushing and pulling this image and updates to it
|
||||||
`Using cache`. 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!
|
||||||
|
|
||||||
## Multi-stage builds
|
## Multi-stage builds
|
||||||
|
|
@ -238,7 +219,6 @@ that JDK isn't needed in production. Also, you might be using tools like Maven o
|
||||||
Those also aren't needed in our final image. Multi-stage builds help.
|
Those also aren't needed in our final image. Multi-stage builds help.
|
||||||
|
|
||||||
```dockerfile
|
```dockerfile
|
||||||
# syntax=docker/dockerfile:1
|
|
||||||
FROM maven AS build
|
FROM maven AS build
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
COPY . .
|
COPY . .
|
||||||
|
|
@ -259,8 +239,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
|
||||||
# syntax=docker/dockerfile:1
|
FROM node:18 AS build
|
||||||
FROM node:12 AS build
|
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
COPY package* yarn.lock ./
|
COPY package* yarn.lock ./
|
||||||
RUN yarn install
|
RUN yarn install
|
||||||
|
|
@ -272,7 +251,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?
|
||||||
|
|
||||||
## Next steps
|
## Next steps
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue