3

I am storing container ID in my database, based on that I regularly check if my container is still running or has stopped using 'Docker Inspect' I update this info in my database.

The above method is fine if I only want to update the status but if someone wants to change the env. variables and use the docker run command to create the container then a new container ID gets generated.

So my question is if it is possible to generate the new docker container with the container Id it had previously?

Thamer
  • 1,896
  • 2
  • 11
  • 20
RajieRoo
  • 121
  • 1
  • 9
  • Could you base this on container *name* instead (i.e. `docker run --name [...]`). – Oliver Charlesworth Nov 01 '18 at 09:11
  • unfortunately no, because in my Database only container ID is being stored, but i can store container name in my Database too, is it possible to inspect a docker container based on container name in JAVA ? – RajieRoo Nov 01 '18 at 09:13
  • Related: [How the docker container id is generated](https://stackoverflow.com/questions/33012659/how-the-docker-container-id-is-generated). – tgogos Nov 01 '18 at 09:18

1 Answers1

1

It looks like there is no option to "manually" set a UUID when you run a container. This is something that the Docker Daemon takes care of.

Container identification

The operator can identify a container in three ways:

| Identifier type       |                              Example value                        |
|===========================================================================================|
| UUID long identifier  | "f78375b1c487e03c9438c729345e54db9d20cfa2ac1fc3494b6eb60872e74778"|
| UUID short identifier | "f78375b1c487"                                                    |
| Name                  | "evil_ptolemy"                                                    |

The UUID identifiers come from the Docker daemon. If you do not assign a container name with the --name option, then the daemon generates a random string name for you. Defining a name can be a handy way to add meaning to a container. If you specify a name, you can use it when referencing the container within a Docker network. This works for both background and foreground Docker containers.

tgogos
  • 23,218
  • 20
  • 96
  • 128
  • in my java code, I am using DockerClient , which has a function to inspect a container using container ID, there is no such function for container Name :/ – RajieRoo Nov 01 '18 at 09:48
  • I'm sure there will be a way to programmatically take the `name` value of your container with java. I'd post a new question for that though... – tgogos Nov 01 '18 at 09:55
  • 1
    Ya i am thinking to pass the name instead of ID inside that function and see if that works, otherwise I will post a new question. – RajieRoo Nov 01 '18 at 09:59
  • If you are using the docker client from [github.com/spotify/docker-client](https://github.com/spotify/docker-client), it seems easy... `final ContainerInfo info = docker.inspectContainer(id);` – tgogos Nov 01 '18 at 09:59