36

I'm familiar with LXC and wanted to try out docker. The issue I'm facing is that I can't find a way to just tell docker to start a container in the background, without executing a command. For example, with LXC I would do :

lxc create -t ubuntu -n my_container

lxc-start -n my_container -d

At this point I would have a running container I can use as any VM (ssh to it, install stuff in it ...) It seems that docker prevent this kind of usage. Am I missing something ?

rmonjo
  • 2,675
  • 5
  • 30
  • 37

4 Answers4

55

When I need to inspect a docker container that I've created that is having issues running the normal CMD in the Dockerfile, I comment out that command and replace with "sleep" command to just pause the container when it starts so I can log into it and inspect the installation.

In Dockerfile

CMD ["sleep","3600"]

To log into the running Docker instance

docker exec -i -t <Container ID> bash
GameSalutes
  • 1,762
  • 2
  • 18
  • 24
  • 2
    Feels silly to have to do this, but works a treat. Except it unceremoniously kicks you out after an hour! I now use `sleep infinity`. – Heath Raftery May 02 '19 at 02:10
  • 1
    For containers without the `sleep` command, you could use this: https://hub.docker.com/r/refstudycentre/scratch-base – rudolfbyker May 14 '21 at 18:06
  • 3
    I am new to docker but I am not sure I understand the benefit of doing this. Whatever is the CMD in your Dockerfile, you will able to create an interactive container from that image with `docker container run -it /bin/bash` So why the sleep command needed? – guibar May 25 '21 at 20:36
  • @guibar i don't get it too. "CMD sets default command and/or parameters, which can be overwritten from command line when docker container runs." https://goinbigdata.com/docker-run-vs-cmd-vs-entrypoint/ – Winand Jun 08 '21 at 08:58
  • @winand You are correct. What you proposed would work in that you could inspect the built container by running it with bash, but the container would stop when you quit the shell. The OP wanted to keep the container running as a VM so it would need to stay open in the background after he would close any terminals. But if all you need to do is inspect this is absolutely a simpler solution that I use all the time. – GameSalutes Jul 02 '21 at 20:37
  • @rudolfbyker: Scratch-base seems really useful, but when I use `docker run -d theimage:v1`, I get this error `docker: Error response from daemon: OCI runtime create failed: container_linux.go:380: starting container process caused: exec: "sleep": executable file not found in $PATH: unknown.` – Nav Jul 26 '21 at 16:01
  • What is `theimage:v1`? This works for me on docker 20.10.2: `docker pull refstudycentre/scratch-base; docker run refstudycentre/scratch-base` – rudolfbyker Jul 26 '21 at 18:22
20

With docker, from the CLI, you can't create a container without running a command on it. If you want to use the REST Api, you can call the 'create' endpoint without 'start'.

However, it wouldn't be any good for you I think.

In most case, you probably just want to run a container with bash docker run -t -i ubuntu bash and do stuff there. Once you did everything you needed, you can simply commit and run from this point.

Usually however, it is better to do one step at a time in order to keep a clear history. Take a look at the Docker builder :)

creack
  • 116,210
  • 12
  • 97
  • 73
  • Thank you ! They must have down strong lifting on the "containerized" OS !! running the `ps -ef` command inside a container only output the process I'm running. Any idea how they achieved this ? – rmonjo Jun 30 '13 at 20:50
  • It is the PID namespace. – creack Jan 09 '14 at 18:51
2

You can build a Docker image that includes a run command and other configuration, such that a docker run <image> will start the container. The easiest way to do this is with CMD from the Docker Builder. You'll need a recent version of Docker (> 0.4.6?).

Outside of using Docker Builder, check out the flags for docker commit and docker run (where the command arguments are optional).

gabrtv
  • 3,558
  • 2
  • 23
  • 28
  • 1
    the command arguments are indeed optional according to the doc, however, `docker run -d b750fe79269d` is complaining about "no command specified" – rmonjo Jun 30 '13 at 18:06
  • 1
    @rmonjo you get that error when there is no CMD specified in the Dockerfile. Simple add at the end of `docker run` what you want to run inside the container. e.g. bash – Filippo Vitale Jan 17 '14 at 05:39
0

Adding some more thoughts here as I was playing around with this myself.

Let's say I want to work with 3 docker containers :

blong@mycomputer:~$ docker run --name ubuntuContainer1 -itd ubuntu 
2ce602710fb9b84b6530e5a1072961627e91731aba8f8b019f346fc78df08d7c
blong@mycomputer:~$ docker run --name ubuntuContainer2 -itd ubuntu 
e32b0eb72456fc23222f3915c91afc77e06a7e37a073b11f7088fabe8fa4bf20
blong@mycomputer:~$ docker run --name ubuntuContainer3 -itd ubuntu 
40574f704dceb0378f48ebe01d014d598434093d649be13573911d9833d9825d

See that they keep running, even though I didn't ask to run /bin/bash explicitly

blong@mycomputer:~$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
40574f704dce        ubuntu              "/bin/bash"         2 seconds ago       Up 1 seconds                            ubuntuContainer3
e32b0eb72456        ubuntu              "/bin/bash"         5 seconds ago       Up 4 seconds                            ubuntuContainer2
2ce602710fb9        ubuntu              "/bin/bash"         8 seconds ago       Up 7 seconds                            ubuntuContainer1

I can shell into the containers

blong@mycomputer:~$ docker attach ubuntuContainer1
root@2ce602710fb9:/# 

I can execute commands (e.g. installing packages) in the container

root@2ce602710fb9:/# apt-get update

# ... omitting output

root@2ce602710fb9:/# apt-get install nodejs
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following extra packages will be installed:
  libc-ares2 libv8-3.14.5
The following NEW packages will be installed:
  libc-ares2 libv8-3.14.5 nodejs
0 upgraded, 3 newly installed, 0 to remove and 5 not upgraded.
Need to get 1912 kB of archives.
After this operation, 7538 kB of additional disk space will be used.
Do you want to continue? [Y/n] Y

# ... omitting output

Processing triggers for libc-bin (2.19-0ubuntu6.6) ...
root@2ce602710fb9:/# nodejs --version
v0.10.25

Afterwards, I can exit, and keep everything running by pressing CTRL-p CTRL-q

root@2ce602710fb9:/# blong@mycomputer:~/$ 
blong@mycomputer:~/$ 

See also:

Community
  • 1
  • 1
blong
  • 2,815
  • 8
  • 44
  • 110