Venkat Ch
Venkat Ch

Reputation: 1268

Is it possible to get bash access in NOT running container?

I able to run Flask API container successfully. But during the app execution it fails and stops the container for some reason.

I do checked container logs and noticed some file missing error is coming. Now I want to debug what file is missing by accessing /bin/bash of stopped container. But it throws an error saying container is not running.

docker exec -it CONTAINER /bin/bash

Is there any workaround to access bash in the STOPPED container?

Upvotes: 9

Views: 10338

Answers (4)

Ivhani Maselesele
Ivhani Maselesele

Reputation: 168

No, you cannot.

However, it might be useful to either check the logs or specify bash as an entry point when doing a docker run

Checking logs: https://docs.docker.com/config/containers/logging/

docker logs  <CONTAINER_NAME>

Shell Entry point: https://docs.docker.com/engine/reference/run/#entrypoint-default-command-to-execute-at-runtime

docker run --name <CONTAINER_NAME> --entrypoint /bin/bash <IMAGE_NAME>

If you container does not have /bin/bash, try

docker run --name <CONTAINER_NAME> --entrypoint /bin/sh <IMAGE_NAME>

Upvotes: 4

Raman Sailopal
Raman Sailopal

Reputation: 12867

From the man pages from docker-run:

      --entrypoint=""
      Overwrite the default ENTRYPOINT of the image

So use something like:

 docker run --entrypoint=/usr/bin/sleep 1000 ......

This will start the container and wait 1000 seconds, allowing you to connect and debug.

Upvotes: 0

Krishna Chaurasia
Krishna Chaurasia

Reputation: 9552

You can try to use the docker commit command.

From the docs:

It can be useful to commit a container’s file changes or settings into a new image. This allows you to debug a container by running an interactive shell, or to export a working dataset to another server.

Resource with a example:

We can transform a container into a Docker image using the commit command. All we need to know is the name or the identifier of the stopped container. (You can get a list of all stopped containers with docker ps -a).

docker ps -a
CONTAINER ID   IMAGE   COMMAND     CREATED         STATUS                    NAMES
0dfd54557799   ubuntu  "/bin/bash" 25 seconds ago  Exited (1) 4 seconds ago  peaceful_feynman

Having the identifier 0dfd54557799 of the stopped container, we can create a new Docker image. The resulting image will have the same state as the previously stopped container. At this point, we use docker run and overwrite the original entrypoint to get a way into the container.

# Commit the stopped image
docker commit 0dfd54557799 debug/ubuntu

# now we have a new image
docker images list
REPOSITORY    TAG     IMAGE ID       CREATED         SIZE  
debug/ubuntu  <none>  cc9db32dcc2d   2 seconds ago   64.3MB


# create a new container from the "broken" image
docker run -it --rm --entrypoint sh debug/ubuntu
# inside of the container we can inspect - for example, the file system
$ ls /app
App.dll
App.pdb
App.deps.json
# CTRL+D to exit the container

# delete the container and the image
docker image rm debug/ubuntu

Upvotes: 9

user9733940
user9733940

Reputation:

You can't because this container is dead as well as turned down virtual machine is dead. You can check logs using docker logs command.

docker container ls -aq

docker logs <name_of_your_dead_container>

Upvotes: 1

Related Questions