This article is a shortcut of docker tools. These tools are commonly used in micro-service architecture:
- Docker
- Docker Compose
Docker
Docker can be used interactively, from console. Below most useful commands:
docker ps
- show all running container
docker images
- show images in local repository
docker run -d [image_name]
- run image in daemon mode
docker exec -it [container_id] "[command to run in the container, ex /bin/sh]"
- plug in and execute command on specific container
docker container logs [container_id]
- print logs from container
docker pull [image name]
- pull image from external images repository
but the biggest benefit of docker is that can be used by scripts, so all process is repeatable and can be automatized. Default docker file is Dockerfile. Below some example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | # base image to this build FROM openjdk:8-jdk-alpine # define what directory should be mounted to host # - mounted directories are created in /var/lib/docker/volumes VOLUME /tmp # only inform what ports can expose application EXPOSE 8080 # define variable ARG JAR_FILE=target/*.jar # define variable using environment variable, or "v.1.0.0" if not defined. #ENV override ARG variable. Example execution with variable: # $ docker build --build-arg CONT_IMG_VER=v2.0.1 . ENV SOME_ENV_VAR ${CONT_IMG_VER:-v1.0.0} #copy file from host to container storage COPY ${JAR_FILE} app.jar #copy file from host to container storage, but comparing to COPY # can also get file from url and extract tar file ADD ${JAR_FILE} app.jar # run command in container RUN uname -a # health check command - docker is checking if application is working properly HEALTHCHECK --interval=5m --timeout=3s --retries=5 \ CMD curl -f http://localhost/ || exit 1 # run application as goal of this image ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"] |
having Dockerfile it is executed command:
docker build
and then if this image have to be pushed to remote repository
docker image tag [image_tag_name] [docker repository path]
docker push [docker repository path]
There is also possible to start a repository on docker container, executing a command:
docker run -d -p 5000:5000 --name registry registry:2
and to remove container registry container
docker container stop registry && docker container rm -v registry
Docker Compose
docker-compose build
- build images included in file docker-compose.ymldocker-compose up -d
- run containers in daemon modedocker-compose down
- stop containersdocker-compose logs
- print logs from containersand docker-compose.yml file example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | # version of file format version: "3.3" # definition of services (container templates) services: #name of service mongoDB: # image name - this image is retrieved from remote repository image: library/mongo:4.4.0 # container name container_name: "mongoDBcontainerName" # what if application is dead restart: on-failure # ports which should be exposed to host (host port: container port) ports: - 27017:27017 # images have defined variables, this way are defined their values environment: MONGO_INITDB_ROOT_USERNAME: sboot MONGO_INITDB_ROOT_PASSWORD: example MONGO_INITDB_DATABASE: test # storage mapping ( host : containers path : access mode) volumes: - ./src/main/sql/mongo-init.js:/docker-entrypoint-initdb.d/mongo-init.js:ro app: # build properties - this service will be built build: # where is context path on host context: ./ # docker file dockerfile: Dockerfile container_name: "myApp" # definition of depending on services depends_on: - mongoDB # this defines in container dns names for depending on services links: - mongoDB |
To prepare this article I used:
- Docker in version 19.03.6 - provided by system
- Docker Compose in version 1.17.1 - provided by system
No comments:
Post a Comment