Scott Skiles
Scott Skiles

Reputation: 3847

Why does docker-compose build a new image on `docker-compose up`?

When I run docker-compose up, why does a new image get created? How can I tell docker-compose to run with the image I just created? What am I doing wrong here?

When I run the command docker build . -t prod_srdc_web from a clean system, using docker system prune -a, everything is built fine. When I run docker image ls after building the image I see the following:

REPOSITORY          TAG                 IMAGE ID            CREATED              SIZE
prod_srdc_web       latest              923538608c06        5 seconds ago        1.1GB
<none>              <none>              04b24dbf7c93        About a minute ago   4.23MB
python              3.7                 42d620af35be        9 days ago           918MB
alpine              3.7                 6d1ef012b567        4 months ago         4.21MB

After I run docker-compose up everything gets built again and the container launches. Now docker image ps shows an identical image with a new name, srdc_django_web:

REPOSITORY          TAG                 IMAGE ID            CREATED              SIZE
srdc_django_web     latest              7e75a841e0fd        About a minute ago   1.1GB
prod_srdc_web       latest              923538608c06        2 minutes ago        1.1GB
<none>              <none>              04b24dbf7c93        4 minutes ago        4.23MB
python              3.7                 42d620af35be        9 days ago           918MB
alpine              3.7                 6d1ef012b567        4 months ago         4.21MB

Why does docker-compose up build a new image? Ideally I'd like to use the image I just built and tagged, prod_srdc_web.

Dockerfile:

FROM alpine:3.7

RUN apk upgrade --no-cache build-base gcc python=3.7 python3-dev postgresql-dev bash git

ENV LIBRARY_PATH=lib:/usr/lib

FROM python:3.7
ENV PYTHONUNBUFFERED 1
RUN mkdir -p /code && mkdir -p /var/www/website.com/static/
WORKDIR /code

# Install dependencies
COPY requirements.txt /code/
RUN pip3 install --no-cache-dir --upgrade pip
RUN pip3 install -r requirements.txt
RUN pip3 install gunicorn

# Copy project
COPY . /code

# Copy entrypoint.prod.sh
COPY ./entrypoint.prod.sh /

EXPOSE 4500

# Run entrypoint.prod.sh
RUN [ "chmod", "+x", "./entrypoint.prod.sh" ]
ENTRYPOINT [ "./entrypoint.prod.sh" ]

docker-compose.yml

version: '3'

services:
  django_web:
    build: .
    volumes:
      - .:/srdc
    ports:
      - "4500:4500"
    container_name: srdc_c_django_web

entrypoint.prod.sh

#!/usr/bin/env bash
python manage.py makemigrations
python manage.py migrate --noinput
python manage.py collectstatic --noinput
gunicorn srdc.wsgi:application -w 3 -b 0.0.0.0:4500

It looks like the repository name srdc_django_web is created by concatenating the directory name (srdc) with the service name in the docker-compose.yml file (django_web). If I eventually add Nginx I want to use docker-compose up to run the entire container prod_srdc_web, not have it build each service fresh.

FYI my versions are:

Docker version 18.09.2, build 6247962
docker-compose version 1.23.2, build 1110ad01

Upvotes: 2

Views: 3694

Answers (1)

BMitch
BMitch

Reputation: 263666

Your compose file does not specify an image name. Therefore, even if you build the image in advance manually, docker-compose does not have any mapping from the build you want to run and the image you've already created. To give an image name, specify:

version: '3'

services:
  django_web:
    build: .
    image: prod_srdc_web:latest
    volumes:
      - .:/srdc
    ports:
      - "4500:4500"
    container_name: srdc_c_django_web

Docker will use the image cache to avoid recreating the same image layers that it already has. However, since you have the following:

COPY . /code

Any file in the build context (the current directory, assuming you do not ignore files with a .dockerignore) that changes will result in a new image being created. That can include file ownership and permissions, and files like the docker-compose.yml if they are in this directory are also included.

Upvotes: 5

Related Questions