1

我正在使用以下版本的 Docker

$ docker -v
Docker version 20.10.7, build f0df350

当我使用“docker-compose up”启动我的容器时,我可以看到它们都想出了

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

我想使用可读的图像名称执行到我的 docker 容器中,所以我尝试了这个

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

使用映像名称(而不是容器 ID)执行到 docker 的正确方法是什么?

4

1 回答 1

0

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).

于 2022-03-01T02:11:06.450 回答