In the previous steps, you successfully containerized a ToDo application. Now, in this phase, you will focus on updating both the application and the associated container image. Additionally, this phase will introduce you to the essential processes of stopping and removing a container, providing you with a comprehensive understanding of container management.
Update the source code
In the following steps, you’ll change the “empty text” when you don’t have any todo list items to “You have no todo items yet! Add one above!”
- In the
src/static/js/app.js
file, update line 56 to use the new empty text.
- <p className="text-center">No items yet! Add one above!</p>
+ <p className="text-center">You have no todo items yet! Add one above!</p>
2. Build your updated version of the image, using the docker build
command.
docker build -t getting-started .
3. Start a new container using the updated code.
docker run -dp 127.0.0.1:3000:3000 getting-started
You probably saw an error like this:
docker: Error response from daemon: driver failed programming external connectivity on endpoint laughing_burnell
(bb242b2ca4d67eba76e79474fb36bb5125708ebdabd7f45c8eaf16caaabde9dd): Bind for 127.0.0.1:3000 failed: port is already allocated.
The error occurred because you aren’t able to start the new container while your old container is still running. The reason is that the old container is already using the host’s port 3000 and only one process on the machine (containers included) can listen to a specific port. To fix this, you need to remove the old container.
Remove the old container
To remove a container, you first need to stop it. Once it has stopped, you can remove it. You can remove the old container using the CLI or Docker Desktop’s graphical interface. Choose the option that you’re most comfortable with.
Removing Docker Containers with Ease
If you’ve been working with Docker, you know that managing containers is a key part of the process. In this short guide, we’ll walk you through the simple steps to remove a Docker container using the command-line interface (CLI).
Step 1: Identify the Container
Before you can remove a container, you need to know which one you’re targeting. To get the ID of the container, open your terminal and run the following command:
$ docker ps
This command will display a list of all running containers along with their details.
Step 2: Stop the Container
Once you’ve identified the container you want to remove, you’ll need to stop it first. Use the `docker stop` command, replacing `<the-container-id>` with the actual ID of the container you want to stop:
$ docker stop <the-container-id>
This command will gracefully stop the specified container.
Step 3: Remove the Container
With the container now stopped, you can proceed to remove it from your system using the `docker rm` command:
$ docker rm <the-container-id>
And just like that, the container is gone, freeing up resources and decluttering your Docker environment.
Streamlining Container Removal with Docker Desktop
Docker Desktop is a powerful tool for managing containers, offering an intuitive graphical interface that simplifies many Docker tasks. One such task is the removal of containers, which can be done effortlessly within Docker Desktop. In this blog post, we’ll guide you through the process of removing a container using Docker Desktop’s user-friendly interface.
Step 1: Launch Docker Desktop
Begin by opening Docker Desktop on your system. Once it’s up and running, navigate to the “Containers” view. This view provides a comprehensive overview of all the containers currently managed by Docker Desktop.
It will look like this :
Step 2: Identify the Container to Remove
In the “Containers” view, you’ll see a list of all your containers, along with essential details like their names, statuses, and actions. To remove a specific container, locate it in the list.
Step 3: Delete the Container
Under the “Actions” column for the container you want to delete, you’ll find a convenient trash can icon. Click on this icon to initiate the removal process for the selected container.
Step 4: Confirm Deletion
After clicking the trash can icon, a confirmation dialog will appear. Docker Desktop takes data safety seriously, so it will ask you to confirm your intention to delete the container. To proceed, select “Delete forever.”
Step 5: Container Removal
Once you confirm the deletion, Docker Desktop will swiftly remove the container from your system. You’ll receive a notification or confirmation message indicating the successful removal of the container.
And that’s it! You’ve successfully removed a container using Docker Desktop’s user-friendly interface, making container management a breeze. This streamlined process helps keep your Docker environment organized and efficient, allowing you to focus on what matters most — developing and running your applications.
Docker Desktop’s intuitive design and features simplify many Docker tasks, making it an invaluable tool for developers and sysadmins alike. So, the next time you need to remove a container, turn to Docker Desktop for a hassle-free experience.
These straightforward steps make it easy to manage your Docker containers, keeping your workspace organized and efficient. So, the next time you need to remove a container, remember these simple CLI commands to get the job done swiftly. Happy container management!
Launching Your Updated App Container with Docker
So, you’ve successfully updated your application, and now it’s time to bring it to life in a Docker container. In this blog post, we’ll guide you through the process of starting your updated app container using the simple and powerful docker run
command.
Step 1: Start the Container
To get your updated app up and running in a Docker container, open your terminal and enter the following command:
$ docker run -dp 127.0.0.1:3000:3000 getting-started
Let’s break down this command:
docker run
: This is the command that tells Docker to run a new container.-dp
: These flags indicate that we want to run the container in detached mode (-d
) and map ports (-p
). Detached mode means the container runs in the background, and the-p
flag maps port 3000 inside the container to port 3000 on your local machine.127.0.0.1:3000:3000
: This part of the command specifies the port mapping. The first3000
is the port on your local machine, and the second3000
is the port inside the container.getting-started
: This is the name of the Docker image for your updated app.
Step 2: Check the Result
With your updated app container up and running, it’s time to see the changes in action. Open your web browser and go to http://localhost:3000. You should now see your updated help text or any other changes you made to your application.
And that’s it! You’ve successfully started your updated app in a Docker container, and it’s now accessible through your local machine. Docker’s flexibility and ease of use make it a fantastic tool for deploying and testing applications, ensuring a smooth development and deployment process.
Whether you’re developing a new project or updating an existing one, Docker provides a reliable and consistent environment, making it an essential tool in your development toolkit.
In this section of our Docker journey, you’ve acquired crucial skills for effective container management. You’ve learned how to seamlessly update and rebuild containers, ensuring that your applications are always up to date. Additionally, you’ve gained proficiency in the essential tasks of stopping and removing containers, and maintaining a well-organized and efficient Docker environment.
These skills are fundamental for any Docker user, whether you’re a developer deploying applications or an IT professional managing infrastructure. By mastering container management, you’re better equipped to streamline your workflows, enhance application deployment, and maintain a smooth development process. Keep building on this knowledge as you explore the vast possibilities of containerization and Docker’s capabilities.