David Findlay
David Findlay

Reputation: 1356

Docker Image Contains files that Docker Container doesn't

I have a Dockerfile that contains steps that create a directory and runs an angular build script outputing to that directory. This all seems to run correctly. However when the container runs, the built files and directory are not there.

If I run a shell in the image:

docker run -it pnb_web sh
# cd /code/static
# ls
assets  favicon.ico  index.html  main.js  main.js.map  polyfills.js  polyfills.js.map  runtime.js  runtime.js.map  styles.js  styles.js.map  vendor.js  vendor.js.map

If I exec a shell in the container:

docker exec -it ea23c7d30333 sh
# cd /code/static
sh: 1: cd: can't cd to /code/static
# cd /code
# ls
Dockerfile  api  docker-compose.yml  frontend  manage.py  mysite.log  pnb  profiles  requirements.txt  settings.ini  web_variables.env

david@lightning:~/Projects/pnb$ docker container ls
CONTAINER ID        IMAGE                          COMMAND                  CREATED             STATUS              PORTS                                            NAMES
ea23c7d30333        pnb_web                        "python3 manage.py r…"   13 seconds ago      Up 13 seconds       0.0.0.0:8000->8000/tcp                           pnb_web_1_267d3a69ec52

This is my dockerfile:

FROM python:3
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
RUN curl -sL https://deb.nodesource.com/setup_10.x | bash -
RUN apt install nodejs
WORKDIR /code
ADD requirements.txt /code/
RUN pip install -r requirements.txt
ADD . /code/
RUN mkdir /code/static
WORKDIR /code/frontend
RUN npm install -g @angular/cli
RUN npm install
RUN ng build --outputPath=/code/static

and associated docker-compose:

version: '3'

services:
  db:
    image: postgres
  web:
    build:
      context: .
      dockerfile: Dockerfile
    working_dir: /code
    env_file:
      - web_variables.env
    command: python3 manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/code
    ports:
      - "8000:8000"
    depends_on:
      - db

In the second example, the static directory has never been created or built into. I thought that a container is an instance of an image. How can the container be missing files from the image?

Upvotes: 2

Views: 710

Answers (1)

prometherion
prometherion

Reputation: 2299

You're confusing build-time and run-time, along playing with Volumes.

Remember that host mount has priority over FS provided by the running container, so even your built image has assets, they are going to be overwritten by .services.web.volumes because you're mounting the host filesystem that overwrites the build result.

If you try to avoid volumes mounting you'll notice that everything is working as expected.

Upvotes: 4

Related Questions