0

i'm need docker-api method that will allow me to run image as daemon.

Docker command for reference: docker run -t -d some_image

Does anyone know a solution?

2 Answers2

1

You don't need to do anything special. At an API level, docker run does three things:

  1. It creates a new container with most of the options you specify;
  2. It starts the container; and
  3. It attaches to the containers's stdin and stdout.

The GitHub project you link to has some fairly intricate examples. (Its API documentation doesn't say a whole lot beyond that; for example the Docker::Container docs mostly just list the methods without explaining what could go into the various hash parameters.) If you create and start the container, but don't attach to it, it will have the same effect of docker run -d of running the container "in the background".

# Lifted from https://github.com/swipely/docker-api

# Create a Container.
container = Docker::Container.create('Cmd' => ['ls'], 'Image' => 'base')

# Start running the Container.
container.start

# It is "in the background", unless you specifically #attach to it or
# #wait for it.
David Maze
  • 130,717
  • 29
  • 175
  • 215
  • We will get Exited status after `container.start` – Mykhailo Melnyk Nov 12 '19 at 11:56
  • If you run something like `ls` as the main container process as the example shows, it will in fact exit quickly. This is equivalent to `docker run -d alpine ls` (swapping in a standard Docker Hub image) and that will show “Exited (0)” in `docker ps -a` output for the same reasons. – David Maze Nov 12 '19 at 13:14
  • Sorry, but you probably misunderstood me. I need something after running the container,I could exec commands. – Mykhailo Melnyk Nov 12 '19 at 15:49
  • What does the container _do_? That should be its main process; you probably shouldn’t be programmatically trying to exec commands in it. – David Maze Nov 12 '19 at 16:06
  • Why not? I need to run the code that comes to me from users. – Mykhailo Melnyk Nov 13 '19 at 10:39
0

I find solution:

container = Docker::Container.create('Cmd' => ["tail", "-f", "/dev/null"], 'Image' => 'some_image')

container.start
$ docker ps -a
CONTAINER ID        IMAGE               COMMAND               CREATED             STATUS                      PORTS               NAMES

08564fe29591        some_image          "tail -f /dev/null"   20 seconds ago      Up 10 seconds                                   nervous_ardinghelli

Container not Exited after start, and you can make exec in this container!