I deploying Vue app, and use method multi-stage ( with Nginx as server ) .
Structure files :
- docker-compose.yml
- client ( folder code )
- Dockerfile
This is files config :
docker-compose.yml
version: '3.4'
services:
client:
build:./client
ports:
- "8080:80"
volumes:
- "./client:/app"
- "/app/node_modules"
Dockerfile
FROM node:lts-alpine as build-stage
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build
# production stage
FROM nginx:stable-alpine as production-stage
COPY --from=build-stage /app/dist /usr/share/nginx/html
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
This run so smooth. But, when i changed content of source code and rebuild image with
-
docker-compose build client
docker-compose up –build
( or some arguments –no-cache, –force-recreate … event set name
for it )
It always will create new 2 image : 23.6MB and 333MB as below
docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
root_ad latest 236945fbfbb5 7 minutes ago 23.6MB
<none> <none> fbdc81e7983c 7 minutes ago 333MB
<none> <none> 7969e4ba9ecf 18 minutes ago 23.6MB
<none> <none> e31655da8c88 18 minutes ago 333MB
<none> <none> 4d5cae08a889 20 minutes ago 23.6MB
<none> <none> ee369d2602a9 20 minutes ago 333MB
nginx alpine 513f9a9d8748 3 weeks ago 22.9MB
node 15.0-alpine 1e8b781248bb 11 months ago 115MB
How can i rebuild image without duplicate its ?
2
Answers
when your using docker-compose, you don’t need to build the container separately,
just run,
because in your docker-compose.yml file you’re already specified to build the image using client Dockerfile,
build:./client
.then it will not create the same image again and again
23.6MB
‘snone
isproduction-stage
333MB
‘snone
isbuild-stage
That is intermediate temporary images. You may delete it.
Why that appear again and again?
Even if first part:
is always the same (Let’s say we don’t changing
package*.json
)The second part:
Always changing because you copying updated source code at every build.
Docker needs to create every time new intermediate image to be able run
from this intermediate
build-stage
imageAfter that docker creates new image
production-stage
(asnone
) and tags it asroot_ad:latest
.Why here is three
23.6MB
‘s images and only one isroot_ad:latest
?Because at every build docker untag older
root_ad:latest
and add this tag to newer23.6MB
‘snone
.You may check this behaviour by edit manually image version at every build:
After that you will see something like: