Home  >  Article  >  Operation and Maintenance  >  How to access the server in Docker

How to access the server in Docker

PHPz
PHPzOriginal
2023-04-10 14:19:281067browse

Docker is a lightweight containerization technology that is widely used in software development, testing and production environments. With Docker containers, applications and their dependencies can be packaged into a portable container for easy use in different environments. However, when using Docker containers, we may need to access the server inside the Docker container. This article will introduce how to access the server inside Docker.

1. Use the docker exec command

Docker officially provides a docker exec command that can execute commands inside a running container. The general syntax of this command is as follows:

docker exec [OPTIONS] CONTAINER COMMAND [ARG...]

Among them, OPTIONS includes a series of parameters, such as -t ( allocate a pseudo terminal), -i (keep STDIN open), etc. CONTAINER is the container name or ID of the command to be executed, COMMAND is the command to be executed, and ARG is the parameter of the command.

Suppose we want to access a server running on port 80 inside a container named mycontainer. We can follow the following steps:

  1. Use the docker exec command to enter the container:

docker exec -it mycontainer /bin/bash

  1. Execute commands inside the container to access the server:

curl http://localhost:80

In the above command, the -it parameter is used to allocate an interactive terminal to the container, and /bin/bash is the shell command to be run. If the curl client is installed inside the container, we can use it to access the server.

2. Use the docker port command

In addition to using the docker exec command, we can also use the docker port command to view the port mapping inside the Docker container. The general syntax of this command is as follows:

docker port CONTAINER [PRIVATE_PORT[/PROTO]]

Among them, PRIVATE_PORT is the port to be mapped, and PROTO is the mapping protocol, such as TCP or UDP etc. If PROTO is not specified, the default is TCP.

Suppose we want to access port 80 inside a container named mycontainer, you can follow the following steps:

  1. Use the docker port command to view the mapping of port 80 in the mycontainer container:

docker port mycontainer 80

This command will return a string in the form:

0.0.0.0:32789

Among them, 32789 is mapped to port 80 inside the mycontainer container.

  1. Use curl or other client tools on the host to access:

curl http://localhost:32789

In the above command, localhost is the host name, and 32789 is the port number just obtained from the docker port command.

3. Use the docker network command

If the Docker container is running in its own network, we can use the docker network command to connect the container to the network of the host or other containers. The general syntax of this command is as follows:

docker network connect [OPTIONS] NETWORK CONTAINER

Among them, OPTIONS includes a series of parameters, such as --alias (set an alias for the container ), --ip (set the IP address for the container), etc. NETWORK is the network name or ID to connect to, and CONTAINER is the container name or ID to connect to.

Suppose we want to connect a container named mycontainer to the default bridge network and let it have an alias named webserver. You can follow the following steps:

  1. Use The docker network command connects mycontainer to the bridge network:

docker network connect --alias webserver bridge mycontainer

  1. Use curl or other on the host Client tool to access:

curl http://webserver

In the above command, webserver is the alias we set for the container, which can be accessed from the host direct interview.

Summary

Accessing the internal server of a Docker container is an important issue when using Docker technology. This article introduces three methods to achieve this goal. Using the docker exec command to execute commands inside the container is the most basic way. Viewing port mapping through the docker port command is also a convenient method, and using the docker network command to connect to the network allows more flexible access to the server inside the container. In actual applications, we can choose different methods according to specific needs to conveniently and quickly access the server inside the Docker container.

The above is the detailed content of How to access the server in Docker. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn