1

I created the following simple dotnet core console application from Visual Studio 2019.

Console.WriteLine("Hello World!"); 
var readText = Console.ReadLine(); 
Console.WriteLine(readText);

Then I add docker support to this console project. I have written the step by step instructions in another so question to add the docker support.

In a command prompt, I now navigate to the folder where the docker-compose file is present and issue the following command

docker-compose run <service name from docker-compose.yml file>

Specifically for my case it would be

docker-compose run dokconsoleapp

Where dokconsoleapp is the service name defined inside of docker-compose file.

This builds the image and my console app is launched and run inside of the container interactively.

And when the app exits, the container stops. Now when I want to run again, I issue the same command again. The app runs as expected. But curiously the app is launched in a new container, not reusing the existing stopped container.

Repeated Docker-Compose Run Creating new Containers

So I run the command three times and each time a new container is created, see in docker desktop.

Docker-Compose Run Launching Multiple Containers as seen in Docker Desktop

Now my question is, I am just curious, is there way here with docker-compose run, to reuse the existing container, rather than creating new ones.

Note that the command

docker-compose up 

does not work as the container it creates is not interactive. So I have to use 'run' only. More details here.

VivekDev
  • 20,868
  • 27
  • 132
  • 202
  • "When the app exits, the container stops." That is generally how Docker works; why is it a problem for your application? Can you include some of the Docker-related artifacts in your question (like for example whether there are `volumes:` in your `docker-compose.yml` file)? – David Maze Jun 04 '20 at 10:56

1 Answers1

2

Using docker-compose run $SERVICENAME will always creates a new container as mentioned in the documentation.

Ref:- https://docs.docker.com/compose/reference/run/

Commands you use with run start in new containers with configuration defined by that of the service, including volumes, links, and other details. However, there are two important differences.

I checked your other post, as you are trying to get an interactive shell with a particular service, you can achieve in the following manner. Have a look at the example docker-compose.yml.

version: '3'
services:
  test:
    container_name: test
    image: 'busybox'
    command: sleep 5d

Run docker-compose up -d

-d => Run in detached mode

command: sleep 5d according to what container will be running for 5 days and then after that and then exec into the container with following command

$ docker exec -it test /bin/sh 

and then you will not get new container every time.

Take a look at this article as well:- https://docs.docker.com/compose/compose-file/#domainname-hostname-ipc-mac_address-privileged-read_only-shm_size-stdin_open-tty-user-working_dir

nischay goyal
  • 3,206
  • 12
  • 23