This article has participated in the activity of “New person creation Ceremony”, and started the road of digging gold creation together.

Disclaimer: This article is the original version of “Operation and Maintenance home”, please indicate the source of reprint, more content please pay attention to the public account “Operation and maintenance home”.

keynote

The installation of the above has introduced the compose and related commands, so in this paper, we began to specific how to use the detailed say, compose the configuration files and how to write? To look down.

The environment

Docker environment Docker-compose environmentCopy the code

For chestnuts

PS: It is recommended that you switch to the docker-compose configuration file in the same directory when executing docker-compose.

Docker-compose: docker-compose: docker-compose: docker-compose: docker-compose

[yunweijia@localhost ~]$mkdir -pv docker/compose/test01 mkdir: docker/compose Docker /compose/test01" [yunweijia@localhost ~]$CD docker/compose/test01/ [yunweijia@localhost test01]$touch docker-compose.yml [yunweijia@localhost test01]$ vim docker-compose.yml version: '2' services: nginx: image: nginx:latest redis: image: redis:latest [yunweijia@localhost test01]$Copy the code

The above example means to manage both Redis and nginx images. We start it as follows:

[yunweijia@localhost test01]$ sudo docker-compose up -d
Creating network "test01_default" with the default driver
Creating test01_redis_1 ... done
Creating test01_nginx_1 ... done
[yunweijia@localhost test01]$
[yunweijia@localhost test01]$ sudo docker-compose ps
     Name                   Command               State    Ports  
------------------------------------------------------------------
test01_nginx_1   /docker-entrypoint.sh ngin ...   Up      80/tcp  
test01_redis_1   docker-entrypoint.sh redis ...   Up      6379/tcp
[yunweijia@localhost test01]$  
Copy the code

Close him as follows:

[yunweijia@localhost test01]$ sudo docker-compose stop
Stopping test01_redis_1 ... done
Stopping test01_nginx_1 ... done
[yunweijia@localhost test01]$ sudo docker-compose ps
     Name                   Command               State    Ports
----------------------------------------------------------------
test01_nginx_1   docker-entrypoint.sh redis ...   Exit 0        
test01_redis_1   /docker-entrypoint.sh ngin ...   Exit 0        
[yunweijia@localhost test01]$
Copy the code

After stopping, when we look again, we find that state has changed to exit, and then we delete it to prevent it from affecting the experiment we do next.

[yunweijia@localhost test01]$ sudo docker-compose rm
Going to remove test01_redis_1, test01_nginx_1
Are you sure? [yN] y
Removing test01_redis_1 ... done
Removing test01_nginx_1 ... done
[yunweijia@localhost test01]$ sudo docker-compose ps
Name   Command   State   Ports
------------------------------
[yunweijia@localhost test01]$ 
Copy the code

Docker-compose port mapping

Similarly, host ports may not be equal to container port instances:  [yunweijia@localhost test01]$ mkdir -pv /home/yunweijia/docker/compose/test02 mkdir: The created directory "/ home/yunweijia docker/compose/test02" [yunweijia @ localhost test01] $CD/home/yunweijia docker/compose/test02 / [yunweijia@localhost test02]$ touch docker-compose.yml [yunweijia@localhost test02]$ vim docker-compose.yml version: '2' services: nginx: image: nginx:latest ports: - "80:80" redis: image: redis:latest ports: - "6379:6379" [yunweijia@localhost test02]$Copy the code

In the first example, the port number is mapped to the host so that we can use it easily.

[yunweijia@localhost test02]$ sudo docker-compose up -d Starting test02_redis_1 ... done Starting test02_nginx_1 ... done [yunweijia@localhost test02]$ sudo docker-compose ps Name Command State Ports -------------------------------------------------------------------------------------------------- test02_nginx_1 /docker-entrypoint.sh ngin ... Up 0.0.0.0:80->80/ TCP,:::80->80/ TCP test02_redis_1 docker-entrypoint.sh redis... Up 0.0.0.0:6379->6379/ TCP,:::6379->6379/ TCP [yunweijia@localhost test02]$[yunweijia@localhost test02]$sudo firewall-cmd --add-port=80/tcp --permanent success [yunweijia@localhost test02]$ sudo firewall-cmd --add-port=6379/tcp --permanent success [yunweijia@localhost test02]$ sudo firewall-cmd --reload success [yunweijia@localhost test02]$Copy the code

Browser access validation nginx:

Redis, for the same reason, doesn’t validate; We then stop and delete the two containers to prevent any impact on what we are going to do next;

[yunweijia@localhost test02]$ sudo docker-compose stop
Stopping test02_nginx_1 ... done
Stopping test02_redis_1 ... done
[yunweijia@localhost test02]$ sudo docker-compose rm
Going to remove test02_nginx_1, test02_redis_1
Are you sure? [yN] y
Removing test02_nginx_1 ... done
Removing test02_redis_1 ... done
[yunweijia@localhost test02]$ sudo docker-compose ps
Name   Command   State   Ports
------------------------------
[yunweijia@localhost test02]$
Copy the code

3. Docker-compose is used to set network mode

Grammar: network_mode: "patterns" example: [yunweijia @ localhost test02] $mkdir - pv/home/yunweijia/docker/compose/test03 mkdir: The created directory "/ home/yunweijia docker/compose/test03" [yunweijia @ localhost test02] $CD/home/yunweijia docker/compose/test03 / [yunweijia@localhost test03]$ touch docker-compose.yml [yunweijia@localhost test03]$ vim docker-compose.yml version: '2' services: nginx: image: nginx:latest network_mode: "host" redis: image: redis:latest network_mode: "none" [yunweijia@localhost test03]$Copy the code

Redis uses null network mode, nginx uses host network mode, nginx uses null network mode, nginx uses host network mode.

[yunweijia@localhost test03]$ sudo docker-compose up -d
Creating test03_nginx_1 ... done
Creating test03_redis_1 ... done
[yunweijia@localhost test03]$ sudo docker-compose ps
     Name                   Command               State   Ports
---------------------------------------------------------------
test03_nginx_1   /docker-entrypoint.sh ngin ...   Up           
test03_redis_1   docker-entrypoint.sh redis ...   Up           
[yunweijia@localhost test03]$
Copy the code

Redis is null and cannot be accessed, so we can directly access nginx to verify (PS: 80 port firewall is enabled in the second example, skip this step) :

Let’s stop and delete these two containers to prevent the following experiment from being affected:

[yunweijia@localhost test03]$ sudo docker-compose stop
Stopping test03_redis_1 ... done
Stopping test03_nginx_1 ... done
[yunweijia@localhost test03]$ sudo docker-compose rm
Going to remove test03_redis_1, test03_nginx_1
Are you sure? [yN] y
Removing test03_redis_1 ... done
Removing test03_nginx_1 ... done
[yunweijia@localhost test03]$ 
Copy the code

   

Docker-compose setup file sharing

[yunweijia@localhost test03]$ mkdir -pv /home/yunweijia/docker/compose/test04 mkdir: The created directory "/ home/yunweijia docker/compose/test04" [yunweijia @ localhost test03] $CD! $ cd /home/yunweijia/docker/compose/test04 [yunweijia@localhost test04]$ mkdir logs [yunweijia@localhost test04]$ touch docker-compose.yml [yunweijia@localhost test04]$ vim docker-compose.yml version: '2' services: nginx: image: nginx:latest volumes: - /home/yunweijia/docker/compose/test04/logs:/var/log/nginx network_mode: "host" redis: image: redis:latest [yunweijia@localhost test04]$Copy the code

Here is an example of putting the nginx log into the host, and we start to verify it:

[yunweijia@localhost test04]$ sudo docker-compose up -d
Creating test04_redis_1 ... done
Creating test04_nginx_1 ... done
[yunweijia@localhost test04]$ sudo docker-compose ps
     Name                   Command               State    Ports  
------------------------------------------------------------------
test04_nginx_1   /docker-entrypoint.sh ngin ...   Up              
test04_redis_1   docker-entrypoint.sh redis ...   Up      6379/tcp
[yunweijia@localhost test04]$
Copy the code

Browser access several times:

See if the log file is shared:

[yunweijia@localhost test04]$ cd logs/ [yunweijia@localhost logs]$ ls access.log error.log [yunweijia@localhost logs]$ Cat access.log 192.168.112.1 - - [18/Feb/ 2020:07:51:16 +0000] "GET/HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; X64) AppleWebKit / 537.36 (KHTML, Like Gecko) Chrome/98.0.4758.102 Safari/537.36" "-" 192.168.112.1 - - [18/Feb/ 202:07:51:16 +0000] "GET/HTTP/1.1 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; X64) AppleWebKit / 537.36 (KHTML, Like Gecko) Chrome/98.0.4758.102 Safari/537.36" "-" 192.168.112.1 - - [18/Feb/ 202:07:51:16 +0000] "GET/HTTP/1.1 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; X64) AppleWebKit / 537.36 (KHTML, Like Gecko) Chrome/98.0.4758.102 Safari/537.36" "-" 192.168.112.1 - - [18/Feb/ 202:07:51:16 +0000] "GET/HTTP/1.1 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; X64) AppleWebKit / 537.36 (KHTML, Like Gecko) Chrome/98.0.4758.102 Safari/537.36" "-" 192.168.112.1 - - [18/Feb/ 202:07:51:16 +0000] "GET/HTTP/1.1 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; X64) AppleWebKit / 537.36 (KHTML, Like Gecko) Chrome/98.0.4758.102 Safari/537.36" "-" 192.168.112.1 - - [18/Feb/ 202:07:51:17 +0000] "GET/HTTP/1.1 0 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; X64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36" "-" [yunweijia@localhost logs]$Copy the code

Let’s stop and delete these two containers to prevent the following experiment from being affected:

[yunweijia@localhost test04]$ sudo docker-compose stop
Stopping test04_redis_1 ... done
Stopping test04_nginx_1 ... done
[yunweijia@localhost test04]$ sudo docker-compose rm
yGoing to remove test04_redis_1, test04_nginx_1
Are you sure? [yN] 
Removing test04_redis_1 ... done
Removing test04_nginx_1 ... done
[yunweijia@localhost test04]$ 
Copy the code

**5, if the same image and environment, want to start multiple containers **

[yunweijia@localhost test04]$ mkdir -pv /home/yunweijia/docker/compose/test05 mkdir: The created directory "/ home/yunweijia docker/compose/test05" [yunweijia @ localhost test04] $CD! $ cd /home/yunweijia/docker/compose/test05 [yunweijia@localhost test05]$ touch docker-compose.yml [yunweijia@localhost test05]$ vim docker-compose.yml version: '2' services: nginx: image: nginx:latest redis: image: redis:latest [yunweijia@localhost test05]$ sudo docker-compose up -d --scale nginx=3 Creating network "test05_default" with the default driver Creating test05_nginx_1 ... done Creating test05_nginx_2 ... done Creating test05_nginx_3 ... done Creating test05_redis_1 ... done [yunweijia@localhost test05]$Copy the code

In the example above, we will start three nginx images, but redis is not affected, still one. If we want to enter multiple nginx images, we need to specify the container ID, as follows:

[yunweijia@localhost test05]$ sudo docker-compose exec --index=2 nginx bash
root@f50418f6d919:/# exit
exit
[yunweijia@localhost test05]$ sudo docker-compose exec --index=1 nginx bash
root@23e004cc5330:/# exit
exit
[yunweijia@localhost test05]$
Copy the code

    

Stop and delete these four containers as follows:

[yunweijia@localhost test05]$ sudo docker-compose stop
Stopping test05_nginx_3 ... done
Stopping test05_nginx_2 ... done
Stopping test05_redis_1 ... done
Stopping test05_nginx_1 ... done
[yunweijia@localhost test05]$ sudo docker-compose rm
Going to remove test05_nginx_3, test05_nginx_2, test05_redis_1, test05_nginx_1
Are you sure? [yN] y
Removing test05_nginx_3 ... done
Removing test05_nginx_2 ... done
Removing test05_redis_1 ... done
Removing test05_nginx_1 ... done
[yunweijia@localhost test05]$
Copy the code

Docker-compose: docker-compose, docker-compose, docker-compose, docker-compose, docker-compose, docker-compose, docker-compose, docker-compose, docker-compose, docker-compose, docker-compose, docker-compose

Next, we will write down how to build harbor warehouse and how to use it. If you have any questions, please leave a message on the background.