Outils pour utilisateurs

Outils du site


issue200:c_c

Ceci est une ancienne révision du document !


We continue from our last issue – we are still talking docker, so you can get a containerized application on your webserver. Hopefully you got stuck on your homework or the last question and did some internet research.

The reason “nothing” happened is because there is no application to run. The purpose of docker is to run an application and exit. An operating system is not an application. This does not mean we cannot interact with it. We can do so by tacking on a command or invoking something like an interactive terminal.

To interact with it, we specify -it for interactive terminal (see below) and we need to give it a command, like sleep, for instance. We will ask it to run bash, the command will run and you will be asked for your computer password. However, once typed in, you will be logged into a root prompt on the docker container. Notice the name change in the prompt. It now shows root@<container id> and not your machine name.

If we then run a command, it runs inside that container (see top right). Please also try this.

As you can see, I am running Ubuntu inside my docker container, inside Ubuntu Mate, inside VM (inception stuff, I know!!!). To exit from the container’s bash, simply type exit.

Though we specifically wanted to access the terminal inside the container this time, most of the time we would not want to. A container is designed to come up and run one application until it completes, or crashes. So type: sudo docker run ubuntu sleep 5 - and start counting to five. The container launches and sleeps for five seconds, then exits. This is the expected behaviour. However, in that five seconds, you could not do anything in your terminal. If you have not tried it, please go ahead. If you want to free up your terminal, you need to run the container in detached mode, and to no one’s surprise, you type “-d”. Now type: sudo docker run -d ubuntu sleep 30 and your terminal is free to type again. Yay! Now type: sudo docker ps - and look at the output. This behaves the same as typing the ps command in a terminal, only instead of running processes, you now get back running containers! I will use: ps -a so you can see all my containers, running or not.

From the ps output (above), we can see the command we ran under the command column. I kept my terminal narrow to get a screenshot that is readable, but please make your terminal full screen. If you type: sudo docker ps - and you get no output, it means that there are no running containers, they have completed their tasks and exited.

Let’s say you typed some malformed command and now your docker container is running and eating up resources, or in the real world, your container encountered some error and you need to kill it and restart your application. Well, for that we have the stop command.

If I wanted to stop that top container, I could stop it by container id or friendly name. So in my case, I’d type: sudo docker stop friendly_kalam. I want you to run a container, with any command that will keep it alive and then stop it. Notice how long it takes. This is the power of containers. Since you already know how to remove a container, I want you to remove yours. (Practice makes perfect)

Say we were planning ahead, and we did not want to run an image from docker hub, but we want to use it later, we can simply pull the image. This creates the image on your computer for future use. Try typing sudo docker pull nginx - and once done type: sudo docker images - then see what is there. So now, next time we want to run a nginx container, docker will look locally for the image and run it, instead of reaching out to docker hub and downloading the image first. If you pull an image you don’t need, you simply type: sudo docker rmi centos - and my centos image is deleted, obviously you will substitute “centos” with your downloaded image name.

We covered the run command, the pull command, the stop command, the ps command, rm command, images command, the rmi command and yes I know they are not all commands, but it is easy to take stock this way. This means you have the basic skills to reach out to docker hub, grab an image, run it or interact with it, and remove it when you are done. I suggest that you grab images of topics you are interested in. Want to learn more about Redis? Then grab that image. It is important to note: This will get boring really fast if you are just doing this parrot fashion from my ramblings. I have given you the basic tools to get started. Try to grab “official” images of things like nginx or Redis, as you do not always know what changes have been made to unofficial images. That said, I know there are some of you out there that would want to create your own images. We can look at that in the next issue and maybe even look at the docker registry. I don’t want to dive too deep into this subject, as I am not sure what kind of interest there is out there.

If you would like to know more about the topic, you know what to do, ping me on Telegram, say something in our FCM Telegram group, or e-mail misc@fullcirclemagazine.org

issue200/c_c.1703929023.txt.gz · Dernière modification : 2023/12/30 10:37 de auntiee