Creating and Exposing a Docker Container
This article explains the steps taken to create a Docker container and expose it to a specific port on the host machine. The process involves using the docker run
command with several key options to configure the container's behavior and network settings.
Step 1: Running the Container
The first command executed was docker run -d --name games -p 3003:80 nginx:alpine-perl
. Let's break down each part of this command:
-
docker run
: This is the core command used to create and start a new container. -
-d
: This flag runs the container in detached mode, meaning it runs in the background. The terminal is immediately returned to the user, allowing them to continue with other tasks. -
--name games
: This assigns the namegames
to the container, making it easier to reference and manage later instead of using its long, cryptic ID. -
-p 3003:80
: This is the port mapping or port publishing option. It maps a port on the host machine to a port inside the container. In this case, it maps host port 3003 to container port 80. Any traffic coming to port 3003 on the host will be redirected to port 80 inside the container. Since Nginx listens on port 80 by default, this setup allows external access to the web server. -
nginx:alpine-perl
: This specifies the Docker image to use for the container. It is a variant of the official Nginx image that includes theperl
module and is based on the lightweight Alpine Linux distribution.
After running this command, the system outputs a long string of characters (33fbef20b61f...
), which is the container ID. This confirms that the container was successfully created and started.
Step 2: Verifying the Container Status
The next command, docker ps
, is used to list all currently running containers. The output of this command provides essential information about the newly created container.
-
CONTAINER ID
: The short version of the unique ID for the container (33fbef20b61f
). -
IMAGE
: The name of the image used to create the container (nginx:alpine-perl
). -
NAMES
: The name assigned to the container (games
). -
STATUS
: The current status of the container, which is "Up 28 seconds," indicating it's running successfully. -
PORTS
: This is a crucial field that shows the port mapping. The entry0.0.0.0:3003->80/tcp
explicitly confirms that port 3003 on all host machine interfaces (0.0.0.0
) is forwarding TCP traffic to port 80 inside the container.
These steps demonstrate how to launch a Docker container in the background and publish its internal service to a specific port on the host machine, making it accessible from the outside.
Top comments (0)