DOCKER - 13 Have a Shell in a Container

Hello everyone and welcome to this introductory course to Docker. For this course, you need to know how to start a container from previous courses. It will simply be a question of entering a container to manipulate it from the inside. There are a few commands we can use. The first is a container run with -it. We'll talk about how it works to use shell in a container. Then to run the containers you want to run a second process on, you can also use Container exec, and we'll show several examples. Next, we will mention some differences between a classic Linux distribution, such as Ubuntu, for installation on a virtual machine or on physical. And then what do these Linux distros look like in a container.
Let's start. Looking into a container from the outside is sometimes very convenient. But how to enter the container and how to access it live from the command line. We have at our disposal several commands that allow us to obtain a shell inside the container itself while it is running. The first is simply the docker container run command. But we will specify a few more options. We are going to do an –it, then an nginx. Let's call it a proxy. Next, we'll need to specify another command. When you run a docker container you have optional commands and arguments you can send to that new container you're about to launch to tell it what to run. The image contains a default command that we will see later how to configure. But if you want to modify what was executed at startup, you can do it directly from the execution line. Let's just say Bash. Because Bash is one of the most common shells you'll usually find in a Container, which will give you a shell to work with. I am now in a command prompt. You'll notice that my prompt says root, which is the user the container started as. That doesn't mean I'm root on the host. It just means I'm acting as root on the container. This number after is actually the identifier of the container. If I do an ls -al, I get a full list of everything that is where I am. And you can see that I'm at the root of the filesystem inside the container. Actually I can see all the files inside the container which is based on the Nginx image. From there, I could change the configuration, download packages from the Internet, and do just about any common administrative operation from a normal shell. To exit the shell when you're done, I'm going to type exit, like you would with any shell, and now that container is stopped. If we check with an ls docker container, we won't see it here. With an -a. We see it
Now look at the command he executed. The default command for an Nginx container is to run the Nginx program itself. But when we typed our last command, we changed the default program to be a Bash, which gave us our shell. When we exited the shell, the container stopped. Because the container only runs as long as the command run at startup is running. Since we changed it to Bash, you just have to quit Bash, To quit it Now, what would happen if I used an image of Linux instead of using Nginx? Let's do the same thing but with an Ubuntu It will first download the Ubuntu image and put me in the command prompt of this new container. If you've used Ubuntu before, you're familiar with the apt package manager. So I can use that here. Apt-get update Just like I would in a standard Ubuntu server. I could actually install something with apt-get install –y curl. What's special about Ubuntu and other distros inside a container is that they are usually very small. A CD or downloaded ISO image of Ubuntu that you would normally place on a virtual machine will contain a lot more software installed by default than an Ubuntu container which is really a very basic version of Ubuntu. You can always add more software with the apt package manager.
So now this container has curl installed (Curl google.Com) and I can use it as I would on a physical machine. Once I exit the shell again, it will stop the container. Dockercontainer ls. You notice that we don't see it. Docker container ls -a And here is the Ubuntu image we just launched. If I would relaunch that container, it would still have curl installed. But if I created a new container from the Ubuntu image, the curl command for that different container would not be installed. And if we wanted to relaunch that container and put Bash back in it again, We could use the docker container start command. It's a little different when we launch an already existing container, we add a -ai. Then the name of our container, so Ubuntu. You'll notice we're back inside and I can curl Google and it works. If I exit, it stops it again.
What now if I actually want to have my shell in a container that is already running and running something like MySQL or Nginx. We can do that too and that is by using the exec command. docker container exec --help And you can see our few options here. Suppose I want to access the MySQL container because I need to perform administrative tasks on MySQL itself. I could redo –it then the container name so mysql. Then the program I want to run. And in this case again, it's Bash and I'm now in the container with MySQL. I can of course, as on ubuntu, launch commands, updates, etc. You'll notice that when I quit this Bash and do an ls, MySQL is still running. Since docker container exec actually launches an additional process on an already existing running container, it will not affect the original MySQL process. Let's do one more test with an Alpine container. Now what is Alpine? Alpine is another Linux distribution, but Alpine is designed to be very small: its size does not exceed 5 MB. If I do an alpine pull docker, the latest Alpine image will be downloaded and if I do an ls docker image, you can see how small the Alpine image is compared to the Ubuntu one. For now, let's do a Bash shell in Alpine. Docker container run -it alpine bash. Now what just happened? It tells me that the container process is starting and bash cannot be found. This means that Bash is not in the container. And that brings us back to the concept that we can only run elements of the container that already exists in its image when you start it. Or maybe something you added later via exec or run commands. But in this case, how would I get into the alpine container? It doesn't contain Bash, but it does have sh. We can therefore replace the command with sh, which is not as complete as Bash, but allows us to enter directly into the container. If you searched for Alpine on the internet, you would learn that its package manager is apk and we could actually use it to install Bash if we really needed it.
To recap what we have learned in this lesson. We've heard of the -it option for running the container, which gives us the equivalent of an interactive shell for doing things in the container. We then learned a new command for docker container exec with the same result as run, but does it on an existing container that is running, it does not start a new container. Finally, we learned a bit more about Alpine and how it differs from major Linux distros and the actual size of 4MB which is ideal for container images.
That's all for this video but see you soon for the next one.