marc:linux:docker
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| marc:linux:docker [2022/04/18 09:18] – [Additional Dockerfile instructions:] marcv | marc:linux:docker [2022/04/19 10:03] (current) – [2 phase container building:] marcv | ||
|---|---|---|---|
| Line 183: | Line 183: | ||
| COPY $(PWD) . | COPY $(PWD) . | ||
| - | #copy everything from working directory (local) to working directory (container); | + | # copy everything from working directory (local) to working directory (container); |
| # Because we chose to copy the non-changing files before the RUN commands, the docker build will execute faster as | # Because we chose to copy the non-changing files before the RUN commands, the docker build will execute faster as | ||
| # it does not need to include the RUN commands if we change other files | # it does not need to include the RUN commands if we change other files | ||
| Line 196: | Line 196: | ||
| CMD [" | CMD [" | ||
| </ | </ | ||
| + | |||
| + | We can also define another Dockerfile to use for building a container: | ||
| + | < | ||
| + | docker build -f Dockerfile.dev | ||
| + | </ | ||
| + | |||
| + | Another example of then starting the container: | ||
| + | < | ||
| + | docker run -p 3000:3000 -v / | ||
| + | </ | ||
| + | |||
| + | In this example: | ||
| + | * docker run: run container | ||
| + | * -p 3000:3000: map local port 3000 to containerport 3000 | ||
| + | * -v / | ||
| + | * -v $(pwd):/ | ||
| + | |||
| + | The last parameter (-v $(pwd):/app is basically a volume mapping! | ||
| + | |||
| ===== Docker Compose: ===== | ===== Docker Compose: ===== | ||
| Line 225: | Line 244: | ||
| ports: | ports: | ||
| - " | - " | ||
| + | volumes: | ||
| + | - / | ||
| + | - .:/ | ||
| + | # container (except / | ||
| + | app3: | ||
| + | restart: never | ||
| + | build: | ||
| + | context: . # working directory | ||
| + | dockerfile: ./ | ||
| + | # it can also be a url to a git_directory | ||
| </ | </ | ||
| Line 274: | Line 303: | ||
| {{: | {{: | ||
| + | |||
| + | |||
| + | ===== 2 phase container building: ===== | ||
| + | |||
| + | By using a multiphase build we can: | ||
| + | |||
| + | - Use multiple images | ||
| + | - Reduce space by only copying the needed contents to the final container | ||
| + | |||
| + | |||
| + | {{: | ||
| + | |||
| + | The __Dockerfile__ would look something like this: | ||
| + | |||
| + | < | ||
| + | FROM node:alpine as builder | ||
| + | WORKDIR '/ | ||
| + | COPY package.json . | ||
| + | RUN npm install | ||
| + | COPY . . | ||
| + | RUN npm run build # This creates the content we want to serve in the /app/build directory | ||
| + | |||
| + | FROM nginx # Not using another tag; we differentiate the phases using " | ||
| + | COPY --from=builder /app/build / | ||
| + | </ | ||
| + | |||
| + | Notice we did not define the RUN command; this is not needed as this container will start nginx itself. | ||
marc/linux/docker.1650266290.txt.gz · Last modified: (external edit)
