Docker Runtime Behaviors

Docker is not your regular Virtual Machine. Think of this as more of an application than Virtual Machine. Docker behaves like your application runtime. It is actually an application packaged with OS and all necessary dependencies.

In the similar fashion, say if you are running a python or Java application, and your main executes a function only, then the application exists after the execution of the function. If you want to run your application as service, then you will need to write your main accordingly to keep it up and running.

Similarly, in Docker, if you have a service, because you have setup your main correctly, the docker, in this case, your service will stay up and running.

Having said that there are other ways to keep your docker running as well. Once way is to run the docker in interactive mode. However, if you have an EntryPoint that exits, then your docker will exit after the entry point execution.

Here are few ways to keep your docker running if you require

Method 1: run in an interactive mode

docker run -it imageid

Method 2: EntryPoint if you do not have a service setup in your main in your program and you want to test your setups

EntryPoint ["tail", "-f", "/dev/null"]


docker run -d imageid tail -f /dev/null

Method 3: Run to sleep in infinite loop

docker run -d imageid sleep infinity

Now you have docker running, what do you do next? You want to log into the docker. Here is how you login to the docker

docker exec -it imageid/runid /bin/bash 
# above will get you to bash shell

Coping files into Docker

This is not a recommended method; however, if you require to copy file into docker. following is the command.

docker cp file docker_image:/filelocation

Stopping a docker image

docker stop imageid