Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Yusuf Isah
Yusuf Isah

Posted on • Originally published atyuscode.hashnode.dev

Chapter 3 - Docker Containers

Image description

Introduction

Docker containers are the building blocks of modern containerized applications. In this chapter, we'll delve into the world of Docker containers, exploring their fundamental concepts, basic operations, and essential management tasks. You'll learn how containers are instances of images, how to run, list, inspect, and access containers, and how to perform basic container lifecycle operations such as stopping, starting, restarting, and removing. By the end of this chapter, you'll have a solid understanding of Docker containers and be equipped with the skills to effectively work with them in your containerized applications.

Table of Contents

Docker Containers

A Docker container is a lightweight and standalone executable package that includes everything an application needs to run, such as code, libraries, and dependencies. Containers are isolated from each other and the host system, ensuring consistent behavior and performance.

Understanding Containers as Instances of Images

Containers are created from Docker images, which are templates that define the container's configuration and contents. When you run a container, Docker creates an instance of the image, allowing multiple containers to share the same image. This approach ensures efficient resource utilization and enables easy deployment.

Basic Container Operations

Running a Container

  • To run a Docker container, you can use thedocker run command. This command creates a new container from a specified image and starts it. For example:

    docker run[OPTIONS] IMAGE[COMMAND][ARG...]

Let's breakdown the command above:

  • docker run: The command to create and start a new container.

  • [OPTIONS]: Optional flags that modify the container's behavior (e.g.,-it for interactive mode,-p for port mapping, or-d for detached/daemon mode).

  • IMAGE: The Docker image to use as a template for the container.

  • [COMMAND]: The command to run inside the container (if not specified, the image's default command is used).

  • [ARG...]: Arguments passed to the command (if any).

For example:

  docker run-d-p 80:80--name webserver nginx
Enter fullscreen modeExit fullscreen mode

The command above runs an Nginx web server namedwebserver in detached mode, mapping port 80 of the host to port 80 of the container.

Listing Running Containers

To list all running containers, use thedocker ps command.

docker ps
Enter fullscreen modeExit fullscreen mode

The command above will show a list of all currently running containers with details such as container ID, image name, and status.

Inspecting Containers

Use thedocker inspect command to get detailed information about a container.

docker inspect[container_id]
Enter fullscreen modeExit fullscreen mode

Accessing a Running Container

Use thedocker exec command to run commands inside a running container.

dockerexec-it[container_id][command]
Enter fullscreen modeExit fullscreen mode

For example:

dockerexec-it 929374hfh573hdjf /bin/bash
Enter fullscreen modeExit fullscreen mode

Let's breakdown what the command above is doing:

  • docker exec is the command to execute a command inside a running container.

  • -it allocates a pseudo-TTY and keeps the container running after the command finishes.

  • 929374hfh573hdjf is the container ID.

  • /bin/bash is the command to run inside the container (in this case, starts a new Bash shell).

Stopping, Starting, Restarting, and Removing Containers

  • Stopping a Container: To stop a running container, use thedocker stop command followed by the container ID or name.

    docker stop[container_id]
  • Starting a Container: To start a stopped container, use thedocker start command.

    docker start[container_id]
  • Restarting a Container: To restart a running container, use thedocker restart command.

    docker restart[container_id]
  • Removing Containers: To remove a stopped container, use thedocker rm command, followed by the container ID.

    dockerrm[container_id]

If you want to remove a running container, you can use the-f option to forcefully remove it.

dockerrm-f[container_id]
Enter fullscreen modeExit fullscreen mode

Conclusion

In this chapter, we defined Docker containers. We also learned how to run containers, list and inspect them, access their shells, and perform basic operations like stopping, starting, and removing. By mastering these essential skills, you're on your way to efficiently deploy and manage applications in a containerized environment.

Feel free to leave comments and share this article. Follow my blog for more insights on Docker and DevOps!

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Hello. My name is Yusuf Isah, and I am a DevOps enthusiast from Nigeria. I am also passionate about Technical Writing.
  • Joined

More fromYusuf Isah

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp