简体   繁体   中英

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM