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/04/18 09:18] – [Additional Dockerfile instructions:] marcvmarc: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); defined as /usr/app!+# 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
 # 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 ["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 225: 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 274: Line 303:
  
 {{:marc:linux:github_workflow.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.1650266290.txt.gz · Last modified: (external edit)

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki