skip to Main Content

I have one docker image which spins up a container for executing some task in a small time period. Container is exited as soon as the task is completed.

Below is the output from docker ps -a command

CONTAINER ID   IMAGE               COMMAND                  CREATED          STATUS                      PORTS                                         NAMES
40be32cb4299   88841cd3d4a7        "/home/test/testing-…"   40 seconds ago   Exited (0) 22 seconds ago                                                 beautiful_agnesi

Since the container is automatically exited in small time period, I can’t perform docker exec -it -u root 40be32cb4299 bash Output of exec command gives below error since container is exited.

Error response from daemon: Container 40be32cb4299 is not running 

Is there way for me to perform exec on this container for editing some files inside the same container in order to perform docker commit and save as new image ?

2

Answers


  1. There is no way to exec into a stop container. But, you really have workaround to achieve you aim, something like next:

    1. docker commit 40be32cb4299 old_image, this commit the old stopped container as an old docker image.
    2. docker run -it --entrypoint=/bin/bash --name=new_container old_image, this use the old image to start a new container. As the entrypoint has been override, so the new_container will not run your default entrypoint or command, then new_container won’t exit, and with -it, you are now in the new container.
    3. In new container, make file changes.
    4. Exit that new container, now you could use docker commit new_container new_image to commit your new changes to a new docker image.
    Login or Signup to reply.
  2. You can start a stopped container. To make this simple name your container

    docker run --name container_name image_name
    

    Start the container using the container name

    docker start container_name
    

    Now exec the rest of the commands

    docker exec $(docker start container_name) //bin/bash -c " your command"
    

    Just remember to run the container without –rm

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search