Question

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 ?

Was it helpful?

Solution 2

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

OTHER TIPS

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

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

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:

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top