Anthony
Anthony

Reputation: 35928

How to gain bash access to a docker container after its ran

My dockerfile looks like this:

FROM my/ubuntu:latest

RUN apt-get update \
 && apt-get install -y build-essential cmake pkg-config wget \
 libjpeg8-dev libtiff4-dev libjasper-dev libpng12-dev \
 libavcodec-dev libavformat-dev libswscale-dev libv4l-dev \
 libatlas-base-dev gfortran \
 python \
 python2.7-dev \
 && wget https://bootstrap.pypa.io/get-pip.py \
 && python get-pip.py \
 && pip install numpy

I build the image like this

docker build -t my/ocr:latest docker-ocr

I run the image like this

docker run -d --name ocr my/ocr
6cb4d2408ced5b5b3c68f3f5b236784c2ec3ba780592104c7a7651620bd3bd75

However, when I try to bash into the container - it says it isn't running. The reason why I want to bash into it is because I want to install OpenCV. So I want to bash into it first to manually execute commands to ensure they are working properly so that I can put them in my docker file.

▶ docker ps -a
CONTAINER ID        IMAGE                   COMMAND                  CREATED             STATUS                     PORTS                    NAMES
6cb4d2408ced        my/ocr            "/bin/bash"              9 seconds ago       Exited (0) 9 seconds ago                            ocr
74c1f48e98ad        my/tomcat:7.0     "/run.sh"                2 days ago          Up 2 days                  0.0.0.0:8080->8080/tcp   tomcat7
db3f66a2d97e        my/mysql:latest   "/sbin/entrypoint.sh "   3 days ago          Up 3 days                  0.0.0.0:3306->3306/tcp   mysql

Since the container isn't running I can't seem to bash into it:

▶ docker exec -it ocr bash
Error response from daemon: Container ocr is not running

Question

How can I gain shell access to this container so that I can bash into it using

docker exec -it ocr bash

Upvotes: 0

Views: 1765

Answers (1)

christian
christian

Reputation: 10188

You could start the container with

docker run -it --name ocr my/ocr bash

and get a shell directly.

If you don´t want that, you could start your cotainer with

docker run -d --name ocr my/ocr sleep infinity

and then exec into your container:

docker exec -it ocr bash

Upvotes: 4

Related Questions