How to rebuild docker container
How to rebuild docker container
Rebuild container after each change?
The Docker documentation suggests to use the ONBUILD instruction if you have the following scenario:
For example, if your image is a reusable python application builder, it will require application source code to be added in a particular directory, and it might require a build script to be called after that. You can’t just call ADD and RUN now, because you don’t yet have access to the application source code, and it will be different for each application build. You could simply provide application developers with a boilerplate Dockerfile to copy-paste into their application, but that is inefficient, error-prone and difficult to update because it mixes with application-specific code.
Basically, this all sounds nice and good, but that does mean that I have to re-create the app container every single time I change something, even if it’s only a typo.
This doesn’t seem to be very efficient, e.g. when creating web applications where you are used to change something, save, and hit refresh in the browser.
How do you deal with this?
1 Answer 1
does mean that I have to re-create the app container every single time I change something, even if it’s only a typo
Note that the ONBUILD instruction is meant for cases where a Dockerfile inherits FROM a parent Dockerfile. The ONBUILD instructions found in the parent Dockerfile would be run when Docker builds an image of the child Dockerfile.
This doesn’t seem to be very efficient, e.g. when creating web applications where you are used to change something, save, and hit refresh in the browser.
If you are using a Docker container to serve a web application while you are iterating on that application code, then I suggest you make a special Docker image which only contains everything to run your app but the app code.
Then share the directory that contains your app code on your host machine with the directory from which the application files are served within the docker container.
That way I can change files in /home/thomas/workspace/project1/ and the changes are reflected live without having to rebuild the docker image or even restart the docker container.
How To Completely Rebuild A Docker Container
Not entirely sure what I am asking but I will attempt to try and explain what I want.
I have a docker-compose file. And from the command line I type in.
The first time this ever happens it tells me it is pulling the docker image and then downloading it, building it and running it.
Sometimes I need to change the docker-compose file and I would like to just re-do the entire process (as specified above).
But. the only way I can see to do this is to go the the GUI of docker (on mac) and hit the bomb icon to factory reset the entire thing.
Of course this nukes my other docker images (which feels like a sledgehammer to crack a nut).
Can anybody furnish me with a command that will reset and force and entire fresh download of an image?
3 Answers 3
Trending sort
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch to Trending sort
So my understanding of what you’re asking for is to be able to make the very first run of a docker compose up operation repeatable
So docker provides the following commands:
This lists all the images for a given name. It works with wildcards too, so if all your images are prefixed then docker images my_app* works
Now you take those ids and use docker rmi which will delete the image.
TL;DR;
So putting it all together you go:
I think the backtick technique will work in OSX (under bash), but if it doesn’t you just want to evaluate the image ids first before docker rmi is called. Let me know and I’ll remove this paragraph.
How to rebuild docker container in docker-compose.yml?
There are scope of services which defined in docker-compose.yml. These service have been started. I need to rebuild only one of these and start it without up other services. I run the following commands:
11 Answers 11
Trending sort
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch to Trending sort
With docker-compose 1.19 up
Without one or more service_name arguments all images will be built if missing and all containers will be recreated.
From the help menu
Without cache
To force a rebuild to ignore cached layers, we have to first build a new image
From the help menu
Then recreate the container
This should fix your problem:
As @HarlemSquirrel posted, it is the best and I think the correct solution.
But, to answer the OP specific problem, it should be something like the following command, as he doesn’t want to recreate ALL services in the docker-compose.yml file, but only the nginx one:
Maybe these steps are not quite correct, but I do like this:
Removing container with rm is essential. Without removing, Docker will start old container.
didn’t work (you said it is still running). If you are going to rebuild it anyway, you can try killing it:
If it still doesn’t work, try to stop it with docker directly:
If that still doesn’t work, check your version of docker, you might want to upgrade.
It might be a bug, you could check if one matches your system/version. Here are a couple, for ex: https://github.com/docker/docker/issues/10589
As a workaround, you could try to kill the process.
Rebuild Docker container on file changes
For running an ASP.NET Core application, I generated a dockerfile which build the application and copys the source code in the container, which is fetched by Git using Jenkins. So in my workspace, I do the following in the dockerfile:
While Jenkins updates the files on my host correctly with Git, Docker doesn’t apply this to my image.
My basic script for building:
What is the recommended way to update a container?
My typical scenario would be: The application is running on the server in a Docker container. Now parts of the app are updated, e.g. by modifying a file. Now the container should run the new version. Docker seems to recommend building a new image instead of modifying a existing container, so I think the general way of rebuilding like I do is right, but some detail in the implementation has to be improved.
4 Answers 4
Trending sort
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch to Trending sort
Video with visual explanation (from 2022)
Since I got a lot of positive feedback to my previously, first visual explanation, I decided to create another video for this question and answer since there are some things which can be visualized better in a graphical video. It visualizes and also updates this answers with the knowledge and experience which I got in the last years using Docker on multiple systems (and also K8s).
While this question was asked in the context of ASP.NET Core, it is not really related to this framework. The problem was a lack of basic understanding of Docker concepts, so it can happen with nearly every application and framework. For that reason, I used a simple Nginx webserver here since I think many of you are familiar with web servers, but not everyone knows how specific frameworks like ASP.NET Core works.
The underlying problem is to understand the difference of containers vs images and how they are different in their lifecycle, which is the basic topic of this video.
Textual answer (Originally from 2016)
After some research and testing, I found that I had some misunderstandings about the lifetime of Docker containers. Simply restarting a container doesn’t make Docker use a new image, when the image was rebuilt in the meantime. Instead, Docker is fetching the image only before creating the container. So the state after running a container is persistent.
Why removing is required
Therefore, rebuilding and restarting isn’t enough. I thought containers works like a service: Stopping the service, do your changes, restart it and they would apply. That was my biggest mistake.
Containers should be as independent as possible
With this knowledge, it’s comprehensible why storing data in containers is qualified as bad practice and Docker recommends data volumes/mounting host directorys instead: Since a container has to be destroyed to update applications, the stored data inside would be lost too. This cause extra work to shutdown services, backup data and so on.
So it’s a smart solution to exclude those data completely from the container: We don’t have to worry about our data, when its stored safely on the host and the container only holds the application itself.
How to remove a container
We can bypass those limitations by simply removing the container:
There is a better way (Added 2016)
Especially when you’re using multiple containers, compose offers way more benefits. For example, linking the containers which requires to create/maintain networks manually otherwise. You can also specify dependencies, so that a database container is started before the application server, which depends on the DB at startup.
In the past with Docker-Compose 1.x I noticed some issues, especially with caching. This results in containers not being updated, even when something has changed. I have tested compose v2 for some time now without seeing any of those issues again, so it seems to be fixed now.
Full script for rebuilding a Docker container (original answer vom 2016)
According to this new knowledge, I fixed my script in the following way:
Visual Studio Docker Tools how to force a container rebuild from scratch
Here are my images:
Is there a way to do this with the tool?
3 Answers 3
Trending sort
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch to Trending sort
First of all, container tools 1.0 (previously called Docker Tools) is built in VS 2017. You don’t need to install 0.41. In fact, 0.41 only supports VS 2015. Please do NOT install 0.41 on VS 2017 even if it is possible.
Visual Studio Docker Tools how to force a container rebuild from scratch
Running ‘clean’ against the docker-compose.dcproj in Visual Studio will do docker-compose down for you. If you want to rebuild images, switch to ‘Release’ configuration and run ‘rebuild’ against docker-compose.dcproj.
There is also a command line solution.
To refresh the PostgreSQL database environment variables, you need to remove the database container and its volume. When Visual Studio next runs the docker-compose project, it will recreate the database container and its associated volume with a new ‘install’ of PostGres and updated environment variables. You will lose your data, so backup first!
Here is the starting point, two containers: one for the ASP.NET Core container ( mcoapi ) and one for the database ( mcodatabase ). Mco is the acronym for our company. \v4 is the root of my Visual Studio solution but these commands can be run from anywhere as we’re not using the docker-compose command line (see why below).
First, I stop the containers. I am using the first two digits of the CONTAINER_ID as a shorthand.
Now I remove the containers. I need to remove both because I believe they are both using the same docker volume.
I now have no containers.
I can now remove the volume where the database is stored.
BEFORE YOU DO, BACKUP ANY DATA YOU NEED, THIS WILL WIPE THE DATABASE!
Next, I check to see what volumes have been mounted:
I now run the docker-compose project in Visual Studio and the database container is recreated with the new values.
Why I didn’t call docker-compose from the command line
It’s possible to use docker-compose down like commands on the command line to perform similar tear downs but I found the command line more complex to get right because it includes all the different yml files that Visual Studio needs. For example, the up that is called from the Visual Studio tool is:
This was less intuitive than doing it manually. If someone knows a better way, I’m all ears!
Источники информации:
- http://stackoverflow.com/questions/50461413/how-to-completely-rebuild-a-docker-container
- http://stackoverflow.com/questions/36884991/how-to-rebuild-docker-container-in-docker-compose-yml
- http://stackoverflow.com/questions/41322541/rebuild-docker-container-on-file-changes
- http://stackoverflow.com/questions/43547594/visual-studio-docker-tools-how-to-force-a-container-rebuild-from-scratch