eignhpants
eignhpants

Reputation: 1771

docker-compose not showing any changes to code

I am working on a project that uses docker-compose up to start up and run. I am not very familiar with docker-compose and I couldn't really find an answer in the docs. According to the docs the up command rebuilds the container from the docker file, however this does not seem to happen.

When I try to add print commands for debugging nothing happens. The program already throws a few print commands and I tried changing those to be sure, and they always print the same. Do I have to add something to the up command to make the container rebuild?

docker-compose/yml:

dockbrato:
  build: .
  volumes:
    - "/sys:/sys"
    - "/var/run/docker.sock:/var/run/docker.sock"

Upvotes: 57

Views: 51034

Answers (6)

Mykola Danylko
Mykola Danylko

Reputation: 31

We don't need to run docker-compose up --build each time if we want our changes to be applied. Using volume, we can set a bind between our dev directory (where changes were made) and dir inside a docker container.

Example: In my file system, the working dir for my project is /home/user/frontend, and inside the container is home/node/app. All I need to do is set a proper mapping like this:

volumes:
  - /home/user/frontend:/home/node/app

Docker will pick up changes from your working dir dynamically.

(windows users should use wsl with some disto to make it work)

Upvotes: 2

Matthew Purdon
Matthew Purdon

Reputation: 773

Seems like many people don't get the difference between ADD/COPY during build and changes to a mounted volume. If changes in a mounted volume seem to stop syncing I stop the container and restart it using:

docker-compose stop <container>
docker-compose up -Vd <container>

which recreates volumes instead of reloading them

Upvotes: 2

Rajshree Rai
Rajshree Rai

Reputation: 642

You can use

$ docker-compose build --no-cache
$ docker-compose up --force-recreate

taken from https://vsupalov.com/docker-compose-runs-old-containers/

Upvotes: 15

lionbigcat
lionbigcat

Reputation: 993

To force the image to be rebuilt after a change in the source, use the --build flag:

docker-compose up --build

will rebuild all layers after the layer with the changes. For layers before the change, docker uses the cache versions.

This avoids having to use two separate commands as per @jwodder's answer.

Upvotes: 32

Abdollah
Abdollah

Reputation: 5177

I had a similar problem with Docker version 1.3 and Docker Compose version 1.22 on CentOS 7. I could resolve the problem by upgrading to Docker version 18.06. To do this, I took the following steps. First, I removed the old version of Docker by running the following command:

sudo yum remove docker \
                  docker-client \
                  docker-client-latest \
                  docker-common \
                  docker-latest \
                  docker-latest-logrotate \
                  docker-logrotate \
                  docker-selinux \
                  docker-engine-selinux \
                  docker-engine \

Then, I set up required repositories:

sudo yum install -y yum-utils \
    device-mapper-persistent-data \
    lvm2
sudo yum-config-manager \
    --add-repo \
    https://download.docker.com/linux/centos/docker-ce.repo

and finally installed the latest CE version:

sudo yum install docker-ce

After upgrading Docker, I removed containers built based on the old version of Docker by running this command:

sudo docker container rm -f -v my_container_1 my_container_2

Running docker-compose up rebuilt the containers. Afterward, changing the code was successfully reflected in the desired container.

Upvotes: 1

jwodder
jwodder

Reputation: 57470

As far as I can tell, docker-compose up only builds images that don't exist already; it will not rebuild an image whose source has changed. If you want to rebuild your image, run docker-compose build before docker-compose up.

Upvotes: 71

Related Questions