DEV Community

Cover image for How To Access The Commandline Interface Of Your Docker Container
Samuel K.M
Samuel K.M

Posted on

How To Access The Commandline Interface Of Your Docker Container

While working in docker you may feel as you are locked in a box and you cant do anything inside the box.However docker provides for a way to work inside a container using its cli commands.

Lets say we are running a mongodb image and would like to access the mongo shell. How do you do that?

  1. Simply Open A New Commandline interface different from where you have spinned up docker.
  2. Type
docker exec -it mongodb mongo
Enter fullscreen mode Exit fullscreen mode

This command is applicable to any docker container ,even those started using docker-compose. You just need to know two things:

  • The Docker Container name
  • The command that applies to that container
docker exec -it ContainerName ContainerCommand
Enter fullscreen mode Exit fullscreen mode

So in our prior example:

docker exec -it mongodb mongo
Enter fullscreen mode Exit fullscreen mode

mongodb is the name of our container
mongo is the name used to intialize the mongo shell

This command also applies to services started using the docker compose command .

Let say you are running a laravel php application on docker this would apply

docker exec -it Laravel php  artisan make:controller ShopController
Enter fullscreen mode Exit fullscreen mode

Notice :
Laravel is the name of my container which may vary depwnding on how you have named your container
php artisan make:controller ShopController is a cli command for Laravel

Oldest comments (0)