Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to upgrade docker container after its image changed

Tags:

docker

Let's say I have pulled the official mysql:5.6.21 image.

I have deployed this image by creating several docker containers.

These containers have been running for some time until MySQL 5.6.22 is released. The official image of mysql:5.6 gets updated with the new release, but my containers still run 5.6.21.

How do I propagate the changes in the image (i.e. upgrade MySQL distro) to all my existing containers? What is the proper Docker way of doing this?

like image 206
Yaroslav Stavnichiy Avatar asked Nov 04 '14 11:11

Yaroslav Stavnichiy


People also ask

How do I update a docker container?

To update to a newer image, you first need to pull the new version. Run the docker pull command followed by a colon and the name and the tag of the newer image: the name and tag that you took note of previously.

Can we update docker image?

Docker images within a running container do not update automatically. Once you have used an image to create a container, it continues running that version, even after new releases come out. It is recommended to run containers from the latest Docker image unless you have a specific reason to use an older release.

How do I update docker to latest version?

To upgrade Docker Engine, first run sudo apt-get update , then follow the installation instructions, choosing the new version you want to install.


2 Answers

After evaluating the answers and studying the topic I'd like to summarize.

The Docker way to upgrade containers seems to be the following:

Application containers should not store application data. This way you can replace app container with its newer version at any time by executing something like this:

docker pull mysql docker stop my-mysql-container docker rm my-mysql-container docker run --name=my-mysql-container --restart=always \   -e MYSQL_ROOT_PASSWORD=mypwd -v /my/data/dir:/var/lib/mysql -d mysql 

You can store data either on host (in directory mounted as volume) or in special data-only container(s). Read more about it

  • About volumes (Docker docs)
  • Tiny Docker Pieces, Loosely Joined (by Tom Offermann)
  • How to deal with persistent storage (e.g. databases) in Docker (Stack Overflow question)

Upgrading applications (eg. with yum/apt-get upgrade) within containers is considered to be an anti-pattern. Application containers are supposed to be immutable, which shall guarantee reproducible behavior. Some official application images (mysql:5.6 in particular) are not even designed to self-update (apt-get upgrade won't work).

I'd like to thank everybody who gave their answers, so we could see all different approaches.

like image 173
Yaroslav Stavnichiy Avatar answered Sep 22 '22 01:09

Yaroslav Stavnichiy


I don't like mounting volumes as a link to a host directory, so I came up with a pattern for upgrading docker containers with entirely docker managed containers. Creating a new docker container with --volumes-from <container> will give the new container with the updated images shared ownership of docker managed volumes.

docker pull mysql docker create --volumes-from my_mysql_container [...] --name my_mysql_container_tmp mysql 

By not immediately removing the original my_mysql_container yet, you have the ability to revert back to the known working container if the upgraded container doesn't have the right data, or fails a sanity test.

At this point, I'll usually run whatever backup scripts I have for the container to give myself a safety net in case something goes wrong

docker stop my_mysql_container docker start my_mysql_container_tmp 

Now you have the opportunity to make sure the data you expect to be in the new container is there and run a sanity check.

docker rm my_mysql_container docker rename my_mysql_container_tmp my_mysql_container 

The docker volumes will stick around so long as any container is using them, so you can delete the original container safely. Once the original container is removed, the new container can assume the namesake of the original to make everything as pretty as it was to begin.

There are two major advantages to using this pattern for upgrading docker containers. Firstly, it eliminates the need to mount volumes to host directories by allowing volumes to be directly transferred to an upgraded containers. Secondly, you are never in a position where there isn't a working docker container; so if the upgrade fails, you can easily revert to how it was working before by spinning up the original docker container again.

like image 37
kMaiSmith Avatar answered Sep 24 '22 01:09

kMaiSmith