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


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:

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)

-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
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>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>80/tcp,>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


Where is the volume location of Docker?

we can use docker volume ls List all dossiers. When using inspect on OSx to view, as a result, we can't find the Mountpoint directory when we get to the Mac?

wordpress, d-laravel, docker, docker-compose

How to install Wordpress with D-Laravel

An 8-minute teaching video, introducing the installation of Wordpress on D-Laravel. D-Laravel is a docker-compose Laravel framework development environment, but it does not mean that it cannot be used to install other things. This video introduces the use of D-Laravel to install non-Laravel PHP projects, You can learn the usage of related instructions on D-Laravel: ./create --host usage, docker-compose up -d and ./console mysql usage, The database connection setting method after D-Laravel v1.0.0.


A brief introduction to the phpenv container environment I built

I don't have time to shoot an introduction video, so I'll just grab some pictures and introduce the container environment deviny/phpenv I use. can be regarded as an evolutionary version of my previous D-Laravel open source project, conceptually extending the use of many Dlaravel operation methods. The update of the container tends to be controlled by the user to build his own image, so I am not very good at changing the version number. In fact, the php version number of D-Laravel seems to have not been changed for a long time:p