2

I'm using the following version of Docker

$ docker -v
Docker version 20.10.7, build f0df350

When I start my containers using "docker-compose up", I can see them all come up with

CONTAINER ID   IMAGE           COMMAND                  CREATED         STATUS         PORTS                                       NAMES
29e94e032fbb   maps_apache     "httpd-foreground"       2 minutes ago   Up 2 minutes   0.0.0.0:9090->80/tcp, :::9090->80/tcp       maps_apache_1
13a8f2f037e4   maps_client     "docker-entrypoint.s…"   2 minutes ago   Up 2 minutes   0.0.0.0:3001->3000/tcp, :::3001->3000/tcp   web-app
318dff5311a2   maps_web        "bash /my-app/entryp…"   2 minutes ago   Up 2 minutes   0.0.0.0:8000->8000/tcp, :::8000->8000/tcp   maps_web_1
b4a6914cc67a   postgres:10.5   "docker-entrypoint.s…"   2 minutes ago   Up 2 minutes   0.0.0.0:5105->5432/tcp, :::5105->5432/tcp   maps_postgres_1

I would like to exec into my docker container using the readable image name, so I tried this

$ docker exec -it maps_web sh
Error: No such container: maps_web

What's the proper way to exec into docker using the image name (as opposed to the container ID)?

5
  • 1
    We cannot exec into a container through the image name. The image is not the container; multiple containers could use the same image. Hence, this might not be a one-to-one mapping.
    – Turing85
    Oct 16, 2021 at 16:47
  • Did you mean to say "container name"?
    – Paolo
    Oct 16, 2021 at 16:48
  • 1
    Assign proper container names in the Docker-compose file. Eg. name your container maps_web. Problem solved without writing custom shell scripts.
    – Mike Doe
    Oct 16, 2021 at 16:51
  • docker-compose exec web will use the Compose service name without needing to manually assign a container name.
    – David Maze
    Oct 16, 2021 at 17:46
  • Thanks @David Maze. To formalize what I think you were describing, I ran "docker-compose exec web sh" and that allowed me to run shell commands from within the container.
    – Dave
    Oct 17, 2021 at 18:34

2 Answers 2

1

This is an alternative to the docker-compose suggestion in the comments above.

docker exec -it $(docker ps -aqf "name=maps_web_1") "sh"

$(docker ps -aqf "name=maps_web_1") grabs the container ID by searching for the name (per the entries in the far right column when running docker ps).

You can use regex patterns to be more specific, like "name=maps_web.*". The caveat here is that this may return multiple container IDs or none at all if you're not careful.

In your case, $(docker ps -aqf "name=maps_web") would actually work since there are no other container names prefixed with maps_web (it'll use the first one it finds).

UPDATE: you can remove the quotes around the command you're attempting to run if it's causing issues.

2
  • 1
    If you know the container name, you can just pass it directly as an argument; docker exec -it maps_web_1 sh. You shouldn't need to manually look up its ID in most cases.
    – David Maze
    Apr 1, 2022 at 16:20
  • That's much more elegant, thanks! Apr 1, 2022 at 16:35
0

DOCKER_IMAGE_NAME=mydockerimagename

docker exec -it $(docker ps -aqf "ancestor=$DOCKER_IMAGE_NAME"| head -n 1) /bin/bash

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.