Docker Q A - q-uest/notes-doc-k8s-docker-jenkins-all-else GitHub Wiki

  1. What does docker "create" do? Under what circumstances do you use it?

    It creates a writeable container layer over the specified image and prepares it for running the specified command. This is useful as it helps to start the container when required without any delay. This is similar to docker run -d except the container is never started at this point. Use the docker container start (or shorthand: docker start) command to start the container at any point.

    docker container create -i -t --name mycontainer alpine
    docker container start --attach -i mycontainer
    

    The above is equivalent to the below one with "run" command -

    docker run -it --name mycontainer2 alpine
    

    Example2 (Initializing/Sharing Volumes between cotainers):

    Container volumes are initialized during the docker create phase (i.e., docker run too). For example, this allows you to create the data volume container, and then use it from another container:

    docker create -v /data --name data ubuntu
    

    Use the "data" volume from different containers:


## Create a file named, /data/f1 and list it from another container
##########

>> docker run -i --rm --volumes-from data ubuntu touch /data/f1
>> docker run -i --rm --volumes-from data ubuntu ls -l /data
  total 0
  -rw-r--r-- 1 root root 0 Apr 10 06:34 f1

** NEED TO FIND OUT A USE-CASE FOR THIS.**
  1. Can you access a volume of another container from a different container?

  2. What is Docker "init" or What is advantage of Tini??

    https://github.com/krallin/tini/issues/8

  3. What is multi-stage builds in Docker?

    With multi-stage builds, you use multiple FROM statements in your Dockerfile. Each FROM instruction can use a different base, and each of them begins a new stage of the build. You can selectively copy artifacts from one stage to another, leaving behind everything you don’t want in the final image.

    Example:

    Dockerfile (multi-stage build)

    # syntax=docker/dockerfile:1
    FROM golang:1.16
    WORKDIR /go/src/github.com/alexellis/href-counter/
    RUN go get -d -v golang.org/x/net/html  
    COPY app.go ./
    RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app .
    
    FROM alpine:latest  
    RUN apk --no-cache add ca-certificates
    WORKDIR /root/
    COPY --from=0 /go/src/github.com/alexellis/href-counter/app ./
    CMD ["./app"]  
    
    

    Note: By default, the stages are not named, and you refer to them by their integer number, starting with 0 for the first FROM instruction in the above. However, you can name your stages, by adding an AS to the FROM instruction

    Build image with the above Dockerfile

    docker build -t alexellis2/href-counter:latest .
    
    
  4. How do you name the stages in multi stage builds?

    # syntax=docker/dockerfile:1
    FROM golang:1.16 AS builder
    WORKDIR /go/src/github.com/alexellis/href-counter/
    RUN go get -d -v golang.org/x/net/html  
    COPY app.go    ./
    RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app .
    
    FROM alpine:latest  
    RUN apk --no-cache add ca-certificates
    WORKDIR /root/
    COPY --from=builder /go/src/github.com/alexellis/href-counter/app ./
    CMD ["./app"]  
    
    
  5. Would it be possible to use any image other than the ones generated with in the same Dockerfile (multi-stage builds)?

    COPY --from=nginx:latest /etc/nginx/nginx.conf /nginx.conf
    

    COPY --from instruction oould be used to copy from a separate image, either using the local image name, a tag available locally or on a Docker registry, or a tag ID. The Docker client pulls the image if necessary and copies the artifact from there.

  6. What were the steps you had to do before the days of multi-stage builds feature provided by Docker?

    Write a shell script to perform the below steps.

    1. Buile image via a Dockerfile with the required instructions to perform the below (refer, "Dockerfile.build" below) :

      a) Copy in the application code into the container which has the base image of the concerned application package tool (for e.g.maven or gradle).

      b) Compile/Build image out of the application code.

    2. Start an Intermediate container with the image built in step 1 & Copy only the necessary application image (should aim to keep it small/slim) to the host and remove the container.

    3. Build the application image

      a) Build an image from another Dockerfile (refer: Dockerfile below) which contains instructions to copy in the application image generated and available on the host into the container of the required base image to run the application.

      b) Remove the application image on the host.

      Example:

      build.sh

      
      #!/bin/sh
      #Building Application image (with the tag - build) per step 1 above (use Dockerfile.build)
      ###############
      
      docker build --build-arg https_proxy=$https_proxy --build-arg http_proxy=$http_proxy \  
      -t alexellis2/href-counter:build . -f Dockerfile.build
      
      # Create intermediate container and get only application image per step2 above
      ###################
      
      docker container create --name extract alexellis2/href-counter:build  
      docker container cp extract:/go/src/github.com/alexellis/href-counter/app ./app  
      docker container rm -f extract
      
      # Build application image with the tag latest, per step 3 above (using Dockerfile)
      #####################
      
      docker build --no-cache -t alexellis2/href-counter:latest .
      rm ./app
      
      

      Dockerfile.build

      # syntax=docker/dockerfile:1
      FROM golang:1.16
      WORKDIR /go/src/github.com/alexellis/href-counter/
      COPY app.go ./
      RUN go get -d -v golang.org/x/net/html \
        && CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o app .
      
      

      Dockerfile

      # syntax=docker/dockerfile:1
      FROM alpine:latest  
      RUN apk --no-cache add ca-certificates
      WORKDIR /root/
      COPY app ./
      CMD ["./app"]  
      
⚠️ **GitHub.com Fallback** ⚠️