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/01/11 10:19] – [Docker Compose:] marcv | marc:linux:docker [2022/04/19 10:03] (current) – [2 phase container building:] marcv | ||
|---|---|---|---|
| Line 169: | Line 169: | ||
| # Define working directory inside container: | # Define working directory inside container: | ||
| - | WORKDIR /usr/app #will be created if non-existing! | + | WORKDIR /usr/app #will be created |
| ### Instructions | ### Instructions | ||
| Line 177: | Line 177: | ||
| RUN command2 | RUN command2 | ||
| - | COPY ./ ./ #copy everything from working directory (local) to working directory (container); | + | COPY . . |
| + | |||
| + | # OR | ||
| + | |||
| + | COPY $(PWD) . | ||
| + | |||
| + | # 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 | ||
| - | # 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 |
| # Map networkports to containerports: | # Map networkports to containerports: | ||
| Line 190: | 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 219: | 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 238: | Line 273: | ||
| ^ Command ^ Function ^ | ^ Command ^ Function ^ | ||
| | < | | < | ||
| - | | < | + | | < |
| + | | < | ||
| | < | | < | ||
| + | | < | ||
| === Handling crashing containers: === | === Handling crashing containers: === | ||
| Line 247: | Line 284: | ||
| Inside our // | Inside our // | ||
| ^ Restart Policies ^^ | ^ Restart Policies ^^ | ||
| - | | **no** | Never attempt to restart this container if it stops or crashes (default when undefined) | | + | | **"no"** | Never attempt to restart this container if it stops or crashes (default when undefined)Notice the quotes needed because of interpreatation by yaml! | |
| | **always** | If this container stops **for any reason** always attempt to restart it | | | **always** | If this container stops **for any reason** always attempt to restart it | | ||
| | **on-failure** | Only restart if the container stops with an error code (>0) | | | **on-failure** | Only restart if the container stops with an error code (>0) | | ||
| | ** unless-stopped** | Always restart unless we forcibly stop it | | | ** unless-stopped** | Always restart unless we forcibly stop it | | ||
| + | === Updating a docker-compose container: === | ||
| + | |||
| + | < | ||
| + | #!/bin/bash | ||
| + | docker-compose pull | ||
| + | docker-compose up -d --remove-orphans | ||
| + | yes | docker image prune | ||
| + | </ | ||
| + | |||
| + | ===== Github CI/CD: ===== | ||
| + | |||
| + | {{: | ||
| + | |||
| + | {{: | ||
| + | |||
| + | |||
| + | ===== 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.1641892762.txt.gz · Last modified: (external edit)
