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

jQuery simulates picker to achieve sliding selection effect

This article shares the specific code of jQuery t...

How to draw a cool radar chart in CocosCreator

Table of contents Preface Preview text Graphics C...

Ubuntu starts the SSH service remote login operation

ssh-secure shell, provides secure remote login. W...

Sample code for testing technology application based on Docker+Selenium Grid

Introduction to Selenium Grid Although some new f...

How to set up Spring Boot using Docker layered packaging

The Spring Boot project uses docker containers, j...

JS Easy to understand Function and Constructor

Table of contents 1. Overview 1.1 Creating a func...

Linux system disk formatting and manually adding swap partition

Windows: Support NTFS, FAT Linux supports file fo...

Why I recommend Nginx as a backend server proxy (reason analysis)

1. Introduction Our real servers should not be di...

VMware virtual machine installation CentOS 8 (1905) system tutorial diagram

The world-famous virtual machine software VMware-...

Three methods of inheritance in JavaScript

inherit 1. What is inheritance Inheritance: First...

MySQL database JDBC programming (Java connects to MySQL)

Table of contents 1. Basic conditions for databas...

Common parameters of IE web page pop-up windows can be set by yourself

The pop-up has nothing to do with whether your cur...