Docker from getting started to practice notes (ii)

Source: Internet
Author: User
Tags docker ps

1. Mount a host directory as a data volume

The-v flag can also be used to designate a local host directory to be attached to the container.

$ sudo docker run-d-P--name web-v/src/webapp:/opt/webapp training/webapp python app.py

The above command loads the host's/src/webapp directory into the container's/opt/webapp directory. This feature is handy for testing, such as the ability to place programs into a local directory to see if the container is working properly. The path to the local directory must be an absolute path, and Docker will automatically create it for you if the directory does not exist.

* Note: This usage is not supported in Dockerfile because Dockerfile is used for porting and sharing. However, the path format for different operating systems is not the same, so it is not supported at this time.

The default permission for Docker mounted data volumes is read-write, and the user can also specify as read-only by: Ro.

$ sudo docker run-d-P--name web-v/src/webapp:/opt/webapp:rotraining/webapp python app.py

Add: After Ro, it is mounted as read-only.

2. Data Volume container

If you have some continuously updated data that needs to be shared between containers, it's a good idea to create a data volume container.

A data volume container, in fact, is a normal container designed to provide data volumes for other containers to mount.

First, create a named Data volume container dbdata:

$ sudo docker run-d-v/dbdata--name dbdata training/postgres echo data-only container for Postgres

Then, use--volumes-from in other containers to mount the data volumes in the Dbdata container.

$ sudo docker run-d--volumes-from dbdata--name db1 training/postgres$ sudo docker run-d--volumes-from dbdata--name D B2 Training/postgres

You can also use multiple--volumes-from parameters to mount multiple data volumes from multiple containers. You can also mount a data volume from another container that already has a container volume attached.

$ sudo docker run-d--name db3--volumes-from db1 training/postgres

* Note: The container that uses the--volumes-from parameter to mount the data volume itself does not need to remain in the running state.

If you delete a mounted container (including dbdata, DB1, and DB2), the data volume is not automatically deleted. If you want to delete a data volume, you must use the Docker rm-v command to specify that the associated container is deleted at the same time when you delete the last container that also mounts it. This allows users to upgrade and move data volumes between containers.3. External Access Container

Some network applications can be run in a container, and to allow external access to these applications, you can specify port mappings through the-p or-p parameters.

When the-p flag is used, Docker maps a 49000~49900 port to the internal container open network port randomly.

Using Docker PS, you can see that 49155 of the local host is mapped to the 5000 port of the container. Accessing the 49115 port of this machine provides access to the interface provided by the Web App within the container.

$ sudo docker run-d-p training/webapp python app.py$ sudo docker ps-lcontainer ID  IMAGE                   COMMAND       created
   
    status        PORTS                    namesbc533791f3f5  training/webapp:latest  python app.py 5 seconds ago up  2 seconds  0.0.0.0:49155->5000/tcp  Nostalgic_morse
   

Similarly, you can view the app's information through the Docker logs command.

$ sudo docker logs-f 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-

-P (lowercase) You can specify the port to be mapped, and only one container can be bound on a specified port. Supported formats are Ip:hostPort:containerPort | Ip::containerport | Hostport:containerport.

map All interface addresses

Using a local 5000 port in the Hostport:containerport format to map to port 5000 of the container, you can perform

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

All addresses on all interfaces are bound by default at this time.

map to the specified port at the specified address

You can use the Ip:hostPort:containerPort format to specify that mappings use a specific address, such as a localhost address 127.0.0.1

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

any port mapped to the specified address

Using Ip::containerport to bind any port on localhost to port 5000 of the container, the local host is automatically assigned a port.

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

You can also use UDP tokens to specify UDP ports

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

View Map Port configuration

Use Docker port to view the currently mapped port configuration, or to view the bound address

$ docker Port Nostalgic_morse 5000127.0.0.1:49155.

Attention:

    • 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.) )
    • -p flag can be used multiple times to bind multiple ports

For example

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

Use the--link parameter to allow safe interaction between containers.

The following first creates a new database container.

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

Delete a previously created web container

$ 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

At this point, the DB container and the Web container establish an interconnection relationship.

The format of the--link parameter is--link Name:alias, where name is the name of the container to be linked, and alias is the alias of the connection.

Use Docker PS to view the container's connections

$ docker pscontainer ID  IMAGE                     COMMAND               CREATED             STATUS             PORTS                    names349169744e49  Training /postgres:latest  su postgres-c ' usr about  a minute ago  up about a minute  5432/tcp                 db, web/ Dbaed84ee21bde  training/webapp:latest    python app.py         hours ago up        2 minutes       0.0.0.0:49154->5000/tcp  Web

You can see the custom named container, and the DB and Web,db container's names columns have DB and web/db. This means that the Web container is linked to the DB container, and the Web container is allowed access to the DB container's information.

Docker creates a secure tunnel between the two interconnected containers without mapping their ports to the host host. The-p and-p flags are not used when starting the DB container, thus avoiding exposing the database port to the external network.

Docker exposes connection information to containers in 2 ways:

    • Environment variables
    • Update/etc/hosts file

Use the ENV command to view the environment variables for the web container

$ sudo docker run--rm--name web2--link db:db training/webapp env ... db_name=/web2/dbdb_port=tcp://172.17.0.5:5432db_port_5000_tcp=tcp://172.17.0.5:5432db_port_5000_tcp_proto= tcpdb_port_5000_tcp_port=5432db_port_5000_tcp_addr=172.17.0.5. . .

The environment variables at the beginning of the db_ are for use by the Web container Connection DB container, prefixed with uppercase connection aliases.

In addition to environment variables, Docker also adds host information to the parent container's/etc/hosts file. The following is the hosts file for the parent container web

$ sudo docker run-t-i--rm--link db:db training/webapp/bin/bash[email protected]:/opt/webapp# cat/etc/hosts172.17.0.7  Aed84ee21bde ... 172.17.0.5  DB

There are 2 hosts, the first is the Web container, the Web container uses the ID as his host name, and the second is the IP and hostname of the DB container. You can install the ping command in the Web container to test connectivity with the DB container.

[Email protected]:/opt/webapp# apt-get install-yqq inetutils-ping[email protected]:/opt/webapp# ping DbPING db ( 172.17.0.5): Bytes56 data bytes from 172.17.0.5:icmp_seq=0 ttl=64 time=0.267 ms56 bytes from 172.17.0.5:icmp_seq=1 tt l=64 time=0.250 ms56 bytes from 172.17.0.5:icmp_seq=2 ttl=64 time=0.256 ms

Using ping to test the DB container, it resolves to 172.17.0.5. * Note: The official Ubuntu image does not have a ping installed by default and needs to be installed by itself.

Users can link multiple child containers to the parent container, such as multiple web-to-DB containers that can be linked.


Docker from getting started to practice notes (ii)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.