Site Tools


marc:linux:docker

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
marc:linux:docker [2022/02/03 07:49] – [Docker Compose:] marcvmarc: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 inside container(!) if non-existing!
  
 ### Instructions ### Instructions
Line 177: Line 177:
 RUN command2 RUN command2
  
-COPY ..#copy everything from working directory (local) to working directory (container); defined as /usr/app!+COPY . .  
 + 
 +# OR 
 + 
 +COPY $(PWD) . 
 + 
 +# copy everything from working directory (local) to working directory (container); defined as /usr/app!
 # 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: Only needed for incoming traffic! # Map networkports to containerports: Only needed for incoming traffic!
Line 190: Line 196:
 CMD ["prog1", "prog2"] CMD ["prog1", "prog2"]
 </code> </code>
 +
 +We can also define another Dockerfile to use for building a container:
 +<code>
 +docker build -f Dockerfile.dev
 +</code>
 +
 +Another example of then starting the container:
 +<code>
 +docker run  -p 3000:3000 -v /app/node_modules -v $(pwd):/app ContainerID
 +</code>
 +
 +In this example:
 +  * docker run: run container
 +  * -p 3000:3000: map local port 3000 to containerport 3000
 +  * -v /app/node_modules: the /app/node_modules directory in the container is "carved in stone"/ do not change
 +  * -v $(pwd):/app: copy all files from working dir inside container directory "/app" EXCEPT /app/node_modules!!!!
 +
 +The last parameter (-v $(pwd):/app is basically a volume mapping!
 +
  
 ===== Docker Compose: ===== ===== Docker Compose: =====
Line 219: Line 244:
     ports:                                       # ports to map     ports:                                       # ports to map
       - "8080:80"                                # map port 8080 on host to port 80 inside container       - "8080:80"                                # map port 8080 on host to port 80 inside container
 +    volumes:
 +      - /app/node_volumes                        # directory "/app/node/modules" inside(!) volume does not change!
 +      - .:/app                                   # copy all from local working directory to app directory inside
 +                                                 # container (except /app/node_volumes!)
 +    app3:
 +      restart: never
 +      build:
 +        context: .                               # working directory
 +        dockerfile: ./Dockerfile.dev             # define another file than default (Dockerfile)
 +                                                 # it can also be a url to a git_directory
 </code> </code>
  
Line 239: Line 274:
 | <code>docker-compose up -d</code> | start containers without rebuilding and detach | | <code>docker-compose up -d</code> | start containers without rebuilding and detach |
 | <code>docker-compose up --build -d</code> | start containers uncluding rebuilds and detach | | <code>docker-compose up --build -d</code> | start containers uncluding rebuilds and detach |
 +| <code>docker-compose up -d --force-recreate</code> | start containers and force recreates |
 | <code>docker-compose down</code> | stop and remove(!) containers | | <code>docker-compose down</code> | stop and remove(!) containers |
 | <code>docker-compose ps</code> | print status of running container; this will look for **docker-compose.yml** file to establish which containers you are querying about! | | <code>docker-compose ps</code> | print status of running container; this will look for **docker-compose.yml** file to establish which containers you are querying about! |
Line 253: Line 289:
 | ** unless-stopped** | Always restart unless we forcibly stop it | | ** unless-stopped** | Always restart unless we forcibly stop it |
  
 +=== Updating a docker-compose container: ===
 +
 +<code>
 +#!/bin/bash
 +docker-compose pull
 +docker-compose up -d --remove-orphans
 +yes | docker image prune
 +</code>
 +
 +===== Github CI/CD: =====
 +
 +{{:marc:linux:github_scheme.png?direct&800|}}
 +
 +{{:marc:linux:github_workflow.png?direct&800|}}
 +
 +
 +===== 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
 +
 +
 +{{:marc:linux:multi_phase_build.png?direct&800|}}
 +
 +The __Dockerfile__ would look something like this:
 +
 +<code>
 +FROM node:alpine as builder        # Using "as" we can tag this phase
 +WORKDIR '/app'
 +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 "FROM"
 +COPY --from=builder /app/build /usr/share/nginx/html
 +</code>
  
 +Notice we did not define the RUN command; this is not needed as this container will start nginx itself.
marc/linux/docker.1643870957.txt.gz · Last modified: (external edit)

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki