Getting a shell-like access into a container:

To get an interactive shell inside an image we use the below command:

  • -i: interactive mode : allows a session to remain open
  • -t: gives us a kind of terminal like an ssh connection into the container.
Running a command at the start of a container:


Images have default commands that run at the start of the container, these commands can be configured in the dockerfile.
We can run a command of our choice at the start of the container using the command parameter of the run command.

Example:

The below command gives us an error:


docker: Error response from daemon: OCI runtime create failed: container_linux.go:349: starting container process caused "exec: \"ps\": executable file not found in $PATH": unknown.
ERRO[0000] error waiting for container: context canceled 

Which means that the "ps" program we try to run at the start of the container does not exist in the image of the container which is debian
We can only run programs that already exist in the debian image.

Starting and stopping containers:

Starting a stopped container:

  • -a: means attached mode. The container attaches its output to your terminal, it allows us to view the output of the container.
Running a command inside a running container:

We use the below command to run a command inside a running container:


If we try to run the command in a stopped container we get an error. We stop a container using the below command


We check if the container is still running using:


We shouldn't see our container in the list because we have stopped it.
We then try to run the "ls" command in the stopped container:


We get the below error:

Error response from daemon: Container 73310e25a065ee6454d7ac2b5e60ba9b9b34fb672cfa3045c1781ac60550e3c is not running

Brando Sabatini and Ikbal C ©. Powered by Blogger.