DEV Community

Shahriyar Al Mustakim Mitul
Shahriyar Al Mustakim Mitul

Posted on

Docker series (Part 5): Getting into the container and do some basic changes

In this blog, we are going to use these

SImage description
So, basically when we run a container using any image, we use it's default commands.
SO, we previously created these 2 containers
Image description
You can see their default commands

Image description

Also , you can see the format here

Image description
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>
Enter fullscreen mode Exit fullscreen mode

for example, here we are using

docker container run -it --name container_3 nginx bash
Enter fullscreen mode Exit fullscreen mode

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.

Image description

Image description
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

Image description
You can now see what is inside the root file

Image description
From here, I can do any common administrative things.
Now, lets get out of the shell

Image description

What do you think now? The container is now running or not?
Yes, you are right. It is not running. It has been stopped

Image description
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

Image description
We can now update apt package manager etc .

Image description

Lets do it in our container.

SO,Image description
see. You can do all the things using these interactive shell commands. You may also install curl here. Lets do it.

Image description
Lets ping to google now

Image description
Lets get out of this container now. We are done

Image description

The container has been stopped but not removed yet.

Image description

So, you have another option to work with containers. You can work with any running containers using

docker container exec
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

Lets start our container which used mysql image . That is container_2

Image description
Now it is a running container and you can use exec command with it to interact

docker container exec -it <container name> <comamnd>
Enter fullscreen mode Exit fullscreen mode

exec for running containers
-it to access command lines or terminal for that container

Image description
Lets exit it now & check the containers list

Image description

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)