Personal learning series - installation and use of docker compose

Keywords: Linux Docker sudo github curl

Docker Compose is a docker tool used to define and run complex applications Using Docker Compose eliminates the need for shell scripts to start containers. (configured through docker-compose.yml)

Installation of Docker Compose

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
# Add executable permissions to docker compose
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
# Add executable permissions to docker compose
sudo chmod +x /usr/local/bin/docker-compose

Uninstallation of Docker Compose

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

View the version of Docker Compose

docker-compose --version

Configure Dockerfile

#Specify the base image and customize it
FROM java:8

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

#Set working directory
WORKDIR /apps/demo

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

#bash mode to make demo-1.0.0.jar accessible
#RUN creates a new layer and executes these commands on it. After execution, the modification of commit layer forms a new image.
RUN bash -c "touch /demo-1.0.0.jar"

#Declare that the runtime container provides the service port. This is just a declaration. At runtime, the application will not open the service of this port because of this declaration
EXPOSE 8080

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

Configure the docker-compose.yml file

# Edition
version: '3.0'
services:
  demo:
    # build is used to specify the file path of the Dockerfile
    build: .
    # Mapping port
    ports:
    - "8080:8080"
    volumes:  #Specify a file directory to store container data.
    #$PWD indicates the current path
    - $PWD/data:/var/lib/log

Common commands for docker compose

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

Run the docker compose command to build a running image

  1. First, create a new folder in the host to store the Dockerfile,docker-compose.yml and the jar package we have made.
  2. Enter the directory first, run the down command, and delete all the images created before.
  3. Run the build command to generate the image.
  4. Run the up command to start the container.
  5. Access the ip + port number, you can see our program.

Posted by Rhiknow on Wed, 20 Nov 2019 10:22:37 -0800