Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strategies for deciding when to use 'docker run' vs 'docker start' and using the latest version of a given image

Tags:

docker

I'm dockerizing some of our services. For our dev environment, I'd like to make things as easy as possible for our developers and so I'm writing some scripts to manage the dockerized components. I want developers to be able to start and stop these services just as if they were non-dockerized. I don't want them to have to worry about creating and running the container vs stopping and starting and already-created container. I was thinking that this could be handled using Fig. To create the container (if it doesn't already exist) and start the service, I'd use fig up --no-recreate. To stop the service, I'd use fig stop.

I'd also like to ensure that developers are running containers built using the latest images. In other words, something would check to see if there was a later version of the image in our Docker registry. If so, this image would be downloaded and run to create a new container from that image. At the moment it seems like I'd have to use docker commands to list the contents of the registry (docker search) and compare that to existing local containers (docker ps -a) with the addition of some greping and awking or use the Docker API to achieve the same thing.

Any persistent data will be written to mounted volumes so the data should survive the creation of a new container.

This seems like it might be a common pattern so I'm wondering whether anyone else has given these sorts of scenarios any thought.

like image 552
bjlevine Avatar asked Nov 12 '14 16:11

bjlevine


2 Answers

This is what I've decided to do for now for our Neo4j Docker image:

I've written a shell script around docker run that accepts command-line arguments for the port, database persistence directory on the host, log file persistence directory on the host. It executes a docker run command that looks like:

docker run --rm -it -p ${port}:7474 -v ${graphdir}:/var/lib/neo4j/data/graph.db -v ${logdir}:/var/log/neo4j  my/neo4j

By default port is 7474, graphdir is $PWD/graph.db and logdir is $PWD/log.

--rm removes the container on exit, however the database and logs are maintained on the host's file system. So no containers are left around.

-it allows the container and the Neo4j service running within it to receive signals so that the service can be gracefully shut down (the Neo4j server gracefully shuts down on SIGINT) and the container exited by hitting ^C or sending it a SIGINT if the developer puts this in the background. No need for separate start/stop commands.

Although I certainly wouldn't do this in production, I think this fine for a dev environment.

like image 129
bjlevine Avatar answered Sep 20 '22 14:09

bjlevine


I am not familiar with fig but your scenario seems good.

Usually, I prefer to kill/delete + run my container instead of playing with start/stop though. That way, if there is a new image available, Docker will use it. This work only for stateless services. As you are using Volumes for persistent data, you could do something like this.

Regarding the image update, what about running docker pull <image> every N minutes and checking the "Status" that the command returns? If it is up to date, then do nothing, otherwise, kill/rerun the container.

like image 40
creack Avatar answered Sep 19 '22 14:09

creack