Dockers quick learning (4) use of docker containers

Source: Internet
Author: User
Use of docker containers


1. Links between containers:

Run a container and give it a name, for example:

docker run -d -p 0.0.0.0:4455:22 -p 0.0.0.0:8080:80 --name one centos6-ssh

Run another container.

docker run -d -p 0.0.0.0:4456:22 -p 0.0.0.0:8088:80 --link /one:two centos6-ssh2 env

Note:

/One: Two

One is the name of the first container, and two is the name of the second container,

Env is the environment variable for printing the second container.

In this way, the two containers establish a network channel. The ports opened by one and two containers are the ports defined in the dockerfile file,

Run the iptables command on the host, for example:

iptables -L -nChain FORWARD (policy ACCEPT)target     prot opt source               destination         ACCEPT     tcp  --  172.17.1.28          172.17.1.29         tcp spt:3306 ACCEPT     tcp  --  172.17.1.29          172.17.1.28         tcp dpt:3306 ACCEPT     tcp  --  172.17.1.28          172.17.1.29         tcp spt:22 ACCEPT     tcp  --  172.17.1.29          172.17.1.28         tcp dpt:22

The ports between the two containers can access each other,

Note:

The port here is the port opened by the one container, for example, one opens 3306 3306, while the two container only opens 22, and will be placed on two to one, and vice versa. -- Link is based on the port opened by the connection container.

2. understanding of data volumes in docker containers

A data volume is specially designed to provide some features in one or more containers through the UFS file system.

Realize data persistence or sharing.

Data volumes can be shared and reused between containers.

You can directly modify the content in the data volume.

Updating the image does not change the content of the data volume.

The volumes continue until no containers use them.

2.1 Add a data volume

You can use the docker run command with the-V parameter to add a data volume to the container.

docker run -d -p 0.0.0.0:4445:22 --name data -v /data centos6-ssh

This will have a/data volume in the container.

Use the volume command in dockefile to create and add one or more volumes.

2.2 mount the host folder to the data volume

You can use the-V parameter to mount the host's folder to the container.

docker run -d -p 0.0.0.0:44455:22 --name data1 -v /src/data:/opt/data centos6-ssh

In this way, the local/src/data folder will be mounted to the/opt/data directory of the container.

The folder on the host must be an absolute path and will be created automatically when the folder does not exist.

This function cannot be used in the dockerfile.

By default, docker mounts data volumes with read and write permissions, but we can also mount data volumes in read-only mode.

docker run -d -p 0.0.0.0:44455:22 --name data1 -v /src/data:/opt/data:ro centos6-ssh

Or the above command, but we added a Ro option to specify that the file permission should be read-only during mounting.

2.3 create and attach a data volume container

If some data needs to be shared among containers, it is best to create a data volume container and then mount data from the data volume container

1 \ create a named container to share data
 docker run -d -v /dbdata --name dbdata centos6-ssh
2 \ use the -- volumes-from tag in another container to mount the/dbdata volume
docker run -d --volumes-from dbdata --name db1 centos6-ssh2
3 \ mount the/dbdata volume in another container
docker run -d --volumes-from dbdata --name db2 centos6-ssh3


You can use multiple -- volumes-from parameters to put multiple volumes in multiple containers together.

You can mount the containers db1 and DB2 implemented by mounting the dbdata container to expand the relationship chain, for example:

docker run -d --name db2 --volumes-from db1 centos6-ssh4

2.4. Backup, recovery, and migration of data

Use them to back up, restore or migrate data as follows:

-Volumes-from tag to create a container with the volume to be backed up.

docker run --volumes-from dbdata -v $(pwd):/backup centos6-ssh tar cvf /backup/backup.tar /dbdata

Here we create and log on to a new container, mount the data volumes in the dbdata container, and mount a local directory to/backup, finally, run the tar command to back up the dbdata volume to/backup. After the command is executed, the container stops running and keeps the dbdata backup. A backup file is stored in the local directory.

Note: The tar command must be included in the newly created container,

You can recover or migrate the backup data.

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.