Docker Implementation Container Port Binding Local Port

  • 2021-10-25 08:15:38
  • OfStack

Today, after starting mirroring, I encountered a small problem that could not be accessed by the way requested by HTTP. Let's record and share the solution below:

Common docker startup commands and workarounds are as follows:

1. Start docker

As we all know, the command to start the container is as follows:


docker run  Mirror name 

2. Bind container ports to local ports

After starting the container, the port in the container cannot be directly accessed locally, so it is necessary to bind the local port with the port of the container, so that the container can be accessed through the local port. The settings are as follows:


docker run -p  Local port: Container port   Mirror name 

Then you can access the container through the local browser and the local port.

3. The container runs in the background, adding the-d parameter, and the command is as follows:

docker run-d-p Local Port: Container Port Image Name

4. View the image list with the following command


docker image ls
 Or 
docker images

5. Look at the container that is running. The command is as follows


docker ps

Supplement: docker maps local ports to running containers

1. Submit a running container as a mirror (so that all operations in the container will not be lost)


docker commit tang   tang1
### tang( Run container name )  tang1( Generate image name )

2. Run mirroring and add ports


docker run -d -it -p 8000:80 tang1:latest /bin/bash
###  Small p Is a custom port   latest  Is the label of the mirror image ( Better write something professional )

Added: docker loads volume and binds port

Requirements

Having obtained the docker image, you now want to create the container, load the local directory into the container directory, and map the port between the local and container.

Specific examples are as follows:

image ID: 884e0fc83dfe

Setting container name: test

Map local port 9000 to port 80 of container

Load data volume volume: Map local ~/Desktop/test to the/test directory of container

Realization


docker run -it --name test -v ~/Desktop/test:/test -p 9000:80 884e0fc83dfe /bin/bash
#  Enter New container
docker start test
docker attach test

Related articles: