Mastering Docker Commands: Practical Hands-On Session

Welcome back! In this lecture, we’ll dive right into a hands-on session where we’ll explore some essential Docker commands. But first, let’s quickly run through the commands we’ll be working with in this demonstration.

1. docker container inspect

The docker container inspect command allows you to retrieve detailed information about a specific container. This can be useful for troubleshooting or gathering more information about your containers. We won’t delve into the details here, but stay tuned as we’ll explore it during the demo.

2. docker container stop

We’ve previously encountered the docker container stop command in the last lecture. It enables you to gracefully stop a running container. You can specify either the container ID or the container name as an argument to stop a particular container.

3. docker container kill

In contrast to docker container stop, the docker container kill command forcefully terminates a container. Use this command with caution, as it’s equivalent to forcefully killing a process. You can also identify the container by its ID or name.

4. docker container logs

To access the logs generated by a container, use the docker container logs command, followed by the container ID or name as an argument. This allows you to view standard output and logs generated by the container.

Now, let’s move on to an exciting topic that involves launching an Nginx container and accessing its default webpage.

Publishing Ports with Docker

Docker containers are isolated, and by default, they have their own internal IP addresses. However, you can publish container ports to your host machine, enabling external access.

Imagine you have a Docker host with IP 192.168.3.101. This host has its own network interface, but when you launch web containers inside it, each container gets its unique IP address in the 172.17.x.x range. These container IPs are isolated to the host.

To make web content inside the containers accessible from outside, you need to map the ports from your Docker host to the container ports. In our example, we’ll expose port 80 on each container and map them to different host ports (e.g., 8001, 8002, 8003, etc.).

Here’s an example of how you can map a container port to a host port:

docker container run -d -p 8001:80 my-web-container

In this case, -p is used to specify the port mapping. You are binding port 8001 on your host to port 80 inside the container named my-web-container. The same principle applies to map ports for other containers, each with its specific port number.

When you access your Docker host’s IP address (e.g., 192.168.3.101) followed by the port you’ve mapped (e.g., 8001), you’ll be directed to the corresponding container’s web page.

Now, let’s dive into our practical demo and learn by doing.

Practical Hands-On Docker Session

  1. Container Inspection
  • First, run docker container inspect to examine the details of a specific container. Use the container ID or name as an argument.
   docker container inspect <container_id_or_name>
  1. Container Stop and Kill
  • We’ll start by stopping a container using docker container stop. Provide the container ID or name as an argument.
   docker container stop <container_id_or_name>
  • Then, let’s forcefully kill a container with docker container kill. Be cautious with this command, and choose wisely.
   docker container kill <container_id_or_name>
  1. Container Logs
  • To access the logs of a container, use docker container logs with the container ID or name as the argument.
   docker container logs <container_id_or_name>
  1. Publishing Ports
  • Finally, let’s publish ports for web containers and access their web pages from your host machine.
   docker container run -d -p 8001:80 nginx-container-1
   docker container run -d -p 8002:80 nginx-container-2
   docker container run -d -p 8003:80 nginx-container-3

These commands map the respective ports to the container’s port 80. When you access 192.168.3.101:8001, 192.168.3.101:8002, or 192.168.3.101:8003, you’ll access the web pages from different containers.

Remember, replace nginx-container-1, nginx-container-2, and nginx-container-3 with your actual container names.

Conclusion

There you have it – a practical hands-on Docker session. We’ve explored essential Docker commands, inspected containers, stopped and killed them, viewed container logs, and learned to publish ports for web containers.

Now it’s your turn to experiment with these Docker commands and explore the powerful features of Docker containers. Feel free to adapt these commands to your needs and get hands-on experience to boost your Docker skills. See you in the next lecture for more Docker mastery!

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top