Håkon Hægland
Håkon Hægland

Reputation: 40718

Get container id from tag name

I am trying to copy a file from a container using docker cp. I built the container using docker build -t math-gsl-ubuntu-2004 . and after running the container image with docker run a file /math--gsl/Math-GSL-0.41.tar.gz is generated in the container. To copy the file back I can do

$ docker ps
CONTAINER ID        IMAGE                  COMMAND                 CREATED             STATUS              PORTS               NAMES
b651512240f2        math-gsl-ubuntu-2004   "./entrypoint.sh 2.6"   3 hours ago         Up 3 hours                              boring_lamport
ee008e7b8f01        44f63ff22dea           "./entrypoint.sh"       3 hours ago         Up 3 hours                              heuristic_liskov

then I note from the above output that the ID is b651512240f2 and I can issue docker cp b651512240f2:/math--gsl/Math-GSL-0.41.tar.gz . from the host to get the file out of the container.

Now I want to determine the container ID programatically from the name math-gsl-ubuntu-2004:

$ docker inspect --format="{{.Id}}" math-gsl-ubuntu-2004
sha256:866d21bece6aaf63496a5b173cafa37a25e21d22f7d34c1bbae4c602526c419b

but this does not match the container ID of b651512240f2. What am I missing?

Upvotes: 0

Views: 438

Answers (2)

David Maze
David Maze

Reputation: 158647

If you use a docker run -v option, you can cause your process to directly write content out to the host directory:

docker run -v $PWD:/math--gsl ... math-gsl-ubuntu-2004

Note that this hides everything in that directory in the image and replaces it with the host directory content, and then starts the container, and writes after that are reflected in the host directory. If the workflow is what you say – the container generates the tar file – this will work for you and is less fragile than docker cp.

Upvotes: 1

G1Rao
G1Rao

Reputation: 480

command to build an image

docker build -t math-gsl-ubuntu-2004 .

It will create an image with name math-gsl-ubuntu-2004

After building an image, you have to create/launch a container out of it,

to do that

docker run -it --name mycontainer math-gsl-ubuntu-2004 bash

and then

docker cp mycontainer:/math--gsl/Math-GSL-0.41.tar.gz .


To Fetch container name

docker ps -a | grep <image:tag> | awk '{print $1}' | head -n 1

examples:

docker ps -a | grep ubuntu | awk '{print $1}' | head -n 1

docker ps -a | grep ubuntu:18.04 | awk '{print $1}' | head -n 1

Upvotes: 1

Related Questions