Aula 14 - Iniciando um Container Ubuntu | Curso Docker Essencial: Primeiros Passos

Aula 14 - Iniciando um Container Ubuntu | Curso Docker Essencial: Primeiros Passos

Understanding Container Behavior

Introduction to Containers

  • The previous lesson involved executing a "Hello World" container, which simply displays text and then stops. This raises questions about how containers operate.
  • Containers are ephemeral; they initialize, execute their tasks, and then terminate. The "Hello World" example illustrates this behavior.

Determining Container Behavior

  • The behavior of a container is dictated by its image, which serves as a step-by-step guide for execution—akin to a recipe.
  • In the upcoming demonstration, a more complex container will be executed using an Ubuntu operating system that includes all necessary commands and functionalities.

Executing the Ubuntu Container

Checking Docker Service Status

  • Before running the container, it's essential to check if the Docker service is active. A command is used to verify this status.
  • If Docker isn't running, it can be started with service docker start, followed by another status check.

Running the Ubuntu Container

  • The command docker run ubuntu initiates the process of pulling the Ubuntu image from local storage or downloading it if not found.
  • Since no specific command was provided during execution, the container runs briefly before terminating without displaying any output.

Interacting with Containers

Executing Commands in Containers

  • To see output from commands within the container, one can specify commands like echo when running it (e.g., docker run ubuntu echo 'Hello').
  • Another useful command is ls, which lists files and directories within the current working directory of the container.

Using Interactive Mode

  • For ongoing interaction with a container without creating new instances each time, use interactive mode with flags: docker run -it ubuntu.
  • This allows users to enter commands directly into an interactive terminal session within the Ubuntu container.

Managing Container Instances

Exiting and Checking Status

  • After executing commands in interactive mode (like echo 'Hello'), exiting can be done using Ctrl+D. This returns control back to the host user environment.
  • Upon exiting, it's noted that while inside an interactive session, containers may stop running once exited.

Listing Active and Stopped Containers

  • To view currently running containers, use docker ps. It shows active instances but does not list those that have stopped after exit.
  • Use docker ps -a to display all containers including those that are stopped. Each entry includes details such as ID, image name, creation time, and status.

Naming Your Containers

Customizing Container Names

Docker Container Management

Creating and Naming a Docker Container

  • The command docker run is introduced, with the --name flag used to assign a specific name to the container being created. For example, naming the container "meu Ubunto".
  • After running the command, users can verify the container's name using docker ps, which shows that the specified name replaces the default auto-generated one.

Running Containers in Detached Mode

  • The concept of detached mode is explained, allowing containers to run without occupying the terminal. This is useful for running applications or services.
  • To execute a container in detached mode, use the -d flag along with interactive options. An example provided involves running an Ubuntu container.

Checking Container Status

  • After launching a detached container, users can check its status with docker ps, confirming it remains active even when not attached to a terminal session.
  • Users can execute commands within a running detached container using docker exec, demonstrating this by echoing "Lucas" inside the container.

Stopping and Managing Containers

  • To stop a running container, utilize docker stop followed by either its ID or name. It’s noted that only part of the ID (the first four digits) can be used for convenience.
  • After stopping a container, users can list all containers (including stopped ones) using docker ps -a.

Restarting Stopped Containers

  • A method for restarting stopped containers is discussed; instead of creating new ones each time with docker run, you can start existing ones using docker start.
  • When starting an existing stopped container, you have options to attach your terminal back to it interactively by adding flags like --attach.
Video description

⭐ Visão Geral Nesta aula iremos iniciar um container Ubuntu. Além disso, serão mostrados alguns comandos básicos para execução e finalização de containers, execução de containers em background, entre outros. Boa aula e bons estudos! TechEduca: Ensinando tecnologia e transformando mentes! ⭐ Contato Site: www.techeduca.com.br Instagram: @techeducabr LinkedIn Instrutor (Lucas): https://www.linkedin.com/in/lucasandradear/ Inscreva-se no canal: https://www.youtube.com/@TechEducaBr ⭐ Gostou do conteúdo e gostaria de aprofundar no assunto? Indicações de materiais: (pt-br) 1. Descomplicando o Docker: https://amzn.to/3W8de0Q 2. Containers com Docker: Do desenvolvimento à produção: https://amzn.to/4ctYGyk (en-us) 1. Docker: Up & Running: Shipping Reliable Containers in Production: https://amzn.to/3LaDOAo 2. Docker: A Project-Based Approach to Learning: https://amzn.to/3XPd0gD ⭐ Agradecimentos Nosso muito obrigado a cada um de vocês, por estarem conosco apoiando o nosso conteúdo. Lembre-se: o sucesso é a soma de pequenos esforços repetidos diariamente. Valorize cada aprendizado, por menor que pareça. A caminhada pode ser desafiadora e demorada, mas cada esforço te aproxima do seu objetivo. Continue firme e nunca desista, o sucesso está ao seu alcance! Forte abraço, TechEduca 💙🚀