docker compose Installation and Use Detailed Explanation

  • 2021-07-10 21:10:28
  • OfStack

Docker Compose is an Docker tool for defining and running complex applications. Using Docker Compose no longer requires the shell script to start the container. (Configured through docker-compose. yml)

Docker Compose Installation

Github source


sudo curl -L https://github.com/docker/compose/releases/download/1.22.0/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
#  To docker-compose Add executable permissions 
sudo chmod +x /usr/local/bin/docker-compose

Daocloud source


curl -L https://get.daocloud.io/docker/compose/releases/download/1.22.0/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
#  To docker-compose Add executable permissions 
sudo chmod +x /usr/local/bin/docker-compose

Unloading of Docker Compose


sudo rm /usr/local/bin/docker-compose

View the version of Docker Compose


docker-compose --version

Configuring Dockerfile


# Specify the underlying image on which to customize 
FROM java:8

# Maintainer information 
MAINTAINER zhouzhaodong <xiuaiba@163.com>

# Setting the working directory 
WORKDIR /apps/demo

# Add demo-0.0.1-SNAPSHOT.jar  Into the container 
ADD demo-0.0.1-SNAPSHOT.jar demo-1.0.0.jar

#bash Mode execution, so that demo-1.0.0.jar Accessible 
#RUN Newly established 1 Layer on which to execute these commands, and when the execution is finished,  commit  This 1 Layers are modified to form a new image. 
RUN bash -c "touch /demo-1.0.0.jar"

# Declares that the runtime container provides a service port, which is just 1 Declaration, the application of this declaration will not open the service of this port at runtime 
EXPOSE 8080

# Specify container startup program and parameters   <ENTRYPOINT> "<CMD>"
ENTRYPOINT ["java","-jar","demo-1.0.0.jar"]

Configure the docker-compose. yml file


#  Version 
version: '3.0'
services:
 demo:
  # build Is used to specify Dockerfile The file path where the 
  build: .
  #  Mapped port 
  ports:
  - "8080:8080"
  volumes: #  Specify 1 File directory for storing container data. 
  # $PWD  Represents the current path 
  - $PWD/data:/var/lib/log

Common commands for docker-compose


build: #  Build mirroring without cache 
  docker-compose build --no-cache;
up: #  Build and start the container 
  docker-compose up -d
down: #  Delete all containers , Mirror image 
  docker-compose down
restart: # Restart the container 
  docker-compose build; docker-compose down; docker-compose up -d

Run the docker-compose command to build the run image

First, create a new folder in the host machine to store the Dockerfile, docker-compose. yml and the jar package we created before. Go to this directory and run the down command to delete all the images you created before. Run the build command to generate the image. Run the up command to start the container. Visit the ip + port number, and you can see our program.

Related articles: