1

I created a new container from a Python:3.8 image with this command: docker run -it --name first_container -v app_files:/appfiles python:3.8, and every time I have to run the interpreter within that container.

host@host_name:~$ docker run -it --name first_container -v app_files:/app_files python:3.8
Python 3.8.11 (default, Jun 29 2021, 19:54:56)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

I can only leave Python interpreter with exit() or ctrl + d. If I do that, I have to get back to the host@host_name, not root@first_container where I'd like to go.

>>> exit()
host@host_name:~$ docker ps -a
CONTAINER ID   IMAGE        COMMAND     CREATED          STATUS                     PORTS     NAMES
baf1d785d2ac   python:3.8   "python3"   3 minutes ago    Exited (0) 8 seconds ago             first_container

What I want is to run a new container and directly get into root@first_container to run something else. Then I can use the method from Regan to leave container without stopping it. Is this possible, or does this make any sense? (Still a newbie to docker)

7
  • 1
    A Docker container is a wrapper around a single process. You should probably set your image's CMD to actually launch the script, instead of starting a Python REPL. Once the process is exited, the container is done, and you can delete it (or docker run --rm ... to have it delete itself); there's no particular reason to "keep it alive" once the process it runs is done. Commented Jul 11, 2021 at 14:03
  • @DavidMaze Thank you for the concept. This was a practice of Docker tutorial, so I'm still not familiar with it. Commented Jul 11, 2021 at 14:10
  • @DavidMaze But is it OK to run/host a website for a single Docker container? In that case, the container must be running all the time. Commented Jul 11, 2021 at 14:12
  • 1
    Sure, that's a pretty common use case. The server will be the single process the container runs; as long as the server is still running, the container exists. There isn't usually an interactive shell of any sort involved in it. Commented Jul 11, 2021 at 16:40
  • 1
    @chenghuayang The command is what command the container ran when it started. If you look at the Dockerfile for the python:3.8 image, you can see that the last line is a COMMAND statement that runs python3, github.com/docker-library/python/blob/… Commented Jul 12, 2021 at 7:47

1 Answer 1

4

To override the default python command and run a shell instead, you specify the command after the image name. Like this

docker run -it --name first_container -v app_files:/appfiles python:3.8 /bin/bash

Sign up to request clarification or add additional context in comments.

2 Comments

What's the difference between /bin/bash and bash only?
/bin/bash is an absolute path. Only using bash requires that your path is set up correctly. Just bash will work and do the same in this case.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.