Neo
Neo

Reputation: 2405

docker file with run command not executing

I have a docker-compose file as below and an app.docker file for php. When I run this on my laptop everything works fine.

When I run this on my main PC the RUN command in the app.docker file does not run. I have to CLI onto the php instance and run it manually.

Any ideas as to why?

docker-compose.yml

version: '2'

services:
    nginx:
      image: nginx:1.13.12
      ports:
        - "8443:443"
        - "8080:80"
      volumes:
        - ./:/var/www
        - ./docker/nginxconf:/etc/nginx/conf.d
        - ./docker/ssl-cert:/etc/nginx/certs
      working_dir: /var/www
      links:
        - php
    php:
        build:
            context: ./
            dockerfile: docker/app.docker
        volumes:
            - ./:/var/www
        depends_on:
          - db
        links:
            - db
        environment:
            - "DB_PORT=3306"
            - "DB_HOST=db"
    db:
        image: mariadb
        environment:
            - "MYSQL_ROOT_PASSWORD=secret"
            - "MYSQL_DATABASE=dockerApp"
        ports:
            - "33061:3306"

app.docker

FROM php:7-fpm

RUN apt-get update && apt-get install -y libmcrypt-dev mariadb-client libmcrypt4 \
    && docker-php-ext-install pdo_mysql \
    && kill -USR2 1

WORKDIR /var/www

Upvotes: 0

Views: 241

Answers (1)

DazWilkin
DazWilkin

Reputation: 40356

Docker caches 'layers' to save repeatedly regenerating them.

RUN is one of the Dockerfile commands that generates layers and so Docker Engine will cache this layer and thus not re-RUN the command if it exists and is unchanged.

I copied your Dockerfile and built it twice. Here's the 2nd run:

docker build --rm --file=./Dockerfile --tag=59886068:latest .
Sending build context to Docker daemon  2.048kB
Step 1/3 : FROM php:7-fpm
 ---> fa37bd6db22a
Step 2/3 : RUN apt-get update && apt-get install -y libmcrypt-dev mariadb-client libmcrypt4     && docker-php-ext-install pdo_mysql     && kill -USR2 1
 ---> Using cache
 ---> c76aaadf8680
Step 3/3 : WORKDIR /var/www
 ---> Using cache
 ---> fc0287d8edb2
Successfully built fc0287d8edb2
Successfully tagged 59886068:latest

NB Step #2 is Using cache on the 2nd build for (in my case) c76aaadf8680.

You can see this using:

docker image history 59886068:latest

IMAGE               CREATED             CREATED BY
fc0287d8edb2        21 minutes ago      /bin/sh -c #(nop) WORKDIR /var/www
c76aaadf8680        21 minutes ago      /bin/sh -c apt-get update && apt-get install…
fa37bd6db22a        3 weeks ago         /bin/sh -c #(nop)  CMD ["php-fpm"]

Or:

docker image ls --all | grep c76aaadf8680

<none>    <none>    c76aaadf8680        21 minutes ago      477MB

NB Your image ID will be different.

You can force docker and docker-compose to rebuild images obviating the cache with:

docker build --no-cache ...
docker-compose build --no-cache ...

Upvotes: 1

Related Questions