'start docker container interactively

I have a very simple dockerfile with only one row, namely "FROM ubuntu". I created an image from this dockerfile by the command docker build -t ubuntu_ .

I know that I can create a new docker container from this image an run it interactively with the command docker run -it my_new_container

I can later start this new container with the command start my_new container

As I understand it, I should also be able to use this container it interactively by start -i my_new container

But, it does not work. It just runs and exits. I don't get to the container's command prompt as I do when I use run. What am I doing wrong?



Solution 1:[1]

If i understood correctly, you want to see the logs from container in terminal, same as when you run the image with docker run. If that's the case, then try with

docker start -a my_docker_container

Solution 2:[2]

You can enter a running container with:

docker exec -it <container name> /bin/bash

example:

docker exec -it my_new_container /bin/bash

you can replace bash with sh if bash is not available in the container.

and if you need to explicitly use a UID , like root = UID 0, you can specify this:

docker exec -it -u 0 my_new_container /bin/bash

which will log you as root

Solution 3:[3]

Direct answer:

To run an interactive shell for a non-running container, first find the image that the container is based on.

Then:

docker container run -it [yourImage] bash

If your eventual container is based on an alpine image, replace bash with sh.

Technically, this will create a NEW container, but it gets the job done.

EDIT [preferred method]:

An even better way is to give the container something irrelevant to do. A nice solution from the VSCode docs is to put the following command into your service definition of the docker-compose.yml file:

services:
   my-app-service:
      command: ["sleep", "infinity"]
      # other relevant parts of your service def...

The idea here is that you're telling your container to sleep for some time (infinite amount of time). Ironically, this your container will have to maintain this state, forcing the container to keep running.

This is how I run containers. Best wishes to whomever needs this nugget of info. We're all learning :)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dharman
Solution 2 Ron
Solution 3