Create a Docker container that allows us to run Docker commands inside it.

Tiempo de lectura: < 1 minuto

Today, I bring you this tutorial born out of the need to execute Docker commands using the task scheduler Ofelia (https://devcodelight.com/automatizar-tareas-con-cron-desde-docker-con-ofelia)

To execute Docker commands within a container that is running with Docker on a local machine, the first thing we need to do is add a Docker client to our container.

To create the custom image, I use Alpine:

Use a lighter base image (Alpine Linux)

FROM alpine:latest

Install the Docker client

RUN apk --no-cache add docker

Define the default command to run when the container starts

CMD ["/bin/sh", "-c", "sleep infinity"]

And to launch the image, I’ll use this Docker compose:

version: "3"
services:
  docker_executor:
    build:
      context: .
    container_name: container_docker
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock

With this configuration, we can now execute Docker commands in this container. With the following command, you can run ‘docker ps’ and see which containers it has access to:

docker-compose exec container_docker docker ps

Thanks to this setup, I’ve been able to perform my automatic backups (https://devcodelight.com/hacer-una-copia-de-seguridad-de-mysql-o-mariadb-con-ofelia-cron-y-docker)

Leave a Comment