Salt run command on Docker container

Introduction

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 docker.run the salt state.

What is docker.run

Docker.run like the cmd.run 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:

onlyif
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.

unless
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.

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

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

failhardTrue
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.

replaceFalse
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.

forceFalse
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 docker.run

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

build_package:
  docker_container.run:
    - image: myuser/builder:latest
    - binds: /home/myuser/builds:/build_dir
    - command: /scripts/build.sh {{ 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/myscript.sh

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

Reference: https://docs.saltstack.com/en/latest/ref/states/all/salt.states.docker_container.html#salt.states.docker_container.run

You can find more salt manuals https://linuxnotes.org/category/salt-manuals/

Viva La Linux!

Leave A Comment