Clicky

How to Manage Containers in Docker

ProgrammingAugust 25, 2023
We are ONE

Introduction

Welcome to the world of Docker, a platform designed to simplify the process of developing, shipping, and running applications. By using containerization, Docker allows developers to package an application with all its dependencies into a standardized unit for software development. This article will guide you through the essential steps of managing containers in Docker—from creating and starting containers, to stopping and removing them. Whether you’re a seasoned developer or just dipping your toes into the waters of containerization, this step-by-step guide is here to help you navigate with ease.

Step 1: Installing Docker

Before we dive into managing containers, it’s crucial to ensure that Docker is installed on your system. If you haven’t done so already, head over to the Docker website and download the appropriate version for your operating system.

Once downloaded, run the installer and follow the prompts. After installation, verify its success by opening a terminal window (Command Prompt on Windows or Terminal on MacOS/Linux) and typing docker --version. This command should return information about the installed Docker version.

It’s worth noting that Docker Desktop for Windows requires Microsoft Hyper-V to run, which might not be available in some versions of Windows 10. In such cases, consider using Docker Toolbox.

Step 2: Creating a Container

With Docker successfully installed, let’s create our first container. Containers are created from images that contain everything needed to run an application—code, runtime environment, libraries, and system tools.

To create a container from an image use the command docker create <image-name>. For instance docker create ubuntu will create a new container with Ubuntu operating system.

If you don’t have the required image locally, Docker will fetch it from Docker Hub, a cloud-based registry service where you can find official docker images.

Step 3: Starting a Container

Now that we’ve created our container, it’s time to start it. To do this, you’ll need the ID of the container you just created. You can find this by typing docker ps -a in your terminal, which will list all containers (running and stopped) along with their IDs.

To start a container, use the command docker start <container-id>. Replace <container-id> with your actual container ID. Once started, your Docker container will run in the background.

Step 4: Interacting with a Running Container

Once your Docker container is up and running, you might want to interact with it. For instance, if you’re running an Ubuntu image as we did earlier, you might want to access its shell.

To do this, use the command docker exec -it <container-id> /bin/bash. This will open a bash shell inside your running Ubuntu container where you can execute commands as if you were on an Ubuntu machine.

Remember to replace <container-id> with your actual Docker container ID.

Step 5: Stopping a Container

After you’ve finished working with your Docker container, you might want to stop it. To do this, use the command docker stop <container-id>. This will send a SIGTERM signal to the container asking it to shut down gracefully. If the container doesn’t stop after a grace period, Docker will forcibly terminate it.

Remember that stopping a container does not remove it from your system. It simply stops the running process inside the container.

Step 6: Removing a Container

If you no longer need a particular Docker container and wish to free up system resources, you can remove it using the command docker rm <container-id>. Be aware that this action is irreversible and all data inside the removed container will be lost unless it was stored in a volume or bind mount.

Before removing a running container, make sure to stop it first as described in step 5. Alternatively, you can force removal of a running container by adding -f flag like so: docker rm -f <container-id>.

Conclusion

Managing containers in Docker might seem daunting at first but with practice becomes second nature. The steps outlined above provide an essential guide for creating, starting, interacting with, stopping and finally removing Docker containers. As always remember that learning is an iterative process; don’t hesitate to experiment and learn from any mistakes along the way.

Sources