In this blog post, we'll take a hands-on approach to help you grasp the essentials of Docker development. We'll start with an overview of the process and then dive into a step-by-step guide. This post is relevant for developers, operations professionals, and anyone interested in DevOps.

Module Overview: Containerizing Your App

Our goal is to guide you through the process of containerizing an application, starting from source code and ending with a running container. This hands-on experience will give you a holistic understanding of the major components from both a developer and an operations perspective.

You can follow along on your laptop or simply read through the post to gain valuable knowledge. We'll cover topics such as packaging your app as a container image, sharing it in a registry, running it as a container, and understanding Docker fundamentals.

Understanding the full picture is crucial for both developers and operations professionals, as it simplifies their tasks and enhances collaboration. So let's dive in and explore the simplicity and efficiency of Docker development.

An overview

First, we'll give you a high-level overview of the workflow. Don't worry if some concepts are unclear at this stage, as we'll go into more detail later. The main steps in the workflow are:

  • Starting with application source code
  • Using Docker to package it as a container image
  • Pushing the image to a registry
  • Running it as a container

Now, let's break down the process step by step:

Step 1: Building a Container Image

First, you'll need Docker running on your machine (e.g., Docker Desktop on Mac or Windows). Then, you'll have some source code for an application, including a special file called Dockerfile that provides essential information to Docker.

With everything in place, you can build the code into a container image using the command: docker image build. This command pulls the app code, builds dependencies, and packages everything into a neat image.

Step 2: Pushing the Image to a Registry

Once you have the container image, you'll need to push it to a registry with a command like: docker image push. This will upload the image to a centralized repository, such as Docker Hub, making it accessible for future use.

Step 3: Running the Container

Finally, you can run the container with the command: docker container run, along with necessary parameters. For example, if your app is a web app on Port 8080, you can access it via localhost:8080 once the container is running.

Congratulations! You've successfully taken source code and its dependencies, used Docker to package them into an image, pushed the image to a registry, and run it as a container.

With Docker, simplicity is the new normal. You've now gained a basic understanding of the Docker development process. In the next steps, you can further explore each aspect and practice with hands-on exercises. Happy containerizing!

Managing a Containerized App

Now that we have our web app running inside a container, we've created a containerized app. It's similar to a super-fast, lightweight virtual machine. This means we can easily stop, restart, and manage the app just like a VM. Let's dive into managing a containerized app with Docker.

Stopping and Restarting Containers

To stop a container, use the command docker container stop, followed by the container name or its ID. Docker will give the app running inside the container a few seconds to gracefully shut down by sending a SIGTERM signal. If the app doesn't shut down within the grace period, it'll be terminated with a SIGKILL.

Once the container is stopped, you can restart it using the docker container start command with the same container name or ID. Starting and stopping containers is straightforward with Docker.

Deleting Containers

Deleting a container is also simple. First, stop the container, then use the command docker container rm followed by the container name or ID. The container will be completely removed, and attempting to restart it will be unsuccessful.

Running Containers in the Foreground

You can run containers in the foreground, attached to your terminal, by using the docker container run -it command for interactive mode. When running in this mode, your shell is directly connected to the container, and any commands you run will execute inside the container.

To exit the container without terminating it, type ctrl+p+q. This allows you to gracefully leave the container without destroying it. To terminate the container, use the -f flag when deleting it, indicating to Docker that you want to force the deletion of the running container.

Recap

In this section, we learned how to manage a containerized app by stopping, restarting, deleting, and running containers in both the foreground and background. With Docker, managing containerized apps is simple and efficient, making it easier for developers and operations professionals to work together.

 

Saved you some valuable time?

Buy me a drink 🍺 to keep me motivated to create free content like this!