by Devin Yang
(This article was automatically translated.)

Published - 7 years ago ( Updated - 2 years 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, api

Introduction to Swagger

The best APIs are built using the Swagger tool, This article introduces how to use docker to execute swagger-ui and editor, let us create a testable API file. In the docker environment, we can easily start the swagger editor and user interface.

linux,docker,wifi

Use docker to build a WPA2/EAP enterprise Radius authentication server, and the backend uses a mysql database

AP provides WPA2/EAP function, but it won't work? This article introduces how I can quickly build a Radius server through docker's ubuntu:21.04 image. Related applications such as WPA2/EAP of Wifi.

d-laravel, docker, laravel, docker-compose

D-Laravel v1.0.0 release change description

In order to allow the container to be used more flexibly, D-Laravel has released version v1.0.0, which is not backward compatible. https://github.com/DevinY/dlaravel/releases/tag/v1.0.0 If you are using before v1.0.0, you need to modify the .env file of the Laravel project, change DB_HOST=127.0.0.1 to DB_HOST=db If you have a custom docker-compose-custom.yml file....more