How to introduce individual version of docker container in ansible script? - docker

Let suppose 4 docker containers are running. they have there respective versions. Now I want to introduce these individual version in ansible script. Each version need to be declared in group_vars (with leatest by default)
so how can I do that ? appreciated if you reply to this post

Containers themselves can be referred to by their container names, plain and simple. You can add whatever you want to the name within the limitations of container names, e.g. docker run -d --name="webapp-container-1462574616" milind/webapp:0.0.10 or whatever, and then that is how you would refer to that specific container anywhere else. For example docker stop webapp-container-1462574616. You refer to images via the version in the image tag, e.g. milind/webapp:0.0.10.

Related

How to get "build history" of a docker container when inside a container?

When you are inside a docker container, is there anyway to obtain the "build history" (i.e. the original Dockerfile, or rather list of commands in the original Dockerfile that was used to build that container)?
The reason is that for tracking and version control purposes, it might be useful to indicate what/how the environment was configured when the process was run.
Thanks.
You can do it with
docker history
command. But not inside the container. Container itself does not have Docker and container itself does not hold its own history. To run that command you need to be in the host and not in the container.
docker history documentation
have a great explanation on how to use that command.
docker label is a good way to add additional metadata to your docker images.
Check this for more info.
You can get this data using docker inspect. But these commands can be run from outside the container, to run it from the inside you need to make use of docker remote api's as explained here in this answer.
You can also retrieve details of docker image using docker history through this remote api.
If you want just few details about images like version, etc. Then put those data as environment variable while building the image so that you can refer it later inside your running docker container.

How can I reuse a Docker container as a service?

I already have a running container for both postgres and redis in use for various things. However, I started those from the command line months ago. Now I'm trying to install a new application and the recipe for this involves writing out a docker compose file which includes both postgres and redis as services.
Can the compose file be modified in such a way as to specify the already-running containers? Postgres already does a fine job of siloing any of the data, and I can't imagine that it would be a problem to reuse the running redis.
Should I even reuse them? It occurs to me that I could run multiple containers for both, and I'm not sure there would be any disadvantage to that (other than a cluttered docker ps output).
When I set container_name to the names of the existing containers, I get what I assume is a rather typical error of:
cb7cb3e78dc50b527f71b71b7842e1a1c". You have to remove (or rename) that container to be able to reuse that name.
Followed by a few that compain that the ports are already in use (5432, 6579, etc).
Other answers here on Stackoverflow suggest that if I had originally invoked these services from another compose file with the exact same details, I could do so here as well and it would reuse them. But the command I used to start them was somehow never written to my bash_history, so I'm not even sure of the details (other than name, ports, and restart always).
Are you looking for docker-compose's external_links keyword?
external_links allows you reuse already running containers.
According to docker-compose specification:
This keyword links to containers started outside this docker-compose.yml or even outside of Compose, especially for containers that provide shared or common services. external_links follow semantics similar to the legacy option links when specifying both the container name and the link alias (CONTAINER:ALIAS).
And here's the syntax:
external_links:
- redis_1
- project_db_1:mysql
- project_db_1:postgresql
You can give name for your container. If there is no container with the given name, then it is the first time to run the image. If the named container is found, restart the container.
In this way, you can reuse the container. Here is my sample script.
containerName="IamContainer"
if docker ps -a --format '{{.Names}}' | grep -Eq "^${containerName}\$"; then
docker restart ${containerName}
else
docker run --name ${containerName} -d hello-world
fi
You probably don't want to keep using a container that you don't know how to create. However, the good news is that you should be able to figure out how you can create your container again by inspecting it with the command
$ docker container inspect ID
This will display all settings, the docker-compose specific ones will be under Config.Labels. For container reuse across projects, you'd be interested in the values of com.docker.compose.project and com.docker.compose.service, so that you can pass them to docker-compose --project-name and use them as the service's name in your docker-compose.yaml.

Upgrade docker container to latest image

We are trying to upgrade docker container to latest image.
Here is the process i am trying to follow.
Let's say i have already pulled docker image having version 1.1
Create container with image 1.1
Now we have fixed some issue on image 1.1 and uploaded it as 1.2
After that i wanted to update container running on 1.1 to 1.2
Below are the step i thought i will follow.
Pull latest image
Inspect docker container to get all the info(port, mapped volume etc.)
Stop current container
Remove current container
Create container with values got on step 2 and using latest image.
The problem I am facing is i don't know how to use output of "Docker Inspect" command while creating container.
What you should have done in the first place:
In production environments, with lots of containers, You will lose track of docker run commands. In order to keep up with complexity, Use docker-compose.
First you need to install docker-compose. Refer to official documents for that.
Then create a yaml file, describing your environment. You can specify more than one container (for apps that require multiple services, for example nginx,php-fpm and mysql)
Now doing all that, When you want to upgrade containers to newer versions, you just change the version in the yaml file, and do a docker-compose down and docker-compose up.
Refer to compose documentation for more info.
What to do now:
Start by reading docker inspect output. Then gather facts:
Ports Published. (host and container mapping)
Networks used (names,Drivers)
Volumes mounted. (bind/volume,Driver,path)
Possible Run time command arguments
Possible Environmental variables
Restart Policy
Then try to create docker-compose yaml file with those facts on a test machine, and test your setup.
When confident enough, Roll it in production and keep latest compose yaml for later reference.

Passing a docker's image generated name to another container in docker-composer

I like the fact that image names are handled by docker-compose and I don't want to use container_name to specify a fixed one. But at the same time I need to pass the generated name to a sibling image's container (based on docker's image!) so that the sibling container can ask the host to create a container based on the recently named image! Does that make any sense?
Clarification
I'm trying to configure a RabbitMQ's docker instance. And it comes in two steps. First installing a plugin and once the plugin is installed, I need to add an exchange based on that. The important part is that these steps need to be run in sequence and not parallel (the exchange requires the plugin). Through my other question, I'm trying to somehow find the RabbitMQ's container name and send a docker exec -it command to it. And once this command returns, I need to run a new instance of Python image to run the rabbitmqadmin script to create an exchange within the RabbitMQ.
I know it sounds complicated but this is the only way I could find to configure a RabbitMQ without making my own image.

How to copy and rename a Docker container?

I have a docker container that I want to use to partition client access to a database. I'd like to be able to have one container per client. If I start multiple copies of the container they all have the same name, the only difference being the port the container is assigned to.
How can I copy/rename the containers in such a way that I can differentiate the container without having to consult a lookup table that matches the assigned port to the client?
The docker rename command is part of Docker 1.5. Link to commit:
docker github
I'm using docker 1.0.1 and the following allows me to rename an image:
docker tag 1cf76 myUserName/imageName:0.1.0
All containers have a uniq name. When you do docker ps You can see that the first column is the ID. You can then manipulate your containers with this ID.
You actually need this ID in order to perform any operation on the container (stop/start/inspect/etc..)
I am unsure of what you are trying to do, but for each client, you can start a new container and then link the container ID with your user ID.
At the moment, there is no container naming within Docker, so you can't name nor rename a container, you only can use its ID.
In future versions, naming for container will be implemented.

Resources