Gappa
Gappa

Reputation: 827

How to push and pull modified docker's images automatically?

I have a docker image that I can run in many servers with different parameters. Into the docker image, there is a git repository that needs to be pulled any how. So I need something that:

  1. run the "master" image of the docker in interactive mode (-it)
  2. pull the git repository inside the docker
  3. commit the container's differences (caused by the git pull)
  4. push the new image to a docker cloud registry
  5. pull the new image in each server where the docker was already been installed

some questions:

Now I need to try the watchtower program or find another tools.

Upvotes: 0

Views: 3296

Answers (1)

David Maze
David Maze

Reputation: 158908

To get the net effect of this, you should:

  1. Write a Dockerfile that does the work of installing your application in a pristine Docker container (running docker build will make an image out of it)

  2. Check this Dockerfile into your git repository alongside your source code

  3. Set up some CI system to rebuild the Docker container on every change and tag it with some unique tag (a timestamp, the git commit hash, a relevant git tag) and push it to a repository

  4. On the systems where the containers are running, docker stop && docker rm them, then docker run them with the new tagged image

This approach has two important advantages over what you describe. The first is that anybody who has the source repository can rebuild exactly the running image. (In your approach if you accidentally lose a running container you can't reproduce what was running.) The second is that, if a build goes wrong, it's easy enough to roll back to running the previous version of the image just by changing the tag back.

In particular, if you're asking "can I run something like a bash script, with docker run, so that I can docker commit the result", a Dockerfile is almost exactly what you're looking for.

The last step is the least well-defined of these. You can use a simple cluster manager tool like Ansible to cause containers to be running in places; or update an image version in something like a Docker Compose YAML file running on Docker Swarm; or the watchtower tool you identified it looks like could do it. This is something that Kubernetes does extremely well, but it's...an investment.

In the workflow you describe, there are a couple of things I would say are distinctly not best practices in production environments. I'd suggest you should basically never use docker commit (docker build is quite straightforward and gives you reproducible image builds; even in the context of an SO question "here's my Dockerfile" is much easier to describe than "I did a bunch of stuff in a container and then committed it"). docker exec is useful for debugging but shouldn't be the principal way you interact with containers. Finally, using the same image name/tag and committing different images under that same tag makes it difficult to roll back to an older version of the code ("don't use the :latest tag").

Upvotes: 1

Related Questions