Updating and maintaining your docker images

Previously, I have created a blog post about installing a lightweight blog using ghost and nginx.
For high availability and failover, place an ec2 instance to at least two different availability zones (such as one ec2 instance at ap-southeast-1b and one ec2 instance at ap-southeast-1a) using an auto-scaling group attached to an elastic load balancer.

If you are running your blog using this type of configuration, here are some tips to maintain your local docker image repository and keep it up to date. This also helps you to clean the old images that are pulled and locally stored. Old images or not used images consume local storage space such as ghost is 300MGB and each image will be the similar size. Better to ensure not to have any images stored in your ec2 volumes.

You can back-up the images to a tar file using below command:

docker save -o ghost.tar ghost

docker save -o ngnix.tar nginx

You can copy the files to your s3 bucket for backup

aws s3 cp ghost.tar s3://<bucketname>/ghost.tar

Ok. Now, pull the latest images from docker hub:

docker pull ghost

docker pull nginx

List the current docker images

docker images

It should give you a list of images stored locally as below:

docker image list screenshot

So, at this list, Look at tag section for each image, latest tag is the updated version we just pulled from docker hub. Identify the image ID of the ones that are old or not used. If you haven't stopped the running container which is already using the image, you will not be able to remove the image just yet.

Stop the running containers:

docker stop blog

docker stop nginxserver

Remove the containers:

docker rm blog

docker rm nginxserver

Remove the unused images except the ones having the latest tag. (if you remove all containers when you execute the run command docker will pull the latest image by default)

docker rmi <imageID>

Run the containers with new images

docker run --name blog -p 2368:2368 -e NODE_ENV=production -v /home/ec2-user/dev/ghost/:/var/lib/ghost ghost -d

docker run -v /home/ec2-user/dev/ghost/Nginx/sites-enabled/:/etc/nginx/conf.d/ -v /home/ec2-user/dev/ghost/Nginx/certs/:/etc/nginx/certs -v /home/ec2-user/dev/ghost/Nginx/logs/:/var/log/nginx --name nginxserver -p 80:80 -d --link blog:blog nginx

Congratulations! you have just updated your blog to the latest version of ghost and nginx images.