41

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 ?

1
  • The container (a Linux namespace) has to be built around a process, so there always has to be something running iin it a for namespace to exist. That is also the reason behind the pause binary (and pause container) in Kubernetes stackoverflow.com/questions/48651269/… Commented Jan 26 at 18:23

4 Answers 4

58

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
Sign up to request clarification or add additional context in comments.

7 Comments

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.
For containers without the sleep command, you could use this: hub.docker.com/r/refstudycentre/scratch-base
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 <image> /bin/bash So why the sleep command needed?
@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." goinbigdata.com/docker-run-vs-cmd-vs-entrypoint
@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.
|
23

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 :)

2 Comments

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 ?
It is the PID namespace.
2

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:

Comments

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

2 Comments

the command arguments are indeed optional according to the doc, however, docker run -d b750fe79269d is complaining about "no command specified"
@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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.