Can I run docker command from n8n?

Hi dear friends;
I install n8n on ubuntu server via docker-compose (no.1)
also I run another docker image (no.2) on this server.

Can I probably call another docker commands (no.2) from any node in n8n (no.1)?

Best regards;

Yes you absolutely can in multiple ways.

  1. If n8n is hosted on the same machine as your Docker instance, you could try the Execute Command node.

  2. If n8n is hosted on a different machine or inside Docker itself, you could try the SSH node.

  3. You could use the Docker API

Whichever way works best is up to you. Personally, I’d give the API a whirl: Docker Engine API v1.41 Reference

1 Like

Yes, you can.
Make sure you start the container executing the commands with bind mount /var/run/docker.sock so that you have access to the socket. This way you can run commands against the bind mounted socket as if you were executing them from the host.

1 Like

thanks @joeldeteves, @lu4t

In fact:

  • I installed my n8n in root/n8n/
  • After I read your posts here, I added bellow phrases under n8n service in docker-compose.yml:
    volumes:

      - /var/run/docker.sock:/var/run/docker.sock
  • I want run this command in Execute Command Node:
    docker run --env-file .env -v $HOME:/root/.********

with SSH Node everything work good but when I run via Execute Command Node it show bellow error:

Problem in node ‘Execute Command‘
Command failed: docker run --env-file .env -v $HOME:/root/.******** /bin/sh: docker: not found

That’s because the N8N container doesn’t have docker installed on it.

You should do a little reading on how Docker works. A simple way to do it might be to use a cURL command: How to query Docker socket using curl – sleeplessbeastie's notes - actually it might be better to use the http node for this rather than executing cURL manually.

1 Like

I’ve created a Dockerfile with the following:

FROM n8nio/n8n:latest

RUN apk update && apk add --no-cache docker-cli

RUN addgroup -g 116 docker && addgroup node docker

runned docker-compose build

my docker-compose.yml:

services:
  n8n:
    #image: n8nio/n8n:latest
    build: .
   ...
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock

hope this helps

Thank you for the guide!
At first it didn’t worked but then I found out that your addgroup -g <number> docker was the problem on my machine. Why? The docker group id (<number>) in the docker image must be in sync with the docker group id on the host machine which links the /var/run/docker.socket into docker container.

it can be found with getent group docker | awk -F: '{print $3}'