Docker Compose and execute the command when the container starts

I am trying to unravel the command command in docker. In my current docker-compose.yml, I have launched the prosody encoder image ( https://github.com/prosody/prosody-docker ) and I want to create a list of users when the container is actually running.

The container documentation states that the user can be created using the LOCAL, DOMAIN, and PASSWORD environment parameters, but this is one user. I need a list of users.

When reading some things on the Internet, it seemed that using the command option I should execute the commands in a running or running container.

 xmpp: image: prosody/prosody command: prosodyctl register testuser localhost testpassword ports: - "5222:5222" - "127.0.0.1:5347:5347" 

But this does not seem to work, I checked to start the container using docker exec -it <imageid> bash , but the user was not created.

Is it possible to execute a command in a running container using docker-compose or are there other options?

+6
source share
2 answers

The COMMAND instruction is exactly the same as what is passed at the end of the docker run , for example echo "hello world" in:

 docker run debian echo "hello world" 

The command is interpreted as the argument ENTRYPOINT image, which in the debian case is equal to /bin/bash . In the case of your image, it goes to this script . If you look at the script, your command will simply be passed to the shell. I would expect that any command that you pass will succeed, but the container will exit after your command completes. Note that the default command is installed in the Docker file on the CMD ["prosodyctl", "start"] , which, apparently, is a lengthy process that starts the server.

I'm not sure how Prosody works (or even what it is), but I think that you probably want to either display in a configuration file that contains your users, or set up a data container to save your configuration. The first solution would be to add something like:

 volumes: - my_prosodoy_config:/etc/prosody 

To the docker-compose file, where my_prosody_config is the directory where the configuration files are stored.

In the second solution, you may need to first create a data container, for example:

 docker run -v /etc/prosody -v /var/log/prosody --name prosody-data prosody-docker echo "Prosody Data Container" 

(The echo should end, leaving you with a stopped container with volumes configured for configuration and logs. Just make sure you don't docker rm this container by accident!)

Then in the docker-compose file add:

 volumes_from: - prosody-data 

We hope you can add users by running docker exec , as you did before, then running prosodyctl register on the command line. But it depends on how the prosody and the image behave.

+1
source

CMD directly linked to ENTRYPOINT in Docker (see this question for an explanation). Therefore, changing one of them, you should also check how it affects the other. If you look at the Dockerfile , you will see that by default the command launches prosody through CMD ["prosodyctl", "start"] . entrypoint.sh just passes this command, as Adrian mentioned. However, your command overrides the default command, so your prosody daemon never starts. Maybe you want to try something like

 xmpp: image: prosody/prosody command: sh -c prosodyctl register testuser localhost testpassword && prosodyctl start ports: - "5222:5222" - "127.0.0.1:5347:5347" 

instead of this. More elegant and somehow what the creator created (judging by the entrypoint.sh script) will be something like

 xmpp: image: prosody/prosody environment: - LOCAL=testuser - DOMAIN=localhost - PASSWORD=testpassword ports: - "5222:5222" - "127.0.0.1:5347:5347" 

To answer your last question: no, it is impossible (at the moment) to execute commands on a running container using docker-layout. However, you can easily do this with a docker:

 docker exec -i prosody_container_name prosodyctl register testuser localhost testpassword 

where prosody_container_name is the name of your container to run (use docker ps to display a list of containers).

+1
source

Source: https://habr.com/ru/post/986345/


All Articles