0

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)

chenghuayang
  • 1,424
  • 1
  • 17
  • 35
  • 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. – David Maze Jul 11 '21 at 14:03
  • @DavidMaze Thank you for the concept. This was a practice of Docker tutorial, so I'm still not familiar with it. – chenghuayang Jul 11 '21 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. – chenghuayang Jul 11 '21 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. – David Maze Jul 11 '21 at 16:40
  • @DavidMaze Thanks! I got it! So in your first comment, you meant that I can set a CMD after the run like `docker run -it python:3.8 bash`. Is this correct? – chenghuayang Jul 12 '21 at 03:18
  • Also, may I ask what does it mean for the "COMMAND" on the container list (also shown in my second block of code in my question)? – chenghuayang Jul 12 '21 at 03:18
  • 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`, https://github.com/docker-library/python/blob/dbf2083938bd54ddb0f8697c177d5ccfc927f20f/3.8/buster/Dockerfile – Hans Kilian Jul 12 '21 at 07:47

1 Answers1

1

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

Hans Kilian
  • 18,948
  • 1
  • 26
  • 35