Latest news about Bitcoin and all cryptocurrencies. Your daily crypto news habit.
Once you’ve defined your images and Dockerfiles, you then may want to enter a container to run bash or other commands. There are a few different ways to do this depending on whether you are using docker or docker-compose.
Either use docker run to create a container and run the command in it, or use docker exec to run a command in an existing container. More explanation of their differences can be read in my post:- Docker run vs exec: deep-dive into their differencesDocker
Assuming you know the name of your container, or if you don’t run docker ps to list the processes running and copy the container name…
Use either docker run or use docker exec with the -i (interactive) flag to keep stdin open and -t to allocate a terminal.
docker exec -i -t container_name /bin/bash
Or to run one or many commands:
docker exec -ti my_container sh -c "echo a && echo b"
Meanwhile, in the world of docker-compose…
The equivalent commands docker-compose run and docker-compose exec automatically assign a terminal so you can just run the following to get an interactive prompt:
docker-compose exec container_name sh
The same structure applies to running any command:
docker-compose exec <container name> <command>
To run a string of commands together, they must be connected to a shell. For example:
docker-compose exec <name in yml> sh -c '<command 1> && <command 2> && <command 3>'
Now you should be able to use command line instructions within docker containers. For example you can navigate file structures, view files, copy or replace files within the container — very helpful for debugging or customising configuration files.
Read more from Ryan Davidson
- Docker run vs exec: deep-dive into their differences
- Published in Hackernoon — To boldly log: debug Docker apps effectively using logs options, tail and grep
- Published in Hackernoon — Clean out your Docker images, containers and volumes with single commands
- Published in freeCodeCamp — The ups and downs of docker-compose — how to run multi-container applications
- More of my posts on:Â Docker
Read more from Medium
- Preethi Kasireddy in freeCodeCamp — A Beginner-Friendly Introduction to Containers, VMs and Docker
- Aymen El Amri in Statuscode — My Docker Cheat Sheet
- Travis Reeder in Travis on Docker — Microcontainers — Tiny, Portable Docker Containers
Read more from the web
- Docker docs:Â exec
- Docker-compose docs:Â exec
- DevTools: How do I SSH into a running container? / How do I run a command in my container?
Run bash or any command in a Docker container was originally published in Hacker Noon on Medium, where people are continuing the conversation by highlighting and responding to this story.
Disclaimer
The views and opinions expressed in this article are solely those of the authors and do not reflect the views of Bitcoin Insider. Every investment and trading move involves risk - this is especially true for cryptocurrencies given their volatility. We strongly advise our readers to conduct their own research when making a decision.