Docker implements container port binding local port

Docker implements container port binding local port

Today, I encountered a small problem that after starting the image, I could not access it through HTTP request. Let's record and share the solution:

Common Docker startup commands and solutions are as follows:

1. Start Docker

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

docker run image name

2. Binding container ports to local ports

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

docker run -p local port: container port image name

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

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

docker run -d -p local port: container port image name

4. View the mirror list, the command is as follows

docker image ls
or docker images

5. View the running container, the command is as follows

docker ps

Supplement: Docker maps local ports to running containers

1. Submit the running container as an image (so that various operations in the container will not be lost)

docker commit tang tang1
### tang (name of running container) tang1 (name of generated image)

2. Run the image and add ports

docker run -d -it -p 8000:80 tang1:latest /bin/bash
### The small p is the custom port and latest is the label of the image (it is better to write something professional)

Supplement: Docker loads volume and binds port

Require

The docker image has been obtained. Now you want to create a container, load the local directory into the container directory, and map the ports between the local and container.

Specific examples are as follows:

image ID: 884e0fc83dfe

Set container name: test

Map the local port 9000 to the container's port 80

Load the data volume volume: map the local ~/Desktop/test to the container's /test directory

accomplish

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

The above is my personal experience. I hope it can give you a reference. I also hope that you will support 123WORDPRESS.COM. If there are any mistakes or incomplete considerations, please feel free to correct me.

You may also be interested in:
  • Use non-root users to execute script operations in docker containers
  • Solution to the Docker container not having permission to write to the host directory
  • Solve the problem of setting Chinese language pack for Docker container
  • Solution for Docker container not recognizing fonts such as Songti
  • Docker win ping fails container avoidance guide
  • How to configure Jupyter notebook in Docker container
  • Solve the problem of the container showing Exited (0) after docker run
  • Docker uses root to enter the container

<<:  CSS menu button animation

>>:  Detailed explanation of loop usage in javascript examples

Recommend

Detailed explanation of MySQL information_schema database

1. Overview The information_schema database is th...

Detailed explanation of the use of DockerHub image repository

Previously, the images we used were all pulled fr...

How to delete special character file names or directories in Linux

Delete a file by its inode number First use ls -i...

Native js to realize bouncing ball

On a whim, I wrote a case study of a small ball b...

Detailed explanation of VUE Token's invalidation process

Table of contents Target Thought Analysis Code la...

Native JS to achieve digital table special effects

This article shares a digital clock effect implem...

A brief discussion on the principle of js QR code scanning login

Table of contents The essence of QR code login Un...

Things to note when writing self-closing XHTML tags

The img tag in XHTML should be written like this:...

A guide to writing flexible, stable, high-quality HTML and CSS code standards

The Golden Rule Always follow the same set of cod...

Vue implements countdown function

This article example shares the specific code of ...

How to use stored procedures in MySQL to quickly generate 1 million records

Preface When testing, in order to test the projec...