What is a docker – compose
Compose is a Docker tool for managing multiple Docker containers into an application. You need to define a YAML configuration file docker-comemage. yml to write the call relationship between several containers. These containers can then be started/closed at the same time with a single command
Why docker-compose
Docker-compose: for example, docker-compose is composed for a javaweb backend service. For example, docker-compose is composed for a javaweb backend service Java service, database, Nginx, a series of middleware, we can cram them into a Linux container, but it is not easy to maintain, if I want to change nginx server, or want to upgrade the database to master/standby need to restart, then it will affect our other services, which is not what we want, Docker It is suggested that we only run one service in each container, because the Docker container itself occupies few resources, so it is better to separate each service separately
However, we are faced with a problem. If I need to deploy several services at the same time, do I need to write Dockerfile for each service and then build images and containers, so I am tired to death. Therefore, Docker official provides us with docker-compose multi-service deployment tool
Install the docker – compose
You can choose binary installation or PIP installation, I recommend binary, much more convenient than PIP, the speed may be slow, wait patiently
[root@iZbp1cwfb0i1aften1twtvZ registry]https://github.com/docker/compose/releases/download/1.23.0-rc3/docker-compose- # sudo curl - L ` ` uname - s - ` uname -m ` - o /usr/local/bin/docker-compose% Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 617 0 617 0 0 744 0 - : -- : -- - : -- : -- -- - : 100-744, 100, 11.1 M, 11.1 M 53254 0 0 0 0:03:40 0:03:40 - : - : - 66396Copy the code
To give permission
sudo chmod +x /usr/local/bin/docker-compose
Copy the code
Check the version
[root@iZbp1cwfb0i1aften1twtvZ registry]# docker-compose -versionDocker-compose version 1.23.0-RC3, build ea3D406ECopy the code
Docker-compose installation is completed by following the above steps
Introduction to the docker – compose
Docker-compose uses YAML format files to manage services in Docker-compose, similar to Dockerfile
There are two basic concepts in Docker-compose
- Service: an application container, i.e. a Docker container such as mysql, or nginx container
- Project: A complete business unit consisting of a set of associated application containers, such as the website made up of mysql, Web App, and Nginx containers described above. Docker-compose is managed for projects
Docker-compose template file introduction
Take a look at a sample docker-comemage. yml file
version: '2'
services:
nginx:
image: nginx:latest
ports:
- '80:80'
- '1443:443'
volumes:
- /home/docker/nginx/:/nginx/
mysql:
image: mysql:latest
ports:
- '3306:3306'
volumes:
- /home/docker/nginx:/mysql/
myserver:
build:
context: ./
dockerfile: myserver.dockerfile
image: myserver:latest
volumes:
- /home/docker/myserver:/myserver
depends_on:
- mysql
- nginx
environment:
- PROFILE=test
Copy the code
Docker-compose has a depends_on attribute for nginx,mysql, and myServer. Docker-compose has a depends_on attribute for nginx,mysql, and MyServer. Docker-compose has a depends_on attribute for docker-compose.
Next, we will interpret the parameters of docker-comemage. yml
- Version represents the version of our compose file. Currently, there are 1, 2, and 3 versions. Each version has a different syntax.
- Services: We are going to start defining services, one for each Docker container.
- Nginx,mysql, myServer: here we define the name of the service
- Build the build directive cannot be used in conjunction with image in this release, otherwise Compose would not know which directive to use to generate the final service image.
- Image specifies an image. If docker-compose does not exist locally, an attempt will be made to pull the image from the remote docker-compose
- Volumes: Mounted data volumes work the same as -v in the COMMAND line interface
- Environment: Specifies the environment variables
- Container_name: Compose’s container name is in the following format: < project name >< service name >< sequence number > You can customize the project name and service name, but if you want to fully control the container name, you can specify the name using the tag
- Extends: You can specify that you extend based on a template file, but be aware of loop dependencies and do not inherit containers and data volume resources defined in Links and volumes_FROM
- Net: Set network mode Optional parameters “bridge”(bridge network),” None “(no network),”host”(share network with the server)
Docker-compose up -d: docker-compose up -d: docker-compose up -d: docker-compose up
Docker-compose common command
Docker – Compose command
-
format
-
docker-compose [-f <arg>…] [options] [COMMAND] [ARGS…]
-
The command options are as follows:
-
-f, -file file specifies the Compose template file. The default is docker-comemess. yml and can be specified multiple times.
-
-p, – project-name name Specifies the project name. By default, the name of the directory is used as the project name.
-
-x-network-driver Uses Docker’s pluggable network backend feature (Docker 1.9+ required) -x-network-driver Driver Specifies the network backend driver. The default driver is Bridge (Docker 1.9+ required).
-
-verbose Displays more debugging information
-
-v, -version Prints the version and exits
docker-compose up
-
docker-compose up [options] [–scale SERVICE=NUM…] [SERVICE…]
-
Options include:
-
-d Runs the service container in the background
-
– no-color Does not use color to distinguish the control output of different services
-
– no-deps does not start containers linked to the service
-
– force-set forcibly recreates containers. This concept cannot be used with – no-set
-
– no-set If the container already exists, it is not recreated and cannot be used with – force-set
-
– no-build Does not automatically build the missing service image
-
– Build Builds the service image before starting the container
-
– abort-on-container-exit Stops all containers. If any container is stopped, it cannot be used at the same time with -d
-
-t, — timeout timeout Specifies the timeout for stopping the container (default: 10 seconds).
-
— remove-orphans removes containers from the service that are not defined in the compose file
-
— Scale SERVICE=NUM Sets the number of containers the SERVICE runs in, overriding the parameters specified by scale in compose
-
docker-compose up
-
Start all Services
-
docker-compose up -d
-
Start all services in the background
-
-f specifies the Compose template file to use. The default is docker-comemage. yml and can be specified multiple times.
-
docker-compose -f docker-compose.yml up -d
docker-compose ps
-
docker-compose ps [options] [SERVICE…]
-
docker-compose ps
-
Lists all current containers in the project
docker-compose stop
-
docker-compose stop [options] [SERVICE…]
-
Options include:
-
-t, — timeout timeout Specifies the timeout for stopping the container (default: 10 seconds).
-
docker-compose stop
-
To stop a running container, you can start it again with docker-compose start
docker-compose -h
-
docker-compose -h
-
See the help
docker-compose down
-
docker-compose down [options]
-
Stop and delete containers, networks, volumes, and mirrors.
-
Options include:
-
– rmi type, delete the image. The type must be: all, delete all the images defined in the compose file. Local: deletes a mirror whose mirror name is empty
-
-v, -volumes, delete the anonymous data volumes already defined in the compose file attached to the container
-
— remove-orphans, removes containers in the service that are not defined in compose
-
docker-compose down
-
Deactivate removes all containers and network related
docker-compose logs
-
docker-compose logs [options] [SERVICE…]
-
View the output of the service container. By default, docker-compose will use different colors for different service outputs. You can turn off the color by — no-color.
-
docker-compose logs
-
View the output of the service container
docker-compose build
-
docker-compose build [options] [–build-arg key=val…] [SERVICE…]
-
Build (rebuild) the service container in the project.
-
Options include:
-
– Compress Uses gzip compression to build up and down environments
-
– force-rm Deletes temporary containers during the build process
-
– no-cache No cache is used during image construction
-
– Pull always tries to obtain the image of the updated version by pulling
-
-m, -memory MEM sets the memory size for the built container
-
– build-arg key=val Sets the build-time variable for the service
-
Once the service container is built, it will carry a tag name. You can run it in the project directory at any time
-
Docker-compose build to rebuild the service
docker-compose pull
-
docker-compose pull [options] [SERVICE…]
-
Pull the mirror on which the service depends.
-
Options include:
-
– Ignore-pull-failures: ignores errors during mirror pulling
-
– PARALLEL: multiple mirrors are pulled at the same time
-
– quiet: No progress information is displayed during the image pulling process
-
docker-compose pull
-
Pull the mirror on which the service depends
docker-compose restart
-
docker-compose restart [options] [SERVICE…]
-
Restart services in the project.
-
Options include:
-
-t, — timeout timeout, specifies the timeout to stop the container before restarting. (Default: 10 seconds)
-
docker-compose restart
-
Restart services in the project
docker-compose rm
-
docker-compose rm [options] [SERVICE…]
-
Delete all (stopped) service containers.
-
Options include:
-
-f, -force: forcibly deletes containers, including those in the non-stopped state
-
-v: deletes the data volumes attached to the container
-
docker-compose rm
-
Delete all (stopped) service containers. It is recommended to stop the container by executing the docker-compose stop command.
docker-compose start
-
docker-compose start [SERVICE…]
-
docker-compose start
-
Start an existing service container.
docker-compose run
-
docker-compose run [options] [-v VOLUME…] [-p PORT…] [-e KEY=VAL…] SERVICE [COMMAND] [ARGS…]
-
Executes a command on the specified service.
-
docker-compose run ubuntu ping www.baidu.com
-
Executes a ping command on the specified container.
docker-compose scale
-
docker-compose scale web=3 db=2
-
Sets the number of containers on which the specified service runs. The number is set with the service=num argument
docker-compose pause
-
docker-compose pause [SERVICE…]
-
Pause a service container
docker-compose kill
-
docker-compose kill [options] [SERVICE…]
-
Force a stop to the service container by sending SIGKILL.
-
Supports the -s parameter to specify the signal to be sent, such as the following command to send SIGINT signals:
-
docker-compose kill -s SIGINT
dokcer-compose config
-
docker-compose config [options]
-
Verify and view the compose file configuration.
-
Options include:
-
– resolves -image-digests mark image tags as digests
-
-q, -quiet Verifies the configuration but does not output any information. If the configuration is correct, no information is displayed. If the configuration is incorrect, error information is displayed
-
– services Displays the service name, one in a row
-
– Volumes Displays data volume names, one in a row
docker-compose create
-
docker-compose create [options] [SERVICE…]
-
Create a container for the service.
-
Options include:
-
– force-set: recreate containers even if configurations and mirrors are not changed, incompatible with – no-set parameters
-
– no-set: if a container already exists, there is no need to recreate it. – force-set is not compatible with this parameter
-
– no-build: no image is created, even if the image is missing
-
– Build: Generates an image before creating a container
docker-compose exec
-
docker-compose exec [options] SERVICE COMMAND [ARGS…]
-
Options include:
-
-d Runs commands in background mode.
-
— Privileged.
-
– user user Specifies the running user.
-
-t disables TTY allocation. Docker-compose exec allocates TTY by default.
-
– index=index. If a service has multiple containers, you can use this parameter to log in to any service under the service.
-
For example, docker-compose exec — index=1 web /bin/bash, the web service contains multiple containers
docker-compose port
-
docker-compose port [options] SERVICE PRIVATE_PORT
-
Displays the public ports mapped to a container port.
-
Options include:
-
– protocol=proto, which specifies the port protocol, TCP (default) or UDP
-
– index=index, if agree service has multiple containers, specify the serial number of the command object container (default: 1).
docker-compose push
-
docker-compose push [options] [SERVICE…]
-
Push a mirror of the service.
-
Options include:
-
– Ignore-push-failures Ignores errors during mirror push
docker-compose stop
-
docker-compose stop [options] [SERVICE…]
-
Displays the processes running in each container.
docker-compose unpause
-
docker-compose unpause [SERVICE…]
-
Resume a service in the suspended state.
docker-compose version
-
docker-compose version
-
Print version information.
If you are interested, you can follow my personal wechat public account. I will regularly push technical articles about Java, and at present, they are all dry goods