Running a Redis Container
Previously, we used the following command to run a Redis container:Expected Output
Running these commands might yield outputs similar to the following:Interacting with a Containerized Application
Consider an application that asks the user for their name and then displays a welcome message. Running the application locally produces:-i flag (interactive mode) alone does not display the prompt, as the container isn’t attached to a terminal:
For applications requiring user input, always combine the
-i (interactive) and -t (pseudo terminal allocation) options:Port Mapping for Containerized Web Applications
When running a containerized web application that listens on port 5000, the container’s internal IP (e.g., 172.17.0.2) is only accessible within the Docker host. To expose the application externally, you must map a free host port to the container’s port using the-p parameter.
For instance, to route traffic from port 80 on your Docker host to port 5000 in the container, run:
Data Persistence with Docker Volumes
By default, a MySQL container stores its data in/var/lib/mysql inside the container. However, since container filesystems are ephemeral, all data is lost when the container is removed. Consider the following example:
/opt/datadir on your host and run:
/opt/datadir remains intact even if the container is deleted.
Inspecting Running Containers
To retrieve basic information about your running containers, use thedocker ps command, which lists container names and IDs. For detailed information about a particular container, the docker inspect command provides comprehensive configuration details in JSON format. For example:
docker inspect when you need to review the complete state and configuration of a container.
Viewing Container Logs
If you launch a container in detached mode using the-d flag, you may want to view its logs later. The docker logs command displays the standard output of a container. To view logs from a container named “blissful_hopper,” run: