Docker series (Part 5): Getting into the container and do some basic changes
Tags: docker
Published: 2022-06-08 11:45:04

In this blog, we are going to use these

S![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iv0fxpfuysztagkxe47o.png)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/841u7eitt6ujad6m0243.png)
You can see their default commands

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/78o9d5gylhpgi38tbqry.png)

Also , you can see the format here

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oz0uqpdy8getgn67o1l6.png)
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.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c3np9haz5gzdi64lf7dn.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7fi1l5bbv8dvf00nnaiz.png)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0jyuv2sslme3lthsy80e.png)
You can now see what is inside the root file

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ypctdrzo02lctrilq0b1.png)
From here, I can do any common administrative things.
Now, lets get out of the shell

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ej0epmctxwmhpkzg35qg.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1clqotj3hl9za0u0raxi.png)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/recx36i12bnsvybzdbdl.png)
We can now update apt package manager etc . 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6f700quvodi17wwod9d7.png)
 
Lets do it in our container.


SO,![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k38uuzdmdi5hy3key67s.png)
 see. You can do all the things using these interactive shell commands. You may also install curl here. Lets do it.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l2fsoenbh5h48wxte5zt.png)
Lets ping to google now

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/leac1u14xv8fw9ngrf2y.png)
Lets get out of this container now. We are done

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p51gg3invafzdag2sk7v.png)

The container has been stopped but not removed yet.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qvw38ynyu4c0su12dfts.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w5zl1b7q324zigluwped.png) 
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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/njda9jzf3xx3yn1u4r29.png)
Lets exit it now & check the containers list

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9sdykowx8d71f6rcpgzu.png)
 
  
 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.


<img src="https://c.tenor.com/Tvd3VKwtse0AAAAC/cat-cute.gif">