Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example of using Dockers Container

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

Editor to share with you the use of Dockers container examples, I hope you will learn something after reading this article, let's discuss it together!

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.014455 22-p 0.0.0.0pur808080 80-- name one centos6-ssh

Run another container

Docker run-d-p 0.0.0.0 one:two centos6-ssh3 env 4456 one:two centos6-ssh3 env 22-p 0.0.0.0 80-- link

Description:

/ one:two

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

Env is the environment variable that prints out the second container

In this way, the two containers establish a network channel, and the ports opened by the one and two containers, that is, the open ports defined in the Dockerfile file, can be connected.

Use the iptables command on the host to view, 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

From here, we can see that the ports between the two containers can access each other.

Description:

The port here is the port opened by the one container. For example, one opens 22pj3306, while the two container only opens 22, and it will also put 3306 to one on two, and vice versa. -- link is based on the open port of the connection container.

2. Understanding of data volumes under Docker container

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

Achieve data persistence or sharing.

Data volumes can be shared and reused between containers

You can modify the contents of the data volume directly.

Updates to the mirror do not change the contents of the data volume

Volumes will continue until there is no container to use them.

2.1. Add a data volume

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

Docker run-d-p 0.0.0.0 name data-v / data centos6-ssh 4445

This will have a / data volume in the container.

Use the VOLUME directive in Dockefile to create and add one or more data volumes

2.2. Mount the host folder to the data volume

You can also mount the host folder to the container using the-v parameter

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

This will hang the local / src/data folder in the container / opt/data directory

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

This feature is not available in Dockerfile files

By default, Docker mounts data volumes with read and write permissions, but we can also mount data volumes as read-only

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

The same command as above, except that we added a ro option to specify that the file permissions should be read-only when mounted.

2.3. Create and hang in a data volume container

If some data needs to be shared between containers, the best way to create a data volume container, and then mount the data from the data volume container

1\ create a named container to share data docker run-d-v / dbdata-- name dbdata centos6-ssh2\ in another container using the-- volumes-from tag to mount the / dbdata volume docker run-d-- volumes-from dbdata-- name db1 centos6-ssh33\ also mount / dbdata volume docker run-d-- volumes-from dbdata-- name db2 centos6-ssh4 in another container

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

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

Docker run-d-name db2-volumes-from db1 centos6-ssh52.4, backup, restore, migrate data

Use them to back up, restore, or migrate data. As shown below, we use the

-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 in a new container, mount the data volume in the dbdata container, mount a local directory under / backup, and finally send a tar command to back up the dbdata volume to / backup. When the command execution is completed, the container will stop running and keep the backup of dbdata, and there will be a backup file in the local directory.

Note: tar command is required in the newly created container.

When you get the backup data, you can restore or migrate the data.

After reading this article, I believe you have a certain understanding of "examples of the use of Dockers containers". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report