Skip to content

Commit e81e127

Browse files
committed
update 02-docker-image
1 parent e7886c6 commit e81e127

39 files changed

Lines changed: 4819 additions & 2704 deletions

07-cloud/03-docker/01-docker-commands/README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,7 @@ Docker run `pull`s images automatically if it hasn't them. Let's remove an exist
8686
```bash
8787
docker image rm <IMAGE NAME>:<TAG>
8888
docker rmi <IMAGE NAME>:<TAG>
89-
docker image prune
9089
```
91-
> `prune`: Remove all dangling images, that is, all images with name equals <none>. Dangling images are not referenced by other images and are safe to delete.
9290

9391
# About Basefactor + Lemoncode
9492

07-cloud/03-docker/02-docker-image/Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
FROM node:20-alpine AS base
1+
FROM node:22-alpine AS base
22
RUN mkdir -p /usr/app
33
WORKDIR /usr/app
44

@@ -18,4 +18,4 @@ COPY ./server/index.js ./
1818
RUN npm ci --omit=dev
1919

2020
ENV PORT=8083
21-
CMD node index.js
21+
CMD ["node", "index.js"]

07-cloud/03-docker/02-docker-image/README.md

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ We can create our custom images. In this case, we will use [the node image](http
1717
_./Dockerfile_
1818

1919
```Docker
20-
FROM node:20-alpine
20+
FROM node:22-alpine
2121
```
2222

2323
> You can use [Docker VSCode extension](https://code.visualstudio.com/docs/containers/overview)
@@ -27,7 +27,7 @@ Let's create the path where we are going to copy our app:
2727
_./Dockerfile_
2828

2929
```diff
30-
FROM node:20-alpine
30+
FROM node:22-alpine
3131
+ RUN mkdir -p /usr/app
3232
+ WORKDIR /usr/app
3333

@@ -58,7 +58,7 @@ Copy all files:
5858
_./Dockerfile_
5959

6060
```diff
61-
FROM node:20-alpine
61+
FROM node:22-alpine
6262
RUN mkdir -p /usr/app
6363
WORKDIR /usr/app
6464

@@ -71,7 +71,7 @@ Execute install and build:
7171
_./Dockerfile_
7272

7373
```diff
74-
FROM node:20-alpine
74+
FROM node:22-alpine
7575
RUN mkdir -p /usr/app
7676
WORKDIR /usr/app
7777

@@ -108,7 +108,7 @@ We can create some docker steps to install server and execute it:
108108
_./Dockerfile_
109109

110110
```diff
111-
FROM node:20-alpine
111+
FROM node:22-alpine
112112
RUN mkdir -p /usr/app
113113
WORKDIR /usr/app
114114

@@ -119,13 +119,13 @@ RUN npm run build
119119
+ RUN cp -r ./dist ./server/public
120120
+ RUN cd server && npm ci
121121

122-
+ CMD node server/index.js
122+
+ CMD ["node", "server/index.js"]
123123

124124
```
125125

126126
> RUN vs CMD: I don't want to run `node server` when we build the image, we want to run it when run the container.
127127
>
128-
> CMD VS ENTRYPOINT: https://docs.doppler.com/docs/dockerfile
128+
> [CMD VS ENTRYPOINT](https://codewithyury.com/docker-run-vs-cmd-vs-entrypoint/)
129129
130130
Run the container:
131131

@@ -134,9 +134,9 @@ docker build -t my-app:1 .
134134
docker images
135135

136136
```
137-
> It creates a <none> image due to replace same tag.
137+
> In earlier versions of Docker when you re-use the same tag, it will create a new image with the same tag, but with a different ID. This is called a dangling image and you can remove it with `docker image prune`
138138
>
139-
> We can remove it with `docker image prune`
139+
> But in the latest versions of Docker, it will delete the old image for you.
140140
141141
Run new container:
142142

@@ -167,7 +167,7 @@ _./Dockerfile_
167167
...
168168

169169
+ ENV PORT=8083
170-
CMD node server/index.js
170+
CMD ["node", "server/index.js"]
171171

172172
```
173173

@@ -196,12 +196,10 @@ If we check `docker images` we can see dangling images, due to use same tags for
196196

197197
```bash
198198
docker images
199-
docker image prune
200-
docker images
201199

202200
```
203201

204-
On the other hand, we have an image with `~382MB`, too much size isn't it?. We should use [multi-stage builds](https://docs.docker.com/develop/develop-images/multistage-build/) to decrease this size, with only the necessary info:
202+
On the other hand, we have an image with `~592MB`, too much size isn't it?. We should use [multi-stage builds](https://docs.docker.com/develop/develop-images/multistage-build/) to decrease this size, with only the necessary info:
205203

206204
> Change container project structure:
207205
@@ -225,8 +223,8 @@ On the other hand, we have an image with `~382MB`, too much size isn't it?. We s
225223
_./Dockerfile_
226224

227225
```diff
228-
- FROM node:20-alpine
229-
+ FROM node:20-alpine AS base
226+
- FROM node:22-alpine
227+
+ FROM node:22-alpine AS base
230228
RUN mkdir -p /usr/app
231229
WORKDIR /usr/app
232230

@@ -247,12 +245,12 @@ RUN npm run build
247245
+ RUN npm ci --omit=dev
248246

249247
ENV PORT=8083
250-
- CMD node server/index.js
251-
+ CMD node index.js
248+
- CMD ["node", "server/index.js"]
249+
+ CMD ["node", "index.js"]
252250

253251
```
254252

255-
> We could use `npm ci` instead of `npm install` if we have a `package-lock.json` generated.
253+
> We can use `npm ci` instead of `npm install` because we have a `package-lock.json` generated.
256254
257255
Run it:
258256

@@ -280,6 +278,10 @@ const app = express();
280278
+ const staticFilesPath = path.resolve(__dirname, process.env.STATIC_FILES_PATH);
281279
app.use('/', express.static(staticFilesPath));
282280

281+
app.get('*', (req, res) => {
282+
res.sendFile(path.resolve(staticFilesPath, 'index.html'));
283+
});
284+
283285
const PORT = process.env.PORT || 8081;
284286
app.listen(PORT, () => {
285287
console.log(`App running on http://localhost:${PORT}`);

0 commit comments

Comments
 (0)