1. External access

Network applications can run in the container. To make these applications accessible externally, you can specify port mappings with the -p or -p parameters. When the -p flag is used, Docker will randomly map a port from 49,000 to 49900 to the open network port of the internal container. Using Docker PS, you can see that localhost 49155 is mapped to port 5000 of the container. In this case, you can access the interface provided by the Web application in the container by accessing port 49155 of the local host.

$ sudo docker run -d -P training/webapp python app.py $ sudo docker ps -l CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES BC533791F3f5 training/ webApp :latest Python app.py 5 seconds Up 2 seconds 0.0.0.0:49155->5000/ TCP nostalgic_morseCopy the code

Similarly, you can view application information by using the Docker logs command.

Nostalgic_morse * Running on http://0.0.0.0:5000/ 10.0.2.2 -- [23/May/2014 20:16:31] "GET / HTTP/1.1" 200-10.0.2.2 - - [23/May/2014 20:16:31] "GET /favicon.ico HTTP/1.1" 404 -Copy the code

-p (lowercase) specifies the port to be mapped, and only one container can be bound to a specified port. Support the format of the IP: hostPort: containerPort | IP: : containerPort | hostPort: containerPort.

2. Map all interface addresses

Use the hostPort:containerPort format to map the local port 5000 to the containerPort 5000

$ sudo docker run -d -p 5000:5000 training/webapp python app.py

By default, all addresses on all local interfaces are bound.

3. Map the specified port at the specified address

Can use IP: hostPort: containerPort format specified mapping using a specific address, such as address 127.0.0.1 localhost

$sudo docker run -d -p 127.0.0.1:55:5000 training/ webApp Python app.py

4. Map any port of the specified IP address

Use IP ::containerPort to bind any port of localhost to port 5000 of the container. The localhost automatically allocates a port.

$ sudo docker run -d -p 127.0.0.1::5000 training/webapp python app.py

Udp tags can also be used to specify UDP ports

$ sudo docker run -d -p 127.0.0.1:5000:5000/udp training/webapp python app.py

5. View the configuration of the mapped port

Use docker port to view the current mapped port configuration, and also to view the bound address

$ docker port nostalgic_morse 5000

note

  1. The container has its own internal network and IP address (using Docker Inspect to get all the variables, Docker can also have a variable network configuration).
  2. The -p flag can be used multiple times to bind multiple ports; Such as:

$ sudo docker run -d -p 5000:5000 -p 3000:80 training/webapp python app.py

6. Connect containers

Container linking system is another way to interact with applications in the container besides port mapping. The system creates a tunnel between the source and the receiving container, which can see the information specified by the source container. Using the –link parameter allows containers to safely interact with each other. Let’s start by creating a new database container.

$ sudo docker run -d –name db training/postgres

Delete the Web container you created earlier

$ docker rm -f web

Then create a new Web container and connect it to the DB container

$ sudo docker run -d -P –name web –link db:db training/webapp python app.py

In this case, the DB container is connected to the Web container. The –link argument is formatted as –link name:alias, where name is the name of the container to link to and alias is the alias of the connection. Use Docker PS to view container connections

$ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 349169744e49 training/postgres:latest su postgres -c '/usr' About a minute ago Up About a minute 5432/tcp db, Web /db aED84EE21bde training/ webApp :latest Python app.py 16 hours ago Up 2 minutes 0.0.0.0:49154->5000/ TCP WebCopy the code

You can see custom named containers, DB and Web, with both DB and Web/DB in the NAMES column of db containers. This means that the Web container is linked to the DB container, and the Web container will be allowed to access the db container’s information.

Docker creates a secure tunnel between two interconnected containers without mapping their ports to the host host. The -p and -p flags are not used when starting the DB container to avoid exposing the database port to the external network. Docker exposes connection information for containers in two ways:

  • The environment variable
  • update/etc/hostsfile

Use the env command to view the environment variables of the Web container

$sudo docker run --rm --name web2 --link db:db training/webapp env DB_NAME=/web2/db DB_PORT= TCP ://172.17.0.5:5432 DB_PORT_5000_TCP = TCP: / / 172.17.0.5:5432 DB_PORT_5000_TCP_PROTO = TCP DB_PORT_5000_TCP_PORT = 5432 DB_PORT_5000_TCP_ADDR = 172.17.0.5Copy the code

The environment variable starting with DB_ is used by the Web container to connect to the DB container, prefixed by the connection alias in uppercase. In addition to the environment variables, Docker also adds host information to the /etc/hosts file of the parent container. The following is the hosts file for the parent container Web

$ sudo docker run -t -i --rm --link db:db training/webapp /bin/bash
root@aed84ee21bde:/opt/webapp# cat /etc/hosts
172.17.0.7  aed84ee21bde
172.17.0.5  db
Copy the code

There are two hosts. The first is the Web container with its id as its host name, and the second is the IP and host name of the DB container. You can test the connection to the DB container by installing the ping command in the Web container.

root@aed84ee21bde:/opt/webapp# apt-get install -yqq inetutils-ping root@aed84ee21bde:/opt/webapp# ping db PING db (172.17.0.5): 48 data bytes 56 bytes from 172.17.0.5: ICmp_seq =0 TTL =64 time=0.267 ms 56 bytes from 172.17.0.5: Icmp_seq =1 TTL =64 time=0.250 ms 56 bytes from 172.17.0.5: icmp_seq=2 TTL =64 time=0.256 msCopy the code

Using ping to test the DB container, it resolves to 172.17.0.5. * Note: Ping is not installed on the official Ubuntu image by default. You need to install it yourself.

Users can link multiple parent containers to child containers, such as multiple Web links to db containers.

7. Customize container names

The connection system is performed by the name of the container. Therefore, you first need to customize a memorable container name. Although the system assigns a name by default when creating a container. Custom named containers have two benefits:

  • A custom name that’s easy to remember, like a Web application container we can call Web
  • This can be a useful reference point when connecting to other containers, such as web containers to DB containers

Use the –name tag to give the container a custom name.

$ sudo docker run -d -P –name web training/webapp python app.py

Use Docker PS to validate the naming.

$ sudo docker ps -l CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES aed84ee21bde training/webapp:latest python App. py 12 hours ago Up 2 seconds 0.0.0.0:49154->5000/ TCP webCopy the code

You can also use Docker Inspect to view the container name

$ sudo docker inspect -f "{{ .Name }}" aed84ee21bde
/web
Copy the code

Note: Container names are unique. If you already have a container named Web, when you want to use the name web again, you need to delete the container with the same name. If the –rm flag is added during docker run, the container will be deleted immediately after termination. Note that the –rm and -d arguments cannot be used together.