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

Use of Docker data volumes and container orchestration

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

Share

Shulou(Shulou.com)06/02 Report--

Preface

Docker data volume and container choreography, record it!

Data volume

Data volumes can bypass the copy system and share directories or files among multiple containers and between containers and host hosts. Data volumes bypass the copy system and achieve local disk Imax O performance.

When you run the container, specify a data volume

$docker run-itd-- name nginx-v / usr/share/nginx/html/-p 80:80 e445ab08b2be

At this point, we create a data volume and mount it to the container's / usr/share/nginx/html/ directory.

Use the docker inspect command to locate the data volume mapping directory:

Docker defaults to the / var/lib/docker/volumes/1baaa58867f6e5b6bed703b0eae6ae859d39fa293ad5d85f58bf73d6101914c6/_data directory of the host as the source directory. Next, go to this directory:

At this point, it is found that the contents of the files in this directory are the same as those in the / usr/share/nginx/html/ directory in the container, because an empty data volume is mounted to a non-empty directory in the container, and the files in this directory will be copied to the data volume.

Note: because the Docker in Mac is a bit special, the / var/lib/xxxx directory mentioned above can be entered directly if it is in the Linux environment. If you are in mac, you need to execute the following command first and enter the / var/lib/xxx directory in the new command line: screen ~ / Library/Containers/com.docker.docker/Data/vms/0/tty

Next, modify the contents of the index.html file in the data volume as follows:

$echo "hello volumes" > index.html

After the modification is complete, go back to the browser and enter 127.0.0.1 to view the data in the index.html page in nginx and find that it has changed.

Note: enter curl 127.0.0.1 in Linux environment

Indicates that the files in the host are shared to the container.

Host directory as data volume

The use of data volumes mentioned above is not the best solution. In general, we may need to explicitly specify that a directory in the host should be mounted to the container as follows:

$docker run-itd-- name nginx2-v / root/www/:/usr/share/nginx/html/-p 888980 98ebf73aba75

In this way, the / root/www/ directory in the host is mounted to the container's / usr/share/nginx/html/ directory, and then you only need to add or modify the html file under the / root/www/ directory to see the effect immediately in the nginx access. This usage is very convenient for development and testing without having to redeploy, restart the container, and so on.

Note: the host directory is an absolute path.

Other operations of data volume

Set the data volume in the Dockerfile file

VOLUME / usr/share/nginx/html/

In this way, an anonymous data volume is configured, and during the run, the data is written to the / usr/share/nginx/html/ directory to achieve stateless changes in the container storage layer.

View all data volumes

$docker volume ls

View data volume details

$docker volume inspect

Delete data Volum

$docker volume rm

Note: for data volumes in use, the relevant containers need to be stopped and removed before they can be deleted.

Data volume container

The data volume container is a container dedicated to managing data volumes, which is mainly used for reference and use by other containers.

Create a data volume container

$docker run-itd-v / usr/share/nginx/html/-- name mydata ubuntu

Reference container

$docker run-itd-volumes-from mydata 80:80-name nginx1 nginx$ docker run-itd-volumes-from mydata 81:80-name nginx2 nginx

At this point, both nginx1 and nginx2 mount the same data volume to the / usr/share/nginx/html/ directory. Any one of the three containers modifies the files in this directory, and the other two can see the changes.

Container connection

Generally speaking, after the container starts, we all use the services provided by the container through port mapping. Port mapping is only a way to use container services. In addition to this way, you can also use container services by connecting containers.

For example, there are two containers, one running a SpringBoot project and the other running a mysql database, which allows SpringBoot to access the Mysql database directly through a container connection without having to access the mysql service through port mapping.

To keep the case simple, let me give you another example:

There are two containers, one nginx container and the other ubuntu. I start the nginx container, but do not assign a port mapping, and then start ubuntu, connect through the container, and access the nginx in ubuntu.

The specific steps are as follows:

First start a nginx container without assigning a port. The command is as follows:

$docker run-d-name nginx1 nginx

After the container starts successfully, it is inaccessible in the host.

Start ubuntu

Next, start a ubuntu and establish a connection with nginx, as follows:

$docker run-dit-name ubuntu-link nginx1:mylink ubuntu bash

Here, you use-- link to establish a connection, nginx1 is the container to establish the connection, and the following mylink is the alias of the connection.

After running successfully, go to the command line of the ubuntu container:

$docker exec-it ubuntu bash

Enter env directly in the ubuntu console to view the environment variable information:

You can see that docker creates a series of environment variables for nginx. Each prefix variable is MYLINK, which is just getting an alias for the connection. Developers can use these environment variables to configure the application to connect to nginx. The connection is secure and private.

Note: by default, the curl command is not installed in the ubuntu container and needs to be installed manually. The installation commands are as follows: apt-get update and apt-get install curl.

Container arrangement

Container choreography can be implemented using docker-compose.

Compose is a tool for defining and running multi-container Docker applications.

Install Compose on Linux system to download Docker Compose binaries

Sudo curl-L "https://github.com/docker/compose/releases/download/1.24.1/docker-compose-$(uname-s)-$(uname-m)"-o / usr/local/bin/docker-compose

Apply executable permissions to binaries:

Sudo chmod + x / usr/local/bin/docker-compose

Test if docker-compose is installed:

$docker-compose-version

Write Dockerfile with the following content:

FROM tomcatADD https://github.com/JpressProjects/jpress/raw/alpha/wars/jpress-web-newest.war / usr/local/tomcat/webapps/RUN cd / usr/local/tomcat/webapps/\ & & mv jpress-web-newest.war jpress.war

Explanation:

The container is created based on Tomcat.

Download the war package of the jpress project to the webapps directory of tomcat.

Rename the jpress project.

Write docker-compose.yml with the following content:

Version: '3.1'services: web: build:. Container_name: jpress ports: -' 8080 build 8080 volumes:-/ usr/local/tomcat/ depends_on:-db db: image: mysql container_name: mysql command:-- default-authentication-plugin=mysql_native_password restart: always ports:-'3306' environment: MYSQL_ROOT_PASSWORD: root MYSQL_DATABASE: jpress declares the web container first, and then the db container. Build. Indicates that the web container project build context is., that is, the Dockerfile build web container will be found in the current directory. Container_name represents the name of the container. Ports refers to the port mapping of the container. Volumes represents the data volume of the configuration container. Depends_on indicates that the container depends on the db container. When starting, the db container will start first, and the web container will start later. This is only a matter of timing. It does not mean that the web container will not start until the db container is fully started. For db containers, image is used to build instead of Dockerfile. Restart describes the restart strategy for the container. Environment is the environment variable when starting the container. Here, the password of the database root user is configured and a library named jpress is created at startup. Environment can be configured in both dictionary and array forms.

Note: there can be no extra spaces in the docker-compose.yml file! Otherwise, it will not run successfully.

Run:

$docker-compose up-d

Enter http://localhost:8080/jpress in the browser and you can see the configuration page of jpress.

Common command

# stop Container running $docker-compose stop# start Container $docker-compose start# restart Container $docker-compose restart

Note: the above command needs to be executed in the same directory as docker-compose.yml and Dockerfile.

The above is the whole content of this article, I hope it will be helpful to your study, and I also hope that you will support it.

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: 238

*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