20

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?

Henrik Leijon
  • 1,277
  • 2
  • 10
  • 15
  • 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 '20 at 01:38
  • Don't sure what you mean. The container is running ubuntu... – Henrik Leijon Aug 31 '20 at 08: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 '20 at 10:07

3 Answers3

16

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
Dharman
  • 30,962
  • 25
  • 85
  • 135
scovic
  • 343
  • 2
  • 8
5

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

Ron
  • 5,900
  • 2
  • 20
  • 30
  • 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.) – Henrik Leijon Aug 31 '20 at 00: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 '20 at 00:53
  • But sometimes I need to shut down my computer incl. the container, and want to be able to start it again interactively... – Henrik Leijon Aug 31 '20 at 08: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! – Henrik Leijon Aug 31 '20 at 09:06
  • You already know how to start a container interactively (e.g. with TTY). Get to know the [Docs](https://docs.docker.com/config/containers/multi-service_container/) 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 '20 at 10:24
  • I've noticed that /bin/bash is not needed to enter interactive mode – Phil O Jan 18 '21 at 13:27