I have a working container (Status -> UP) and what I want is to be able to execute a command inside the docker container, if I open the console and do an ls for example, it is done on my machine but not in the docker container. And also how it would be done with docker-compose.
What you need is the command
exec
:Then:
It means that you can use this
exec
to execute a command in a running container. Therefore, if you wanted to list the files and folders:The flag
-i
is to run it interactively.Docker Compose is a tool for managing multiple containers. Remember that containers work with a
Dockerfile
and it is in this file that you run the commands for the container. Example of aDockerfile
:The commands are executed with
RUN
.To complement a little the information provided by @César I put the following examples:
1) Example ls -a
Description: The previous image shows how an ls -a can be executed inside the container called "maps". Not only can you run the command inside docker but you can also pass the parameters associated with the command as it works natively. In this case, you can see the directories located at the root of the operating system, including those that are hidden.
2) Example mkdir
Description: In the previous image you can see how a directory is created that is located in the root of the system executed within docker.
3) touch example
Description: The previous image shows how to create a file within docker by executing the touch command.
4) List of commands available in docker
Description: To know the available commands of the operating system in question (its distribution specifies as Debian for example) you can perform an ls to the bin folder located in the root of the system "/bin". It is important to mention that not all commands that are natively present in the operating system can be executed inside the container. A clear example would be the command "cd" (change directory)
5) Example of cd (command not available)
I hope to be helpful! All the best.
In case you want to use docker-compose, you should have a docker-compose.yml file with the configuration of the containers to be raised, in this case you go to the path where you have the docker-compose.yml file and follow the following syntax: docker-compose exec , in my case I have a container that calls postgres and I want to run sh so it would look like this: docker-compose exec postgres sh , here is my docker-compose.yml: