by Devin Yang
(This article was automatically translated.)

Published - 6 years ago ( Updated - 1 year ago )

This article is an intermediate application of Docker:

- The docker command and Dockerfile are the basic applications of Docker.
- docker-compose is a mid-level application of Docker, you should first understand the commands of docker and related settings such as Dockerfile.
- docker swam and stack are high-level applications of Docker.


If you don't know much about the basic application of Docker, you can refer to my other article first:

Devin Yang's Notes for Beginners with Docker


foreword

We will use docker --network to establish multiple container interconnections, but if there are four containers,
Is it necessary to issue docker run instructions for different containers four times, kill me,
This article introduces the yaml file of docker-compose to help us define the establishment of multiple containers.
And learn how to load more yml configuration files with the dokcer-compose -f parameter.

This article just provides a basic docker-compose usage scenario,
You should be able to refer to the relevant docker-compose templates for a deeper understanding of the meaning of each setting.
For the description of the compose file on Docker’s official website, please refer to:
https://docs.docker.com/compose/compose-file/

docker-compose parameters

Here I use my D-Laravel v1.0.0 as a template (D-Laravel is a development environment for the Laravel framework built using docker-compose)
D-Laravel v1.0.0 version has moved out the yaml file in the sample folder independently,
Put it into the service folder, such as redis.yml, we can create more yaml files to use in the service folder.

Getting to the point, what if we want to add more yaml files to the original docker-compose.yml file?
For example: we want to join the redis service. (Use docker-compose official instructions)

parameterillustrate
-p It is project-name (the default is the folder name), so when not specified, the project-name defaults to dlaravel.
-f Define additional compose files (default is docker-compose.yml)
up -d Detached mode: Let the container execute in the background and print the new container name.

Tip : Like git or Laravel's php artisan, we can also use help to view instructions.
Try to execute docker-compose help up to see the instructions of docker-compose up.


Use multiple yaml files at a time, and start multiple Containers at a time.
(Multiple containers can be set in one yaml file, but the expansion is flexible, I can use the -f parameter to have more additional containers)
$docker-compose -p dlaravel -f docker-compose.yml -f service/redis.yml up -d

Under normal circumstances, for Laravel development, we only need to use nginx, php-fpm and mysql (docker-compose.yml),
Suddenly there is a special need today, and a redis development environment is also required??
No need to knock down the old docker-compose.yml rewrite, we can use -f,
Add additional docker-compose settings, in case there is a loophole in redis.yml that day,
We also just need to update redis.yml.

Because in the startup command, we specified two yaml files, one of which is in the folder of the service, as mentioned in the -p parameter above, when not using -p,
The default folder name will be used. As a result, the folders of dlaravel/docker-compose.yml and dlaravel/service/redis.yml are in different layers.
The resulting contaiener network will be different, so -p is added to this command.

Under normal circumstances, we only need to docker-compose up -d, and docker-compose will intelligently and automatically search for the docker-compose.yml in the directory. If it cannot find it, it will search for the upper directory and use the folder as the default The project name.

But in this example, the yaml file of the redis service we want to add is not the same folder as docker-compose.yml, so we define -p (custom project-name), and all containers will be the same project- name and network so that you can use -f to specify the yaml files in different directories.

Note: We can write a simple bash, so we don't need to type such a long command every time to start.

 $docker-compose -p dlaravel -f docker-compose.yml -f service/redis.yml up -d
Creating network "dlaravel_dlaravel_net" with the default driver
Creating dlaravel_web_1...
Creating dlaravel_db_1...
Creating dlaravel_php_1...
Creating dlaravel_redis_1...
Creating dlaravel_web_1
Creating dlaravel_redis_1
Creating dlaravel_db_1
 Creating dlaravel_redis_1... done

Use docks ps to check, the cool one-line command starts four containers at the same time.

 $docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a3e2bfd4945b deviny/fpm:7.1.7 "docker-php-entryp..." 7 seconds ago Up 4 seconds 9000/tcp dlaravel_php_1
634e46d340b1 mysql:5.7.17 "docker-entrypoint..." 7 seconds ago Up 5 seconds 127.0.0.1:3306->3306/tcp dlaravel_db_1
b3840f993f07 redis "docker-entrypoint..." 7 seconds ago Up 3 seconds 6379/tcp dlaravel_redis_1
 4697181b8896 nginx "nginx -g 'daemon ..." 7 seconds ago Up 4 seconds 0.0.0.0:80->80/tcp, 0.0.0.0:443->443/tcp dlaravel_web_1

If you want to stop, remove -d from the original startup command, and change up to down.
$docker-compose -p dlaravel -f docker-compose.yml -f service/redis.yml down

 $docker-compose -p dlaravel -f docker-compose.yml -f service/redis.yml down
Stopping dlaravel_php_1... done
Stopping dlaravel_db_1... done
Stopping dlaravel_redis_1... done
Stopping dlaravel_web_1... done
Removing dlaravel_php_1... done
Removing dlaravel_db_1... done
Removing dlaravel_redis_1... done
Removing dlaravel_web_1... done
 Removing network dlaravel_dlaravel_net

Note: The reason why there is a -p parameter in the above command is that we have loaded multiple yaml files in different directories.
When the yml file is in the same directory, the -p parameter can be omitted.

Tags: docker d-laravel docker-compose laravel

Devin Yang

Feel free to ask me, if you don't get it.:)

No Comment

Post your comment

Login is required to leave comments

Similar Stories


docker,dlaravel

D-Laravel 1.5.5 Change Notes

D-Laravel's fpm image was rebuilt using the official dockerfile of docker php before php 7.2.1. And so I can specify that the default owner of fpm is dlaravel, --with-fpm-user=USER Set the user for php-fpm to run as. (default: nobody) --with-fpm-group=GRP Set the group for php-fpm to run as.

docker,laravel,nfs

Docker's NFS on macOS

D-laravel's nfs mode, in this article you can learn how to use NFS+Docker on macOS.

d-laravel,docker

D-Laravel v1.2.1 version change description (recommended update, must avoid stepping on thunder)

D-Laravel v1.2.1 revision instructions use GuzzleHttp\Client; When posting, there is an access denied problem. If you are upgrading from an older version, you may need to adjust the configuration file in etc/default-ssl.conf (if there is one) Please adjust php:9000 to web:9000, you can adjust it by executing ./console secure once. In the docker-compose configuration file, change the PHP-FPM container to network_mode: "service:web"...