In this blog, we are going to use these
S
So, basically when we run a container using any image, we use it's default commands.
SO, we previously created these 2 containers
You can see their default commands
Also , you can see the format here
SO, till now, we did not change the commands . But now, to interact and work within the container , we will use some commands . We will use
docker container run -it --name <container name> <image> <command>
for example, here we are using
docker container run -it --name container_3 nginx bash
So, why are we using these? Right!
Let's see what this -i and t means of -it . SO, if you run with -it, it gives you a terminal inside the container to work on.
OKay, let's get back to where we were. Now, you can see a terminal with root & some id , right?
This root means you are acting as root of the container & the ID is the container ID
You can now see what is inside the root file
From here, I can do any common administrative things.
Now, lets get out of the shell
What do you think now? The container is now running or not?
Yes, you are right. It is not running. It has been stopped
So, what basically happened?
Actually you created an interactive container and used bash. Now, when you stopped using the bash shell it stopped. It will run till the moment you are running that bash. OK?
Now,lets create another interactive container. This time we will run a ubuntu image
We can now update apt package manager etc .
Lets do it in our container.
SO,
see. You can do all the things using these interactive shell commands. You may also install curl here. Lets do it.
Lets get out of this container now. We are done
The container has been stopped but not removed yet.
So, you have another option to work with containers. You can work with any running containers using
docker container exec
But before using them, first make sure that the container is running. In our case, our containers are not. Lets start one.
Use this command
docker container start <container name>
Lets start our container which used mysql image . That is container_2
Now it is a running container and you can use exec command with it to interact
docker container exec -it <container name> <comamnd>
exec for running containers
-it to access command lines or terminal for that container
Lets exit it now & check the containers list
Look! The container is still running. while we used just -it , when we exited , the container stopped but now, using exec does not make it disappear.
SO, that is how we can work with existing containers.
Top comments (0)