15

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?

3
  • Conceptually it might be easier to think of a Docker container as a wrapper around some single process, and all of its data and runtime dependencies. What is the process your container is running?
    – David Maze
    Aug 31, 2020 at 1:38
  • Don't sure what you mean. The container is running ubuntu... Aug 31, 2020 at 8:22
  • "Ubuntu" isn't an application. A more typical setup is for a container to run Nginx, or PostgreSQL, or the Flask application you wrote; those aren't things that have interactive shells, and you don't need to "log in" to them. It's also very routine to delete and recreate a container: I'd use docker rm; docker run over docker start in almost all circumstances.
    – David Maze
    Aug 31, 2020 at 10:07

3 Answers 3

14

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

3

You can enter a running container with:

docker exec -it my_new_container /bin/bash

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


to attach to a running container later, use -a / --attach option:

docker start -a  my_new_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

6
  • Thanks for you comment. However, the container is not running. If I execute the command "start my_new container", the container starts and exits immediately. It is the same as if you execute "docker run --name mybox1 busybox" and then "docker start mybox1". (In the first case you could make it work interactively with "docker run -it --name mybox1 busybox", but in the second case I don't know how to do it.) Aug 31, 2020 at 0:49
  • If you want the container to not exit, you need to make sure that whatever main process is running is in the foreground and never exits..
    – Ron
    Aug 31, 2020 at 0:53
  • But sometimes I need to shut down my computer incl. the container, and want to be able to start it again interactively... Aug 31, 2020 at 8:21
  • So how do I do to start it interactively? What do you by "you need to make sure that whatever main process is running is in the foreground and never exits.." Thanks! Aug 31, 2020 at 9:06
  • You already know how to start a container interactively (e.g. with TTY). Get to know the Docs so you understand how Docker works. You cannot interact with something which has exited.. So you need to have the container run indefinitely, and then you can exec a command or shell to get into i, or attach to a running container.. But you nee to read the DOCs
    – Ron
    Aug 31, 2020 at 10:24

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.