[git] 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:

COPY src src

While Jenkins updates the files on my host correctly with Git, Docker doesn't apply this to my image.

My basic script for building:


docker build -t $imageName -f Dockerfile  .

containerRunning=$(docker inspect --format="{{ .State.Running }}" $containerName 2> /dev/null)

if [ "$containerRunning" == "true" ]; then
        docker stop $containerName
        docker start $containerName
        docker run -d -p 5000:5000 --name $containerName $imageName

I tried different things like --rm and --no-cache parameter for docker run and also stopping/removing the container before the new one is build. I'm not sure what I'm doing wrong here. It seems that docker is updating the image correctly, as the call of COPY src src would result in a layer id and no cache call:

Step 6 : COPY src src
 ---> 382ef210d8fd

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.

This question is related to git jenkins docker asp.net-core dockerfile

The answer is

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.

Because containers are permanent, you have to remove them using docker rm <ContainerName> first. After a container is removed, you can't simply start it by docker start. This has to be done using docker run, which itself uses the latest image for creating a new container-instance.

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.

Why -rf may not really help you

The docker run command, has a Clean up switch called -rf. It will stop the behavior of keeping docker containers permanently. Using -rf, Docker will destroy the container after it has been exited. But this switch has two problems:

  1. Docker also remove the volumes without a name associated with the container, which may kill your data
  2. Using this option, its not possible to run containers in the background using -d switch

While the -rf switch is a good option to save work during development for quick tests, it's less suitable in production. Especially because of the missing option to run a container in the background, which would mostly be required.

How to remove a container

We can bypass those limitations by simply removing the container:

docker rm --force <ContainerName>

The --force (or -f) switch which use SIGKILL on running containers. Instead, you could also stop the container before:

docker stop <ContainerName>
docker rm <ContainerName>

Both are equal. docker stop is also using SIGTERM. But using --force switch will shorten your script, especially when using CI servers: docker stop throws an error if the container is not running. This would cause Jenkins and many other CI servers to consider the build wrongly as failed. To fix this, you have to check first if the container is running as I did in the question (see containerRunning variable).

Full script for rebuilding a Docker container

According to this new knowledge, I fixed my script in the following way:


docker build -t $imageName -f Dockerfile  .

echo Delete old container...
docker rm -f $containerName

echo Run new container...
docker run -d -p 5000:5000 --name $containerName $imageName

This works perfectly :)

Whenever changes are made in dockerfile or compose or requirements , re-Run it using docker-compose up --build . So that images get rebuild and refreshed

You can run build for a specific service by running docker-compose up --build <service name> where the service name must match how did you call it in your docker-compose file.

Example Let's assume that your docker-compose file contains many services (.net app - database - let's encrypt... etc) and you want to update only the .net app which named as application in docker-compose file. You can then simply run docker-compose up --build application

Extra parameters In case you want to add extra parameters to your command such as -d for running in the background, the parameter must be before the service name: docker-compose up --build -d application

Questions with git tag:

Does the target directory for a git clone have to match the repo name? Git fatal: protocol 'https' is not supported Git is not working after macOS Update (xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools) git clone: Authentication failed for <URL> destination path already exists and is not an empty directory SSL_connect: SSL_ERROR_SYSCALL in connection to github.com:443 GitLab remote: HTTP Basic: Access denied and fatal Authentication How can I switch to another branch in git? VS 2017 Git Local Commit DB.lock error on every commit How to remove an unpushed outgoing commit in Visual Studio? How to know the git username and email saved during configuration? How to add a new project to Github using VS Code git clone error: RPC failed; curl 56 OpenSSL SSL_read: SSL_ERROR_SYSCALL, errno 10054 fatal: ambiguous argument 'origin': unknown revision or path not in the working tree HTTP Basic: Access denied fatal: Authentication failed npm notice created a lockfile as package-lock.json. You should commit this file Do I commit the package-lock.json file created by npm 5? Abort a Git Merge key_load_public: invalid format git - remote add origin vs remote set-url origin Visual Studio 2017 - Git failed with a fatal error Get git branch name in Jenkins Pipeline/Jenkinsfile Changing the git user inside Visual Studio Code How to compare different branches in Visual Studio Code Git checkout - switching back to HEAD Clear git local cache Deleting a local branch with Git Rebuild Docker container on file changes Cloning specific branch How to add chmod permissions to file in Git? Git copy changes from one branch to another Git merge with force overwrite Project vs Repository in GitHub How to add a file to the last commit in git? Getting permission denied (public key) on gitlab Delete commit on gitlab gpg failed to sign the data fatal: failed to write commit object [Git 2.10.0] Remove a modified file from pull request Updates were rejected because the tip of your current branch is behind its remote counterpart Can't push to the heroku How to discard local changes and pull latest from GitHub repository In Visual Studio Code How do I merge between two local branches? error: RPC failed; curl transfer closed with outstanding read data remaining Change drive in git bash for windows Checkout Jenkins Pipeline Git SCM with credentials? How to fix git error: RPC failed; curl 56 GnuTLS Trying to pull files from my Github repository: "refusing to merge unrelated histories" Visual Studio Code how to resolve merge conflicts with git? merge one local branch into another local branch Can't push to remote branch, cannot be resolved to branch

Questions with jenkins tag:

Maven dependencies are failing with a 501 error Jenkins pipeline how to change to another folder Docker: Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock groovy.lang.MissingPropertyException: No such property: jenkins for class: groovy.lang.Binding How to solve npm install throwing fsevents warning on non-MAC OS? Run bash command on jenkins pipeline Try-catch block in Jenkins pipeline script How to print a Groovy variable in Jenkins? Jenkins pipeline if else not working Error "The input device is not a TTY" How to set and reference a variable in a Jenkinsfile Get git branch name in Jenkins Pipeline/Jenkinsfile Jenkins: Can comments be added to a Jenkinsfile? How to define and use function inside Jenkins Pipeline config? Environment variable in Jenkins Pipeline Rebuild Docker container on file changes How to use the curl command in PowerShell? Jenkins: Cannot define variable in pipeline stage Jenkins fails when running "service start jenkins" How to reset the use/password of jenkins on windows? How can I remove jenkins completely from linux Check if a file exists in jenkins pipeline Checkout Jenkins Pipeline Git SCM with credentials? Jenkins CI Pipeline Scripts not permitted to use method groovy.lang.GroovyObject How to retrieve current workspace using Jenkins Pipeline Groovy script? Conditional step/stage in Jenkins pipeline Jenkins Pipeline Wipe Out Workspace Jenkins "Console Output" log location in filesystem how to setup ssh keys for jenkins to publish via ssh How to list all `env` properties within jenkins pipeline job? How to pass boolean parameter value in pipeline to downstream jobs? Get absolute path to workspace directory in Jenkins Pipeline plugin Running stages in parallel with Jenkins workflow / pipeline Is it possible to capture the stdout from the sh DSL command in the pipeline How can I test a change made to Jenkinsfile locally? How can I trigger another job from a jenkins pipeline (jenkinsfile) with GitHub Org Plugin? How to change workspace and build record Root Directory on Jenkins? Jenkins vs Travis-CI. Which one would you use for a Open Source project? Jenkins - how to build a specific branch Run a command shell in jenkins How to change the JDK for a Jenkins job? Jenkins: Is there any way to cleanup Jenkins workspace? How to access parameters in a Parameterized Build? Is it ok to run docker from inside docker? Where can I find jenkins restful api reference? How to get build time stamp from Jenkins build variables? How do I clone a job in Jenkins? How to change port for jenkins window service when 8080 is being used Reading file from Workspace in Jenkins with Groovy script How/When does Execute Shell mark a build as failure in Jenkins?

Questions with docker tag:

standard_init_linux.go:190: exec user process caused "no such file or directory" - Docker What is the point of WORKDIR on Dockerfile? E: gnupg, gnupg2 and gnupg1 do not seem to be installed, but one of them is required for this operation How do I add a user when I'm using Alpine as a base image? docker: Error response from daemon: Get https://registry-1.docker.io/v2/: Service Unavailable. IN DOCKER , MAC How to fix docker: Got permission denied issue pull access denied repository does not exist or may require docker login Docker error: invalid reference format: repository name must be lowercase Docker: "no matching manifest for windows/amd64 in the manifest list entries" OCI runtime exec failed: exec failed: (...) executable file not found in $PATH": unknown Docker: Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock Can't create a docker image for COPY failed: stat /var/lib/docker/tmp/docker-builder error Is it safe to clean docker/overlay2/ Docker - Bind for failed: port is already allocated docker : invalid reference format Docker CE on RHEL - Requires: container-selinux >= 2.9 /bin/sh: apt-get: not found Docker: How to delete all local Docker images How to see docker image contents How to remove docker completely from ubuntu 14.04 Kubernetes Pod fails with CrashLoopBackOff Cannot connect to the Docker daemon at unix:/var/run/docker.sock. Is the docker daemon running? How to assign more memory to docker container Cannot connect to the Docker daemon on macOS Import data.sql MySQL Docker Container COPY with docker but with exclusion How to check if the docker engine and a docker container are running? Docker "ERROR: could not find an available, non-overlapping IPv4 address pool among the defaults to assign to the network" How to get IP address of running docker container How to run docker-compose up -d at system start up? Dockerfile if else condition with external arguments What does --net=host option in Docker command really do? Docker build gives "unable to prepare context: context must be a directory: /Users/tempUser/git/docker/Dockerfile" Error "The input device is not a TTY" How to create a DB for MongoDB container on start up? How to install "ifconfig" command in my ubuntu docker image? How to use local docker images with Minikube? How to clear the logs properly for a Docker container? accessing a docker container from another container How to stop docker under Linux How to specify Memory & CPU limit in docker compose version 3 docker build with --build-arg with multiple arguments How to mount a single file in a volume Add Insecure Registry to Docker Settings to Windows Firewall to allow Docker for Windows to share drive Can Windows Containers be hosted on linux? denied: requested access to the resource is denied : docker Understanding "VOLUME" instruction in DockerFile ARG or ENV, which one to use in this case? How to persist data in a dockerized postgres database using volumes

Questions with asp.net-core tag:

dotnet ef not found in .NET Core 3 How to use Bootstrap 4 in ASP.NET Core ASP.NET Core - Swashbuckle not creating swagger.json file Getting value from appsettings.json in .net core .net Core 2.0 - Package was restored using .NetFramework 4.6.1 instead of target framework .netCore 2.0. The package may not be fully compatible Automatically set appsettings.json for dev and release environments in asp.net core? Get ConnectionString from appsettings.json instead of being hardcoded in .NET Core 2.0 App Unable to create migrations after upgrading to ASP.NET Core 2.0 EF Core add-migration Build Failed ASP.NET Core form POST results in a HTTP 415 Unsupported Media Type response How to enable CORS in ASP.net Core WebAPI How to get root directory of project in asp.net core. Directory.GetCurrentDirectory() doesn't seem to work correctly on a mac Entity Framework Core: DbContextOptionsBuilder does not contain a definition for 'usesqlserver' and no extension method 'usesqlserver' Visual Studio 2017 error: Unable to start program, An operation is not legal in the current state The default XML namespace of the project must be the MSBuild XML namespace How to create roles in ASP.NET Core and assign them to users? Return file in ASP.Net Core Web API ASP.NET Core return JSON with status code How to read values from the querystring with ASP.NET Core? how to set ASPNETCORE_ENVIRONMENT to be considered for publishing an asp.net core application? ASP.NET Core Get Json Array using IConfiguration Rebuild Docker container on file changes tsconfig.json: Build:No inputs were found in config file ASP.NET Core Dependency Injection error: Unable to resolve service for type while attempting to activate How to read request body in an asp.net core webapi controller? How to set up Automapper in ASP.NET Core How do I access Configuration in any class in ASP.NET Core? How to register multiple implementations of the same interface in Asp.Net Core? ASP.NET Core Web API Authentication How to change the port number for Asp.Net core app? ASP.NET Core Identity - get current user ASP.NET Core Web API exception handling ASP.NET Core 1.0 on IIS error 502.5 How to get HttpContext.Current in ASP.NET Core? How to determine if .NET Core is installed How to get current url in view in asp.net core 1.0 'No database provider has been configured for this DbContext' on SignInManager.PasswordSignInAsync How to unapply a migration in ASP.NET Core with EF Core The term "Add-Migration" is not recognized bypass invalid SSL certificate in .net core AddTransient, AddScoped and AddSingleton Services Differences How to use npm with ASP.NET Core How to return HTTP 500 from ASP.NET Core RC2 Web Api? Send HTTP POST message in ASP.NET Core using HttpClient PostAsJsonAsync How to return a specific status code and no contents from Controller? How to specify the port an ASP.NET Core application is hosted on? How to get current user in asp.net core How to pass multiple parameters to a get method in ASP.NET Core Custom Authentication in ASP.Net-Core How to use SqlClient in ASP.NET Core?

Questions with dockerfile tag:

standard_init_linux.go:190: exec user process caused "no such file or directory" - Docker What is the point of WORKDIR on Dockerfile? Can't create a docker image for COPY failed: stat /var/lib/docker/tmp/docker-builder error /bin/sh: apt-get: not found COPY with docker but with exclusion Dockerfile if else condition with external arguments Docker build gives "unable to prepare context: context must be a directory: /Users/tempUser/git/docker/Dockerfile" denied: requested access to the resource is denied : docker Understanding "VOLUME" instruction in DockerFile ARG or ENV, which one to use in this case? Rebuild Docker container on file changes Docker: How to use bash with an Alpine based docker image? How to set image name in Dockerfile? How to update /etc/hosts file in Docker image during "docker build" How do I use Docker environment variable in ENTRYPOINT array? How to copy folders to docker image from Dockerfile? Difference between RUN and CMD in a Dockerfile How do I make a comment in a Dockerfile? Docker is installed but Docker Compose is not ? why? Difference between links and depends_on in docker_compose.yml Docker: unable to prepare context: unable to evaluate symlinks in Dockerfile path: GetFileAttributesEx Run a script in Dockerfile How to pass arguments within docker-compose? How to pass arguments to a Dockerfile? How to define a variable in a Dockerfile? Multiple FROMs - what it means Connect to mysql in a docker container from the host How do I pass environment variables to Docker containers? How to copy file from host to container using Dockerfile How to copy multiple files in one layer using a Dockerfile? Dockerfile copy keep subdirectory structure How to correctly link php-fpm and Nginx Docker containers? What's the difference between Docker Compose vs. Dockerfile Docker expose all ports or range of ports from 7000 to 8000 How to add users to Docker container? How to name Dockerfiles What is the difference between the 'COPY' and 'ADD' commands in a Dockerfile?