Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Updating docker image with new code

I am having trouble understanding this part of docker. I have spent many days doing research over this and came across stuff like docker-compose and docker container commit. If I have an image and I make changes to my codebase, how would I update my docker image with the code changes? I thought the Dockerfile was supposed to take care of this, but it does not work for me. Any help is appreciated.

like image 727
joethemow Avatar asked May 11 '18 22:05

joethemow


People also ask

How do I change the docker image code?

Navigate to the directory you'd like to open and press “OK”. The sidebar will update to display the selected directory's contents. Click any of the files to open it in the VS Code editor. You can now make changes inside the container, without manually copying files or setting up a working directory bind mount.


3 Answers

Dockerfile takes care of creating a docker image that can be used to run containers. If you would like to update the image configuration or the code base of your application which inside the image you need to rebuild the image.

For instance you might have a Dockerfile that pulls the codebase during the build process in this case you need to rebuild the image whenever there is a new code that needs to be included. or lets say you need to update the production version of your application then you need to rebuild the image which contains a new version of the stable branch of your application.

Also if you are using it for development then you might need to mount the application directory as a volume which makes you able to see the changes immediately without the need to rebuild your docker image unless there is a need to do it.

For docker-compose you can consider it similar to docker run command which provides an easier way to run a container from a specific image including exposing ports or mounting volumes or linking multiple container together, etc..

References:

  • Use volumes | Docker Documentation
  • Overview of Docker Compose | Docker Documentation
like image 180
Mostafa Hussein Avatar answered Oct 18 '22 17:10

Mostafa Hussein


You could try docker-compose up --force-recreate; or if you make changes to the Dockerfile you may have to rebuild it. You can do so by using docker-compose build (you could append --no-cache at the end to force-download the dependencies etc.).

Note that, in some cases you may have to delete created images/builds/containers using docker system prune (you could append -a at the end of it to also remove any stopped containers and all unused images -- not just dangling images). Then do a build using docker-compose build.

You could also use docker stop $(docker ps -a -q) followed by docker rm $(docker ps -a -q) to stop and remove all docker containers.

Hopefully this would point you in the right direction, good luck!

like image 42
designcise Avatar answered Oct 18 '22 17:10

designcise


There are two options.

  1. If you use docker-compose commands,

1.1. first, remove the previous image by running,

docker rmi 'image_id' or docker rmi -f 'image_id'

1.2. Then recreate the image by running,

docker-compose up
  1. If you are using docker commands run,

    docker build

like image 5
Shirantha Madusanka Avatar answered Oct 18 '22 17:10

Shirantha Madusanka