Docker containers are created based on images. If you want to create a new container based on a fedora image and run a terminal on it you can do:
1 docker run -i -t fedora bash
Every time you execute this command a new container will be created based on the fedora image.
Most of the time we run docker containers with servers in a daemonized mode. Here is a very simple example:
1 docker run -i -d fedora bash
In this scenario, we know the container is running, but we can’t really interact with it anymore. If something is not working correctly and we want to debug why, we need to create a new container with a shell and try to reproduce all the steps that led to the error. Well, at least that was the way I used to do it.
Docker exec allows you to execute commands in a running container. It works very similar to docker run, but with a running container. Say we have a container running and we want to see which environment variables are defined in the container:
1 2 3 4 docker exec -i 46fbb818b9ca printenv PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin HOSTNAME=46fbb818b9ca HOME=/root
We can use the same technique open a shell to a running container:
1 docker exec -i -t 46fbb818b9ca bash