Salt run command on Docker container


From time to time We need to run a single command on docker instance. It can be some script that requires nodejs or some additional software. If you don’t want to install it on the server or if you need to run in on a couple of instances you can use salt and run the command with docker using state. For these purposes, We’re using the salt state.

What is like the state, but only for Docker. It does the equivalent of a docker run and returns information about the container that was created, as well as its output.

This state accepts the same arguments as docker_container.running, with the exception of watch_action, start, and shutdown_timeout (though the force argument has a different meaning in this state).

In addition, this state accepts the arguments from docker.logs, with the exception of following, to control how logs are returned.

Additionally, the following arguments are supported:

A command or list of commands to run as a check. The container will only run if any of the specified commands returns a zero exit status.

A command or list of commands to run as a check. The container will only run if any of the specified commands returns a non-zero exit status.

A path or list of paths. Only run if one or more of the specified paths do not exist on the minion.

If True, run container in background and do not await or deliver its results.

If True, the state will return a False result if the exit code of the container is non-zero. When this argument is set to False, the state will return a True result regardless of the container’s exit code.

If True, and if the named container already exists, this will remove the existing container. The default behavior is to return a False result when the container already exists.

If True, and the named container already exists, and replace is also set to True, then the container will be forcibly removed. Otherwise, the state will not proceed and will return a False result

How to run salt command on docker container with

To run the single command on docker instance with salt We’re using the following state:

    - image: myuser/builder:latest
    - binds: /home/myuser/builds:/build_dir
    - command: /scripts/ {{ pkg_version }}
    - creates: /home/myuser/builds/myapp-{{ pkg_version }}.noarch.rpm
    - replace: True
    - networks:
      - mynet
    - require:
      - docker_network: mynet

The following state will run the build of some package version on the user directory. Also, it can be used via CLI:

salt '*' docker.run_container myuser/myimage command=/usr/local/bin/

That’s all. In this article We described how to use salt utility and how to run a command in the container.


You can find more salt manuals

Viva La Linux!

Leave A Comment