Skip to content

Instantly share code, notes, and snippets.

@ReeganExE
Last active January 14, 2018 11:38
Show Gist options
  • Save ReeganExE/a20a33df6efd8aeb2d9dc5acc9e04254 to your computer and use it in GitHub Desktop.
Save ReeganExE/a20a33df6efd8aeb2d9dc5acc9e04254 to your computer and use it in GitHub Desktop.
Some Docker commands

Docker

Dockerfile vs docker-compose.yml

Dockerfile defines an Docker image

docker-compose.yml predefines Docker containers

Commands:

Basic Docker commands

# Show list all images
docker images

# Remove an image
docker image rm <id or name>

# Show running Containers
docker ps

# Show all containers
docker ps -a

# Remove a container
docker rm <id or name>

Docker Compose

### cwd to directory of docker-compose.yml

# Run all containers in a compose file
docker-compose up -d
## -d means detach, it will run containers and detach from stdout

# Run a container
docker-compose up <name>

# Stop a container
docker-compose stop <name>

# Stop and remove a container
docker-compose down <name>

# Stop and remove all container
docker-compose down

An example of docker-compose.yml

eureka:
  image: eureka-service
  ports:
    - "8761:8761"

zuul:
  image: gs-routing-and-filtering/zuul
  ports:
    - "80:8080" # HOST_PORT:GUEST_PORT
  environment:
    - SPRING_APPLICATION_NAME=zuul-proxy
    - "EUREKA_CLIENT_SERVICEURL_DEFAULTZONE=http://eureka:8761/eureka/"
  links:
    - eureka

An example of docker-compose.dev.yml (used for development )

eureka:
  build: eureka-service # <======== look up Dockerfile in ./eureka-service directory
  ports:
    - "8761:8761"

Multiple Compose files (inheritance)

docker-compose -f docker-compose.yml -f docker-compose.dev.yml up

docker-compose.dev.yml inherits docker-compose.yml with additional possibility to build images locally and expose all containers ports for convenient development.

See more: Docs

Example: docker-compose.yml and docker-compose.dev.yml

Gradle Build Script

buildscript {
    ext {
        springBootVersion = '1.5.8.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
+       classpath('se.transmode.gradle:gradle-docker:1.2')
    }
}

apply plugin: 'java'
+apply plugin: 'docker'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'

jar {
    baseName = 'spring-boot-multitenant'
    version = '0.0.1-SNAPSHOT'
}
sourceCompatibility = 1.8
targetCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencyManagement {
    imports {
        mavenBom 'org.springframework.cloud:spring-cloud-dependencies:Camden.SR5'
    }
}

dependencies {
    compile('org.apache.commons:commons-lang3:3.0')
    compile('org.springframework.boot:spring-boot-starter-actuator')
    compile('org.springframework.boot:spring-boot-starter-data-jpa')
    compile('org.springframework.boot:spring-boot-starter-data-rest')
    compile('org.springframework.cloud:spring-cloud-starter-eureka')
    compile('org.springframework.boot:spring-boot-starter-web')
    runtime('mysql:mysql-connector-java')
    testCompile('org.springframework.boot:spring-boot-starter-test')
}

+task buildDocker(type: Docker, dependsOn: build) {
+  // push = true
+  applicationName = jar.baseName
+  dockerfile = file('src/main/docker/Dockerfile')
+  doFirst {
+    copy {
+      from jar
+      into stageDir
+    }
+  }
+}

Run build: ./gradlew buildDocker -x test

-x test means exclude run test task.

View IP Address

docker inspect <name|id> | grep -i ip
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment