Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update source code without rebuilding image each time?

Is there a way to avoid rebuilding my Docker image each time I make a change in my source code ?

I think I have already optimize my Dockerfile enough to decrease building time, but it's always 2 commands and some waiting time for sometimes just one line of code added. It's longer than a simple CTRL + S and check the results.

The commands I have to do for each little update in my code:

docker-compose down
docker-compose build
docker-compose up

Here's my Dockerfile:

FROM python:3-slim as development

ENV PYTHONUNBUFFERED=1

COPY ./requirements.txt /requirements.txt
COPY ./scripts /scripts

EXPOSE 80

RUN apt-get update && \
    apt-get install -y \
    bash \
    build-essential \
    gcc \
    libffi-dev \
    musl-dev \
    openssl \
    wget \
    postgresql \
    postgresql-client \
    libglib2.0-0 \
    libnss3 \
    libgconf-2-4 \
    libfontconfig1 \
    libpq-dev && \
    pip install -r /requirements.txt && \
    mkdir -p /vol/web/static && \
    chmod -R 755 /vol && \
    chmod -R +x /scripts

COPY ./files /files

WORKDIR /files

ENV PATH="/scripts:/py/bin:$PATH"

CMD ["run.sh"]

Here's my docker-compose.yml file:

version: '3.9'

x-database-variables: &database-variables
  POSTGRES_DB: ${POSTGRES_DB}
  POSTGRES_USER: ${POSTGRES_USER}
  POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
  ALLOWED_HOSTS: ${ALLOWED_HOSTS}

x-app-variables: &app-variables
  <<: *database-variables
  POSTGRES_HOST: ${POSTGRES_HOST}
  SPOTIPY_CLIENT_ID: ${SPOTIPY_CLIENT_ID}
  SPOTIPY_CLIENT_SECRET: ${SPOTIPY_CLIENT_SECRET}
  SECRET_KEY: ${SECRET_KEY}
  CLUSTER_HOST: ${CLUSTER_HOST}
  DEBUG: 0

services:
  website:
    build:
      context: .
    restart: always
    volumes:
      - static-data:/vol/web
    environment: *app-variables
    depends_on:
      - postgres

  postgres:
    image: postgres
    restart: always
    environment: *database-variables
    volumes:
      - db-data:/var/lib/postgresql/data

  proxy:
    build:
      context: ./proxy
    restart: always
    depends_on:
      - website
    ports:
      - 80:80
      - 443:443
    volumes:
      - static-data:/vol/static
      - ./files/templates:/var/www/html
      - ./proxy/default.conf:/etc/nginx/conf.d/default.conf
      - ./etc/letsencrypt:/etc/letsencrypt

volumes:
  static-data:
  db-data:
like image 702
Jeremy Avatar asked Sep 12 '25 17:09

Jeremy


1 Answers

Mount your script files directly in the container via docker-compose.yml:

volumes:
  - ./scripts:/scripts
  - ./files:/files

Keep in mind you have to use a prefix if you use a WORKDIR in your Dockerfile.

like image 95
Robert-Jan Kuyper Avatar answered Sep 15 '25 10:09

Robert-Jan Kuyper