Radwan Nizam
Radwan Nizam

Reputation: 131

How to access a Docker container instance using the image name?

Each time I want to access a docker container I have to run the command

docker ps

The command show the id of the running container, after that, I have to copy the container id and use it in the following command :

docker exec -it /bin/bash

Is there a way to avoid searching for the container id each time I want to access that container.

Upvotes: 3

Views: 5367

Answers (4)

Prabhat Singh
Prabhat Singh

Reputation: 976

As you said that you need to copy container Id every time whenever you want to connect to that container is because either you haven't assigned any name to that container or it's picking up something default .

For Example : I am going to run a centOS image and want name it as dev-centos-1

You can write a docker file for this or run this following command to up your container

docker container run --name dev-centos-1 -d centos:latest

once this container is up you can do everything by it's name :

docker stats dev-centos-1

docker logs dev-centos-1

or even connect to it bash :

docker exec -it dev-centos-1 bash

This is always considered as a better way to manage your containers in your environments .

Thanks .

Upvotes: 1

Johnny Bui
Johnny Bui

Reputation: 213

You have to name your container and specify container name in the docker exec command, not image name.

So you have to add --name=CONTAINER_NAME into your docker run command and when you want to manage it just use docker exec -it CONTAINER_NAME bash.

If you use docker-compose to run your container, add container_name: CONTAINER_NAME under your service block to name it.

Example:

version: '3.1'

services:
  server:
    image: nginx:latest
    container_name: nginx-server
    restart: always
    ports:
      - 80:80

Upvotes: 2

Yash Jagdale
Yash Jagdale

Reputation: 1508

You can do docker exec with container name as well. for example

root@qualys-virtual-machine:~/alpine-node-docker# docker ps
CONTAINER ID        IMAGE                                  COMMAND                  CREATED             STATUS              PORTS                    NAMES
fc5932f7ff9c        selenium/node-chrome:3.14.0-gallium    "/opt/bin/entry_poin…"   5 days ago          Up 5 days                                    root_chrome_3
8caa58ce6056        selenium/node-chrome:3.14.0-gallium    "/opt/bin/entry_poin…"   5 days ago          Up 5 days                                    root_chrome_5

now I can do docker exec with container names like root_chrome_3

docker exec -it root_chrome_3 /bin/bash

to give name --name in run command.

Upvotes: 4

Radwan Nizam
Radwan Nizam

Reputation: 131

I can use the folloiwng bash script :

#!/bin/bash
container_id=$(docker ps|grep <your container image name>|cut -d' ' -f1)
echo $container_id
cmd="docker exec -u 0 -it "$container_id" /bin/bash"
echo $cmd
exec $cmd

In my case, I stored the script in a file go_to_my_container.sh. You can run the following command to access your container:

sh go_to_my_container

Upvotes: 3

Related Questions