Skip to content

Instantly share code, notes, and snippets.

@rselva
Last active September 13, 2018 08:21
Show Gist options
  • Save rselva/9e9de8458111aa34c223820210696b51 to your computer and use it in GitHub Desktop.
Save rselva/9e9de8458111aa34c223820210696b51 to your computer and use it in GitHub Desktop.

Part-1. Getting started - Docker environment

Docker CLI

docker [resource/object] [command] [options] [args]

Display Docker version and info

1. $ docker --version
2. $ docker version
3. $ docker info

Explore Docker Platform

4. $ docker image ls (list images)
5. $ docker container ls (or ps) (list containers)
6. $ docker volume ls
7. $ docker network ls 

Part-2. Create and Run first container

Observe the lifecycle of containers Created, Running , Stopped

Pull image (Minimal linux)

1. $ docker image pull alpine
2. $ docker image ls
3. $ docker image history alpine

Create, start, stop, attach, remove container

4. $ docker container create -it alpine 
5. $ docker container ps -a
6. $ docker container create -it --name alpine1 alpine
7. $ docker container start alpine1
8. $ docker container attach alpine1
	/# cat /etc/os-release ; exit
9 $ docker container start alpine1; 
## Running commands in a live container
10. $ docker container exec alpine1 cat /etc/issue 
11. $ docker container rm --force alpine1

Part-3. Detached mode & exec commands

1. $ docker container run -it --name alpine2 --rm alpine
	/# ps aux; exit
2. $ docker container run -d -it --name alpine2 alpine (detached)
3. $ docker container exec alpine2 cat /etc/os-release
4. $ docker container attach alpine2
	/$ exit

Part-4. Container logs ( STDOUT of container)

TERMINAL 1
1. docker container run -it --name alpine3 --rm alpine
	/# ps aux
TERMINAL 2
2. docker container logs -f alpine3
3. docker container logs -f alpine3

Part-5. We are big chill (Example)

Using a shared image

1. $ docker container run --rm --name wearebigchill -p 8080:80 rselva/wearebigchill:beta

Play @ http://localhost:8080

Building this project on your own

1. $ git clone https://github.com/spkane/wearebigchill.git --config core.autocrlf=input
2. $ cd wearebigchill
3. $ docker image build -t wearebigchill:beta .
4. $ docker image ls
5. $ docker container run --rm --name wearebigchill -p 8080:80  wearebigchill:beta
6. $ docker container rm --force wearebigchill
7. $ docker container run --rm --name wearebigchill -p 8080:80 -e "THEME=2" wearebigchill:beta

Part-6. Volumes : Managing storage

a. Create volume; run a container with that volume; delete container; run another container with the same volme

1. $ docker volume create user-db-volume
2. $ docker container run -it --rm -v user-db-volume:/home/db --name bb1 busybox sh
   /# (Add some data)
3. $ docker container run -it --rm -v user-db-volume:/home/db alpine sh
	#/ (look around)

b. Static web application with nginx ; share folder with host

1. $ cd temp; mkdir html ; echo "<h2>Welcom to my blog!</h1>" > html\index.html
2. $  docker container run -d -p 8080:80 -v E:/temp/html:/usr/share/nginx/html --name docker-nginx nginx
3. $ http://localhost:8080
4. $ docker container rm --force docker-nginx

Part-7. Dockerizing an applicatiion - Creating an image, Running a container, Sharing it

a. Java Application-> Save as Application.java

import java.time.Instant;
public class Application {

	public static void main(String[] args) {
		System.out.println("Time now is "+ Instant.now());
	}
	
}

b. Dockerfile -> Save as Dockerfile

FROM openjdk:8-jdk-alpine3.8
COPY . /usr/src/myapp
WORKDIR /usr/src/myapp
RUN javac Application.java
CMD ["java", "Application"]

c. Creating image

$ docker image build -t {tag} {build context}

$ docker image build -t time:beta .
$ docker image ls

d. Creating Container

$ docker container run time:beta
$ docker container run --name time-instance time:beta

e. Sharing (shipping an image)

$ docker login
$  cat ~/.docker/config.json
Tag image prefix and push
$ docker image tag time:beta ${HUB_USER}/time:beta (rselva/time:beta)
$ docker image push rselva/time:beta 
Test it
$ docker image rm rselva/time:beta
$ docker iamge ls
$ docker container run --rm rselva/time:beta

Part-8. Accessing Docker host from containers

$ docker run -it --cap-add SYS_ADMIN --cap-add SYS_PTRACE --pid=host alpine nsenter -t 1  -m -u -n -i sh
$ cat etc/os-release
$ cat etc/issue
$ exit

Part-9. Saving the container state (commit)

Create container and add some files

1. $ docker images
2. $ docker image tag alpine:latest alpine:beta
3. $ docker container run --name myalpine -it alpine:beta sh
	#/echo "Docker class" > class.txt

Commit container and tag image

4. $ docker container commit myalpine alpine:beta2

Check history

5. $ docker image history alpine:beta2
6. $ docker image history alpine:beta

Part-10. Store App example

Store-App
+ product-service
+ web-app //Not covered today
+ mongodb

Step 1. Clone the repo or download from https://github.com/rselva/store-app.git

1. $ git clone https://github.com/rselva/store-app.git
2. $ cd store-app

Step 2. Product service

Build with Maven

1. $ cd product-service
2. $ mvn clean package   --> .\target\product-service-0.1.0.jar
3. $ cat Dockerfile
	FROM openjdk:8-jdk-alpine
	VOLUME /tmp
	ARG JAR_FILE
	COPY ${JAR_FILE} app.jar
	ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
4. $ docker image build --build-arg JAR_FILE=target/product-service-0.1.0.jar -t rselva/product-service:beta . 

Run product service as standalone container

6. $ docker container run --rm -d -p 8080:8080 --name product-service rselva/product-service:beta
	docker container logs -f -t product-service
	http://localhost:8080
## Clean up
7. $ docker container rm --force product-service

Step-3. Composing all services

Docker-Compose

  1. Declartive way to create an application stack.
  2. Compose the application from services
1. docker-compose build --no-cache
2. docker-compose up (-d for running in background)
3. docker-compose ps
4. docker-compose logs product-service
5. docker-compose exec product-service ps
Exploer docker-compose.yml
version: '3.1'

services:
  product-service:
    build:
      context: product-service\.
      args:
        JAR_FILE: ${JAR_FILE}
    image: rselva/product-service:beta
    environment:
      DB: mongo
    ports:
      - 8080:8080
    depends_on:
      - mongo
    networks:
      - devnet
      
  mongo:
    image: mongo
    restart: always
    ports:
      - 27017:27017
    volumes:
      - model-data-volume:/data/db
      - model-config-volume:/data/configdb
    environment:
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: hauX123
    networks:
      - devnet

  mongo-express:
    image: mongo-express
    restart: always
    ports:
      - 8081:8081
    depends_on:
      - mongo
    environment:
      ME_CONFIG_MONGODB_ADMINUSERNAME: root
      ME_CONFIG_MONGODB_ADMINPASSWORD: hauX123
    networks:
    - devnet
    
volumes:
  model-data-volume:
  model-config-volume:
networks:
  devnet:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment