with docker-compose there is a command / argument like „depends_on“ to define dependencies of multiple containers. Is there a similar option for Docker run? I haven’t found anything on the net right now, only detailed information about using docker-compose.
a)
My use case is about creation of a dependency for Telegraf and InfluxDB running as separate containers.
b)
My use case is about having several containers writing to the same InfluxDB container database, is this possible in general?
c)
Currently I am using (but I am not sure if this is the best way).
No. How would that work? In a Docker Compose file you can define the parameters of all containers. Then you can define dependencies, but without compose, you don’t have the definitions of the containers. You could implement in a shell script, but why would you do that instead of using Docker Compose which was designed to do that. On the other hand, a dependency is just the order of running the docker run commands, so you can have a simple script with multiple docker run after eachother. Still, I would use Docker Compose.
Of course. What is the reason of your doubt?
This is not dependency. Docker has no way to know how you wnt to run influxdb, so you need to run that first and run telegraf with the --net parameter to use the same loopback network interface (localhost) as influxdb.
I wrote about net, but link is similar. It will not start the dependency and link is actually a deprecated option. You should use user-defined networks. While net is to choose which network you want to attach to the container, link is allow the new container (the one that uses the link option) to use a hostname to access an other container. This works without links when you are using user-defined networks and you can use the container name as hostname. Whe you are using Docker Compose, you can also use the service name as hostname.
When it is important to start a container before an other. Just a simple example. Imagine that you have two services in a Docker Compose file. Both of them are using the same volume. Let’s call it just “data”. If you mount that volume into both containers, the fist container that starts will mount the volume and copy the content of the mount point to the host so the other container can see the same content. Let’s say these two containers are “php” and “httpd” and your application code is in the “php” container. If httpd starts first, it could initialize the volume and “php” would mount for example a single index.html over the application code.
Not only no, but you can’t do that. By default each container will have its own loopback network interface. (“localhost”). Using --net container:containernamemeans that the container will not have its own, but it will use the “localhost” of an other.
thank you (once again! ) for your detailed feedback, the time invested in your response and the quick help.
I appreciate your support very much.
For some projects I will now use docker-compose instead docker run - especially if containers have dependencies. I already tried in former times a few things with docker-compose but somehow docker run was feeling kind of more comfortable to me.
In parallel I will go in deep dives regarding the user-defined networks you mentioned.
…well…In the beginning, I really only wanted to test docker to have a say (as I preferred virtual machines at that time) - but in the meantime I want to use docker for everything …Therefore, such “beginner-compatible” explanations as these ones from you are all the more helpful. Thank you again for that.
Using docker-compose, do I have to build all containers at once or is it possible to create a “depends_on” to an already running container?
Example:
I have an InfluxDB container running already
When creation of the docker-compose for Telegraf and Grafana (both in the same docker-compose file), is there any way to make a dependency on the InfluxDB container? Or do I have to create a docker-compose file containing Telegraf, Grafana and InfluxDB?
Having an *.env file, can the same file be used for more services (e.g. InfluxDB and Grafana in configuration.env) or do I need to have a separate *.env file for each service (e.g. influxdb_config.env and grafana_config.env)?
You can add new services any time. When you run docker compose up -d it does everything for you. It won’t restart or recreate anything unless it is necessary (for example to add new environment variables to existing services)
You can use the same file if you want and if you don’t mind to add unused variables to services, but you need to keep in mind the followings
If you have passwords in variables then you share the password with containers that don’t need it and you might not want that for security reasons.
If you use the same env file for different containers, you can’t change the env file and recreate only one service, because all of the services will change
If recreating the containers is not a problem or you save only common variables in that common env file, then you can do that.
I take it as “good practise” then to use different *.env configurations (especially with regard to the security topics you mentioned or in the case of having different containers being affected with a change of the *.env file).
sudo docker-compose up -d
ERROR: Service 'telegraf' depends on service 'influxdb' which is undefined.
How can I define it in my docker-compose file? Pointing to a service which is a) already running and b) has no detailed part of the docker-compose service definition (see above)
Or you can use one .env (not *.env) which is automatically read by Docker Compose. These variables would not be addedd the the services as environment variables, but you can refer to these variables in the yaml:
environment:
- APP_PASSWORD
Why would you even do that? When I wrote you can add new services I meant you can create a compose file with one service and later add new services depending on the already added service.
depends_on is for services in the compose file so you can make sure when you start service B which depends on service A, then service A will start too before the other.
If you have a running container which is not a compose service that is like any other service on any remote machine that you want to be running before starting the new service. You can’t controll that. What you can do is creating a n entrypoint or a start command that waits for a TCP connection for example or anything that you can check.
Even if you use depends_on with the short syntax as you did, it will only depend on starting the other container not the application inside that contaier. If you want to change that, you can use healthchecks nd the long syntax of depends_on
Thanks again and 5 stars to you for providing such a beginner friendly explanation.
Now I got your point. So I can run docker compose (no “-”) to have v2 instead of v1 and when I add a service to the docker compose file I can just execute the command once again.
Yes, but this is not a feature of v2. It was always true. v2 is just the latest compose which works as a docker cli plugin. Docker Desktop contains it, but you need to install docker-compose-plugin on Linux host or download the binary from GitHub.