Chapter - 3

PIYUSH SHARMAPIYUSH SHARMA
4 min read

Managing Docker Containers with Redis

To connect to a Redis application, you can create a container using the Redis image by running the command 'Docker Run redis'. This starts the Redis image in a container, and you can check the status of running containers with 'Docker PS'. If you terminate the attached container with Control+C, it stops running. To run a container in detached mode, use 'Docker Run -d redis', which will provide the container ID without keeping it attached. To restart a stopped container, use 'Docker Stop [container ID]' and then 'Docker Start [container ID]'. If you want to see all containers, including stopped ones, use 'Docker PS -a'.

Managing Multiple Docker Containers

In Docker, you can run multiple containers simultaneously, even if they use the same application but different versions. For instance, if you need two Redis containers with different image versions, you can pull and run them using the command 'Docker run' which combines pulling the image and starting the container. Although both containers may listen on the same port (like 6379), you can avoid conflicts by creating a binding between your host machine's ports and the container's ports, allowing them to operate without interference.

Understanding Port Binding in Docker Containers

When binding ports in Docker, conflicts arise if multiple containers attempt to use the same host port, such as 5000. However, multiple containers can listen on the same internal port (e.g., 3000) as long as they are mapped to different host ports. To connect to a running container, the host's port must be specified during the run command using the '-P' option, allowing requests to be forwarded correctly. For instance, binding host port 6000 to container port 6379 enables access to the container.

Understanding Docker Commands and Container Management

In this segment, we explore running Docker containers in detached mode and managing multiple instances. By specifying different port bindings, we can create multiple containers, such as running two Redis containers on different ports while they listen on the same internal port. Key Docker commands discussed include 'docker pull' for fetching images, 'docker run' for starting containers, 'docker start' and 'docker stop' for managing container states, and 'docker ps' for listing running containers. Additionally, 'docker images' helps in managing local images, allowing users to clean up stale ones when needed.

Docker Container Management and Troubleshooting

In this section, we explore how to manage Docker containers, including deleting unused containers and troubleshooting issues. To view logs from a specific container, you can use the 'Docker logs' command along with the container ID or name. Additionally, containers can be named for easier identification, which is helpful when managing multiple containers. An example is provided where a new container is created from the Redis 4.0 image with a custom name, demonstrating the process of stopping and creating containers while specifying ports and names.

Using Docker Commands for Container Management

In this tutorial, we explore useful Docker commands for managing containers, such as identifying container versions, retrieving logs, and accessing a container's terminal. By using the 'docker logs' command, users can troubleshoot issues with specific containers. The 'docker exec' command allows users to access the terminal of a running container, enabling them to navigate directories, check configuration files, and print environmental variables. This functionality is particularly beneficial for containers with complex setups. Users can exit the terminal easily, and commands can also be executed using container names for convenience.

Understanding Docker Run vs Docker Start

In Docker, 'Docker run' is used to create a new container from an image, allowing for various options like port binding and container naming. In contrast, 'Docker start' is utilized to restart an existing stopped container, retaining its original attributes. After grasping these basic concepts, it's crucial to see how Docker fits into the software development workflow, including development, continuous integration, and deployment processes, which will be explored in upcoming videos.

Local Development Workflow with Docker

In this tutorial, we explore how to set up a local development environment for a JavaScript application using Docker. Instead of installing MongoDB directly, a Docker container is downloaded from Docker Hub. After developing the application locally, it is committed to a version control system, triggering a Jenkins build that creates a Docker image. This image is then pushed to a private repository and deployed on a development server, where it pulls the necessary MongoDB container. The two containers communicate, allowing testers and developers to test the application. The video will provide a hands-on demo of this process using a JavaScript and Node.js application.

I'll be sharing a comprehensive Docker cheat sheet soon, which you can use as a reference for future projects. At the final chapter of my Docker journey, I’ll also share the tutorial link that I’ve been following before transitioning to Kubernetes or any other technology. Stay tuned!

#Docker #DevOps #CloudComputing #Kubernetes #TechJourney #Automation #Containerization #CloudNative #HashnodeTech #LearnAndShare

We will Continue further in next Chapter.

Follow me on:

Let’s build something amazing together!

1
Subscribe to my newsletter

Read articles from PIYUSH SHARMA directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

PIYUSH SHARMA
PIYUSH SHARMA

"Passionate DevOps enthusiast, automating workflows and optimizing infrastructure for a more efficient, scalable future."