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

Introduction and use of docker-compose command

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

Share

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

Overview of docker-compose

Docker-compose technology, through a .yml configuration file, writes a series of configurations such as deployment methods, file mappings, container connections, etc., of all containers in a configuration file, and finally only needs to execute docker-compose up commands to install containers one by one like a script and deploy them automatically, which greatly facilitates the deployment of complex services.

The workflow of docker-compose

1. Use an environment that defines the application, Dockerfile so that it can be reproduced anywhere.

2. Define the services that make up the application, docker-compose.yml so that they can run together in an isolated environment.

3. Run docker-compose up and write to start and run the entire application.

Command object and format

For Compose, the object of most commands can be either the project itself or a service or container in the project. If not specified, the command object will be the project, which means that all services in the project will be affected by the command.

The basic format for using the docker-compose command is:

Docker-compose [- f...] [options] [COMMAND] [ARGS...] Second, the command option-f: specify the compose template file used. The default is the docker-compose.yaml file under the current directory, which can be specified multiple times. -p: specify the name of the project. By default, the directory name will be used as the project name. -- verbose: output more debugging information. -v: print version information and exit. Command instructions 1. Build-- to build (rebuild) the service container in the project

The command format is: docker-compose build [options] [SERVICE...].

Once the service container is built, it will be marked with a tag name, such as web_db for a db container in the web project.

You can run docker-compose build in the project directory at any time to rebuild the service.

The above command options include:

-- force-rm: delete temporary containers during construction. -- no-cache: do not use cache during the construction of the image (this will lengthen the construction process). -- pull: always try to get an updated version of the image through pull. 2. Config-- detects errors in compose files

Verify that the Compose file format is correct, if correct, display the configuration, if the format error shows the cause of the error.

The format of the command is as follows:

Start the service with docker-compose config3 and up--

The format is docker-compose up [options] [SERVICE...].

This command is so powerful that it attempts to automate a series of operations, including building the image, (re -) creating the service, starting the service, and associating the service-related container.

Linked services will be started automatically unless they are already running.

It can be said that most of the time you can start a project directly through this command.

By default, the containers started by docker-compose up are in the foreground, and the console will print the output information of all containers at the same time, making it easy to debug.

When you stop the command through Ctrl-C, all containers will stop.

If you use docker-compose up-d, all containers will be started and run in the background. This option is generally recommended in production environments.

By default, if the service container already exists, docker-compose up will try to stop the container and recreate it (keeping the volume mounted using volumes-from) to ensure that the newly started service matches the latest contents of the docker-compose.yml file. If the user does not want the container to be stopped and recreated, you can use docker-compose up-- no-recreate. This will only start the container in the stopped state and ignore the services that are already running. If users only want to redeploy a service, they can use docker-compose up-- no-deps-d to recreate the service and stop the old service in the background and start the new service without affecting the services they depend on.

Options:

-d: run the service container in the background. -- no-color: no colors are used to distinguish the console output of different services. -- no-deps: do not start the container linked by the service. -- force-recreate: force the container to be recreated and cannot be used with-- no-recreate. -- no-recreate: if the container already exists, it will not be recreated and cannot be used with-- force-recreate. -- no-build: do not automatically build missing service images. -t: timeout when stopping the container (default is 10 seconds). 3. Down-- stop container

This command will stop the container started by the up command and remove the network.

4. Images-- list the images contained in the project $docker-compose images # list the images contained in the project $docker-compose-p tt images # if it is not the default project name, you need to specify the project name 5. Logs-- to view the logs of the service container

The format is docker-compose logs [options] [SERVICE...].

By default, docker-compose will use different colors to distinguish different service outputs. Colors can be turned off with-- no-color.

This command is useful when debugging problems.

The $cat docker-compose.yml # compose file is as follows: '3'services: web: build:. Ports:-"5001docker-compose logs 5000" redis: image: "redis:alpine" $docker-compose logs # without the service name, the logs of all services in the project will be displayed, and the logs between different services will be distinguished by different colors $docker-compose logs web # you can also specify the service name in the project. View the log of the specified service $docker-compose logs redis # as above 6, kill-- sends a SIGKILL signal to force the service container to stop

The format is docker-compose kill [options] [SERVICE...].

It is supported to specify the signal to be sent through the-s parameter, for example, to send a SIGINT signal through the following instruction.

$docker-compose kill-s SIGINT web # kill drop the specified service $docker-compose-p tt kill-s SIGINT web # if it is not the default project name, you need-p to specify the project name 8 and port-- to view the public port mapped by a container port

The format is docker-compose port [options] SERVICE PRIVATE_PORT.

Options:

-- protocol=proto specifies port protocol, tcp (default) or udp. -- index=index if there are multiple containers for the same service, specify the serial number of the command object container (default is 1). The result above [root@node02 test] # docker-compose port web 50000.0.0.0 web 500 shows that port 5000 of the web service is mapped to port 5001 of the host, and ps-- lists all the current containers in the project.

The format is docker-compose ps [options] [SERVICE...].

$docker-compose ps-Q web # View the ID$ docker-compose ps of a service # View the container information of all services in the project

Options:

-Q: only the ID information of the container is printed. 10. Restart-- restarts the services in the project

The format is docker-compose restart [options] [SERVICE...].

Options:

-t,-- timeout TIMEOUT specifies the timeout to stop the container before restarting (default is 10 seconds).

Take a chestnut:

$docker-compose restart # restart all services in the current project $docker-compose restart web # restart web services in the current project 11, rm-- delete all service containers in the stopped state

The format is docker-compose rm [options] [SERVICE...].

Options:

-f: no confirmation of removal is required. -s: stop all containers in the project before deleting. -v: delete any anonymous volumes attached to the container. 12. Run-- runs a command on the specified service

Format: docker-compose run [options] [- p PORT...] [- e KEY=VAL...] SERVICE [COMMAND] [ARGS...] .

For example:

$docker-compose run web ping www.baidu.com

The above directive starts the web service container in the project and executes the ping www.baidu.com command.

By default, if there is an association, all associated services will be started automatically unless they are already running.

If you do not want to start the associated container automatically, you can use the-- no-deps option, for example

$docker-compose run-no-deps web ping www.baidu.com

Options:

-d: runs the container in the background. -- name: specify a name for the container. -- entrypoint: overrides the default container startup directive. -e KEY=VAL: sets the value of the environment variable. You can use the option multiple times to set multiple environment variables. -u: specify the user name or uid under which the container is running. -- no-deps: the associated service container is not automatically started. -- rm: automatically delete the container after running the command. It will be ignored in d mode. -p: map the container port to the local host. -- service-ports: configure the service port and map to the local host. -T: no pseudo tty is allocated, which means that instructions that depend on tty will not run. 13. Scale-- sets the number of containers for the service to run

The format is docker-compose scale [options] [SERVICE=NUM...].

Set the quantity through the parameters of service=num. For example:

$docker-compose scale web=3 redis=2

Three containers will be started to run the web service and two containers will run the redis service. However, it should be noted that when the ports field is defined in the docker-compose, the port mapped to the host cannot be specified, otherwise only one can be started.

In general, when the specified number of containers is more than the actual number of containers currently running by the service, new containers will be created and started; otherwise, containers will be stopped.

Options:

-t: timeout when stopping the container (default is 10 seconds).

14. Stop-- stops the container that is running

The format is docker-compose stop [options] [SERVICE...].

Stop the container that is already running, but do not delete it. These containers can be started again through docker-compose start.

Chestnut:

$docker-compose stop web # stop web services in the current project $docker-compose stop # stop all services in the current project

Options:

-t: timeout when stopping the container (default is 10 seconds) 15. Start-- starts the service container that has been stop

The format is docker-compose start [SERVICE...].

Chestnut:

$docker-compose start16, top-- View processes running in each service container $docker-compose top # View processes of all services in the project $docker-compose top web # View processes of web services in the project 17. Pause-- pauses a service container

The format is docker-compose pause [SERVICE...].

Pause a service container.

As follows:

The $docker-compose-p tt pause redis #-p option specifies the project name # if it is the default project name, you can ignore the-p option $docker-compose pause # suspend all services in the project 18 and unpause-- resume services in the suspended state

The format is docker-compose unpause [SERVICE...].

Chestnut:

$docker-compose unpause

-this is the end of this article. Thank you for reading-

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

*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