100-Days-Of-DevOps-Challenge-KodeKloud

Resolve Dockerfile Issues

The Nautilus DevOps team is working to create new images per requirements shared by the development team. One of the team members is working to create a Dockerfile on App Server 1 in Stratos DC. While working on it she ran into issues in which the docker build is failing and displaying errors. Look into the issue and fix it to build an image as per details mentioned below:

Note: Please note that once you click on FINISH button all existing images, the containers will be destroyed and new image will be built from your Dockerfile.

Steps

  1. Let’s login into App server and find the Docker file

     cat /opt/docker/Dockerfile
     ``
    
     ```Dockerfile
    
     FROM httpd:2.4.43
    
     RUN sed -i "s/Listen 80/Listen 8080/g" /usr/local/apache2/conf/httpd.conf
    
     RUN sed -i '/LoadModule\ ssl_module modules\/mod_ssl.so/s/^#//g' conf/httpd.conf
    
     RUN sed -i '/LoadModule\ socache_shmcb_module modules\/mod_socache_shmcb.so/s/^#//g' conf/httpd.conf
    
     RUN sed -i '/Include\ conf\/extra\/httpd-ssl.conf/s/^#//g' conf/httpd.conf
    
     RUN cp certs/server.crt /usr/local/apache2/conf/server.crt
    
     RUN cp certs/server.key /usr/local/apache2/conf/server.key
    
     RUN cp html/index.html /usr/local/apache2/htdocs/
    
  2. Let’s try to Build Image

     docker build -t test:test .
    
     Dockerfile:1
     --------------------
     1 | >>> FROM httpd:2.4.43
     2 |     
     3 |     RUN sed -i "s/Listen 80/Listen 8080/g" /usr/local/apache2/conf/httpd.conf
     --------------------
     ERROR: failed to build: failed to solve: DeadlineExceeded: DeadlineExceeded: httpd:2.4.43: failed to resolve source metadata for docker.io/library/httpd:2.4.43: failed to copy: httpReadSeeker: failed open: failed to do request: Get "https://docker-registry-mirror.kodekloud.com/v2/library/httpd/manifests/sha256:53729354a74c9c146aa8726a8906e833755066ada1a478782f4dfb2ea6994b5d?ns=docker.io": dial tcp 10.0.0.6:443: i/o timeout
    

    It seems network timeout issue

  3. Let’s Pull the base image First

     docker pull httpd:2.4.43
    
     [tony@stapp01 docker]$ docker pull httpd:2.4.43
     2.4.43: Pulling from library/httpd
     bf5952930446: Pull complete 
     3d3fecf6569b: Pull complete 
     b5fc3125d912: Pull complete 
     3c61041685c0: Pull complete 
     34b7e9053f76: Pull complete 
     Digest: sha256:cd88fee4eab37f0d8cd04b06ef97285ca981c27b4d685f0321e65c5d4fd49357
     Status: Downloaded newer image for httpd:2.4.43
     docker.io/library/httpd:2.4.43
     [tony@stapp01 docker]$ 
    

    Image pulled successfully

  4. Let’s try to build again

     docker build -t test:test .
    
     [tony@stapp01 docker]$ docker build -t test:test .
     [+] Building 6.1s (9/11)                                                                          docker:default
     => [internal] load build definition from Dockerfile                                                        0.0s
     => => transferring dockerfile: 562B                                                                        0.0s
     => [internal] load metadata for docker.io/library/httpd:2.4.43                                             0.0s
     => [internal] load .dockerignore                                                                           0.0s
     => => transferring context: 2B                                                                             0.0s
     => [1/8] FROM docker.io/library/httpd:2.4.43                                                               0.1s
     => [2/8] RUN sed -i "s/Listen 80/Listen 8080/g" /usr/local/apache2/conf/httpd.conf                         1.2s
     => [3/8] RUN sed -i '/LoadModule\ ssl_module modules\/mod_ssl.so/s/^#//g' conf/httpd.conf                  1.1s
     => [4/8] RUN sed -i '/LoadModule\ socache_shmcb_module modules\/mod_socache_shmcb.so/s/^#//g' conf/httpd.  1.2s
     => [5/8] RUN sed -i '/Include\ conf\/extra\/httpd-ssl.conf/s/^#//g' conf/httpd.conf                        1.1s
     => ERROR [6/8] RUN cp certs/server.crt /usr/local/apache2/conf/server.crt                                  1.4s
     ------                                                                                                           
     > [6/8] RUN cp certs/server.crt /usr/local/apache2/conf/server.crt:
     1.390 cp: cannot stat 'certs/server.crt': No such file or directory
     ------
     Dockerfile:11
     --------------------
     9 |     RUN sed -i '/Include\ conf\/extra\/httpd-ssl.conf/s/^#//g' conf/httpd.conf
     10 |     
     11 | >>> RUN cp certs/server.crt /usr/local/apache2/conf/server.crt
     12 |     
     13 |     RUN cp certs/server.key /usr/local/apache2/conf/server.key
     --------------------
     ERROR: failed to build: failed to solve: process "/bin/sh -c cp certs/server.crt /usr/local/apache2/conf/server.crt" did not complete successfully: exit code: 1
    

    Now, we are getting certs file path issues. It’s because the Dockerfile is trying to copy files from relative paths, but those files need to be in the Docker build context. We need to use the COPY instruction instead of RUN cp with absolute paths.

     FROM httpd:2.4.43
    
     RUN sed -i "s/Listen 80/Listen 8080/g" /usr/local/apache2/conf/httpd.conf
    
     RUN sed -i '/LoadModule\ ssl_module modules\/mod_ssl.so/s/^#//g' conf/httpd.conf
    
     RUN sed -i '/LoadModule\ socache_shmcb_module modules\/mod_socache_shmcb.so/s/^#//g' conf/httpd.conf
    
     RUN sed -i '/Include\ conf\/extra\/httpd-ssl.conf/s/^#//g' conf/httpd.conf
    
     COPY certs/server.crt /usr/local/apache2/conf/server.crt
    
     COPY certs/server.key /usr/local/apache2/conf/server.key
    
     COPY html/index.html /usr/local/apache2/htdocs/
    
  5. We fixed Dockerfile, now let’s build again

     docker build -t test:test .
    
     [tony@stapp01 docker]$ docker build -t test:test .
     [+] Building 5.9s (13/13) FINISHED                                                                docker:default
     => [internal] load build definition from Dockerfile                                                        0.0s
     => => transferring dockerfile: 557B                                                                        0.0s
     => [internal] load metadata for docker.io/library/httpd:2.4.43                                             0.0s
     => [internal] load .dockerignore                                                                           0.0s
     => => transferring context: 2B                                                                             0.0s
     => [1/8] FROM docker.io/library/httpd:2.4.43                                                               0.0s
     => [internal] load build context                                                                           0.0s
     => => transferring context: 3.19kB                                                                         0.0s
     => CACHED [2/8] RUN sed -i "s/Listen 80/Listen 8080/g" /usr/local/apache2/conf/httpd.conf                  0.0s
     => CACHED [3/8] RUN sed -i '/LoadModule\ ssl_module modules\/mod_ssl.so/s/^#//g' conf/httpd.conf           0.0s
     => CACHED [4/8] RUN sed -i '/LoadModule\ socache_shmcb_module modules\/mod_socache_shmcb.so/s/^#//g' conf  0.0s
     => CACHED [5/8] RUN sed -i '/Include\ conf\/extra\/httpd-ssl.conf/s/^#//g' conf/httpd.conf                 0.0s
     => [6/8] COPY certs/server.crt /usr/local/apache2/conf/server.crt                                          2.0s
     => [7/8] COPY certs/server.key /usr/local/apache2/conf/server.key                                          0.7s
     => [8/8] COPY html/index.html /usr/local/apache2/htdocs/                                                   0.7s
     => exporting to image                                                                                      2.4s
     => => exporting layers                                                                                     2.4s
     => => writing image sha256:a3ff1cbe531b871bec1bfb9ef199a87782289f861b5b43a821870196a6f8b3e8                0.0s
     => => naming to docker.io/library/test:test  
    
  6. We can verify using this command

     docker images
    

    It will display all the images

Good to Know?

Dockerfile Troubleshooting

Common Dockerfile Issues

COPY vs RUN cp

Debugging Strategies