Saturday, 9 May 2020

Working with Docker compose

In a typical application, we may need to use a web server, database, cache etc.,

In plain approach, we will run the container using ‘docker run’ command

docker run mysql
docker run redis:alpine
docker run tomcat
docker run mongodb

We can even execute the above commands using ‘docker compose’ in a better way. Using ‘docker compose’ we can create a configuration file in ‘YAML’ format that contains all the images that we required to deploy our application.

docker-compose.yaml
version: '2'
services:
    web:
        image: tomcat
        container_name: user_service
        volumes:
            - '.:/src'
        restart: always
    database:
        image: mysql
        container_name: user_database
        environment:
            - MYSQL_ROOT_PASSWORD=tiger
        restart: always
    caching:
        image: 'redis:alpine'
        container_name: user_cache
        restart: always

Navigate to the directory where 'docker-compose.yaml' is located, execute the command ‘docker-compose up’ to set up all the containers.
$docker-compose up
Creating network "docker_default" with the default driver
Pulling web (tomcat:)...
latest: Pulling from library/tomcat
90fe46dd8199: Already exists
35a4f1977689: Already exists
bbc37f14aded: Already exists
74e27dc593d4: Already exists
93a01fbfad7f: Pull complete
35b994955649: Pull complete
7f9f18312a34: Pull complete
574205fe650b: Pull complete
229fea8c518f: Pull complete
6306f7102640: Pull complete
Digest: sha256:cae591b6f798359b0ba2bdd9cc248e695ac6e14d20722c5ff82a9a138719896f
Status: Downloaded newer image for tomcat:latest
Pulling caching (redis:alpine)...
alpine: Pulling from library/redis
cbdbe7a5bc2a: Pull complete
dc0373118a0d: Pull complete
cfd369fe6256: Pull complete
e5396613619b: Pull complete
6809b5ad2cd4: Pull complete
386ecfe54d06: Pull complete
Digest: sha256:2586f31f74ac1d7dc6f6c7eabca42f09bba5ec9911fc519d55fbd7508a9c4f01
Status: Downloaded newer image for redis:alpine
Creating user_database ... done
Creating user_cache    ... done
Creating user_service  ... done
Attaching to user_cache, user_database, user_service
user_cache  | 1:C 09 May 2020 15:12:22.258 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
user_cache  | 1:C 09 May 2020 15:12:22.258 # Redis version=6.0.1, bits=64, commit=00000000, modified=0, pid=1, just started
user_cache  | 1:C 09 May 2020 15:12:22.258 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
user_database | 2020-05-09 15:12:22+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.20-1debian10 started.
user_cache  | 1:M 09 May 2020 15:12:22.260 * Running mode=standalone, port=6379.
user_cache  | 1:M 09 May 2020 15:12:22.260 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
user_cache  | 1:M 09 May 2020 15:12:22.260 # Server initialized
user_cache  | 1:M 09 May 2020 15:12:22.260 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
user_cache  | 1:M 09 May 2020 15:12:22.260 * Ready to accept connections
user_service | NOTE: Picked up JDK_JAVA_OPTIONS:  --add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.base/java.io=ALL-UNNAMED --add-opens=java.rmi/sun.rmi.transport=ALL-UNNAMED
user_database | 2020-05-09 15:12:22+00:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
user_database | 2020-05-09 15:12:22+00:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 8.0.20-1debian10 started.
user_database | 2020-05-09 15:12:22+00:00 [Note] [Entrypoint]: Initializing database files
user_database | 2020-05-09T15:12:22.498040Z 0 [Warning] [MY-011070] [Server] 'Disabling symbolic links using --skip-symbolic-links (or equivalent) is the default. Consider not using this option as it' is deprecated and will be removed in a future release.
user_database | 2020-05-09T15:12:22.498150Z 0 [System] [MY-013169] [Server] /usr/sbin/mysqld (mysqld 8.0.20) initializing of server in progress as process 44
user_database | 2020-05-09T15:12:22.503445Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
user_database | 2020-05-09T15:12:22.927650Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
user_service | 09-May-2020 15:12:23.100 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version name:   Apache Tomcat/9.0.34
user_service | 09-May-2020 15:12:23.108 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server built:          Apr 3 2020 12:02:52 UTC
user_service | 09-May-2020 15:12:23.109 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Server version number: 9.0.34.0
user_service | 09-May-2020 15:12:23.109 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Name:               Linux
user_service | 09-May-2020 15:12:23.110 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log OS Version:            4.19.76-linuxkit
user_service | 09-May-2020 15:12:23.110 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Architecture:          amd64
user_service | 09-May-2020 15:12:23.110 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Java Home:             /usr/local/openjdk-11
user_service | 09-May-2020 15:12:23.111 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Version:           11.0.7+10
user_service | 09-May-2020 15:12:23.111 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log JVM Vendor:            Oracle Corporation
user_service | 09-May-2020 15:12:23.111 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_BASE:         /usr/local/tomcat
user_service | 09-May-2020 15:12:23.111 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_HOME:         /usr/local/tomcat
user_service | 09-May-2020 15:12:23.133 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: --add-opens=java.base/java.lang=ALL-UNNAMED
user_service | 09-May-2020 15:12:23.134 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: --add-opens=java.base/java.io=ALL-UNNAMED
user_service | 09-May-2020 15:12:23.134 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: --add-opens=java.rmi/sun.rmi.transport=ALL-UNNAMED
user_service | 09-May-2020 15:12:23.134 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.config.file=/usr/local/tomcat/conf/logging.properties
user_service | 09-May-2020 15:12:23.135 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
user_service | 09-May-2020 15:12:23.137 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djdk.tls.ephemeralDHKeySize=2048
user_service | 09-May-2020 15:12:23.137 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.protocol.handler.pkgs=org.apache.catalina.webresources
user_service | 09-May-2020 15:12:23.137 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dorg.apache.catalina.security.SecurityListener.UMASK=0027
user_service | 09-May-2020 15:12:23.137 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dignore.endorsed.dirs=
user_service | 09-May-2020 15:12:23.138 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.base=/usr/local/tomcat
user_service | 09-May-2020 15:12:23.138 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.home=/usr/local/tomcat
user_service | 09-May-2020 15:12:23.138 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.io.tmpdir=/usr/local/tomcat/temp
user_service | 09-May-2020 15:12:23.138 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent Loaded APR based Apache Tomcat Native library [1.2.23] using APR version [1.6.5].
user_service | 09-May-2020 15:12:23.138 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true].
user_service | 09-May-2020 15:12:23.139 INFO [main] org.apache.catalina.core.AprLifecycleListener.lifecycleEvent APR/OpenSSL configuration: useAprConnector [false], useOpenSSL [true]
user_service | 09-May-2020 15:12:23.151 INFO [main] org.apache.catalina.core.AprLifecycleListener.initializeSSL OpenSSL successfully initialized [OpenSSL 1.1.1d  10 Sep 2019]
user_service | 09-May-2020 15:12:23.467 INFO [main] org.apache.coyote.AbstractProtocol.init Initializing ProtocolHandler ["http-nio-8080"]
user_service | 09-May-2020 15:12:23.501 INFO [main] org.apache.catalina.startup.Catalina.load Server initialization in [691] milliseconds
user_service | 09-May-2020 15:12:23.560 INFO [main] org.apache.catalina.core.StandardService.startInternal Starting service [Catalina]
user_service | 09-May-2020 15:12:23.561 INFO [main] org.apache.catalina.core.StandardEngine.startInternal Starting Servlet engine: [Apache Tomcat/9.0.34]
user_service | 09-May-2020 15:12:23.574 INFO [main] org.apache.coyote.AbstractProtocol.start Starting ProtocolHandler ["http-nio-8080"]
user_service | 09-May-2020 15:12:23.599 INFO [main] org.apache.catalina.startup.Catalina.start Server startup in [96] milliseconds
user_database | 2020-05-09T15:12:23.973925Z 6 [Warning] [MY-010453] [Server] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
user_database | 2020-05-09 15:12:26+00:00 [Note] [Entrypoint]: Database files initialized
user_database | 2020-05-09 15:12:26+00:00 [Note] [Entrypoint]: Starting temporary server
user_database | 2020-05-09T15:12:26.791751Z 0 [Warning] [MY-011070] [Server] 'Disabling symbolic links using --skip-symbolic-links (or equivalent) is the default. Consider not using this option as it' is deprecated and will be removed in a future release.
user_database | 2020-05-09T15:12:26.791936Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.20) starting as process 91
user_database | 2020-05-09T15:12:26.817735Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
user_database | 2020-05-09T15:12:27.021086Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
user_database | 2020-05-09T15:12:27.123241Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Socket: '/var/run/mysqld/mysqlx.sock'
user_database | 2020-05-09T15:12:27.235432Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
user_database | 2020-05-09T15:12:27.237305Z 0 [Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.
user_database | 2020-05-09T15:12:27.267218Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.20'  socket: '/var/run/mysqld/mysqld.sock'  port: 0  MySQL Community Server - GPL.
user_database | 2020-05-09 15:12:27+00:00 [Note] [Entrypoint]: Temporary server started.
user_database | Warning: Unable to load '/usr/share/zoneinfo/iso3166.tab' as time zone. Skipping it.
user_database | Warning: Unable to load '/usr/share/zoneinfo/leap-seconds.list' as time zone. Skipping it.
user_database | Warning: Unable to load '/usr/share/zoneinfo/zone.tab' as time zone. Skipping it.
user_database | Warning: Unable to load '/usr/share/zoneinfo/zone1970.tab' as time zone. Skipping it.
user_database | 
user_database | 2020-05-09 15:12:30+00:00 [Note] [Entrypoint]: Stopping temporary server
user_database | 2020-05-09T15:12:30.229222Z 10 [System] [MY-013172] [Server] Received SHUTDOWN from user root. Shutting down mysqld (Version: 8.0.20).
user_database | 2020-05-09T15:12:32.640263Z 0 [System] [MY-010910] [Server] /usr/sbin/mysqld: Shutdown complete (mysqld 8.0.20)  MySQL Community Server - GPL.
user_database | 2020-05-09 15:12:33+00:00 [Note] [Entrypoint]: Temporary server stopped
user_database | 
user_database | 2020-05-09 15:12:33+00:00 [Note] [Entrypoint]: MySQL init process done. Ready for start up.
user_database | 
user_database | 2020-05-09T15:12:33.482686Z 0 [Warning] [MY-011070] [Server] 'Disabling symbolic links using --skip-symbolic-links (or equivalent) is the default. Consider not using this option as it' is deprecated and will be removed in a future release.
user_database | 2020-05-09T15:12:33.482821Z 0 [System] [MY-010116] [Server] /usr/sbin/mysqld (mysqld 8.0.20) starting as process 1
user_database | 2020-05-09T15:12:33.490767Z 1 [System] [MY-013576] [InnoDB] InnoDB initialization has started.
user_database | 2020-05-09T15:12:33.686378Z 1 [System] [MY-013577] [InnoDB] InnoDB initialization has ended.
user_database | 2020-05-09T15:12:33.779968Z 0 [System] [MY-011323] [Server] X Plugin ready for connections. Socket: '/var/run/mysqld/mysqlx.sock' bind-address: '::' port: 33060
user_database | 2020-05-09T15:12:33.892878Z 0 [Warning] [MY-010068] [Server] CA certificate ca.pem is self signed.
user_database | 2020-05-09T15:12:33.895368Z 0 [Warning] [MY-011810] [Server] Insecure configuration for --pid-file: Location '/var/run/mysqld' in the path is accessible to all OS users. Consider choosing a different directory.
user_database | 2020-05-09T15:12:33.919051Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.0.20'  socket: '/var/run/mysqld/mysqld.sock'  port: 3306  MySQL Community Server - GPL.

After successful execution of docker -compose command, open other terminal, and confirm the containers by listing.
$docker container ls
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                 NAMES
a6260323ed5f        tomcat              "catalina.sh run"        35 seconds ago      Up 34 seconds       8080/tcp              user_service
f596641dfd12        mysql               "docker-entrypoint.s…"   35 seconds ago      Up 34 seconds       3306/tcp, 33060/tcp   user_database
95c90eaa05bd        redis:alpine        "docker-entrypoint.s…"   35 seconds ago      Up 34 seconds       6379/tcp              user_cache

You can use -d (docker-compose up -d) option to execute the command in the background.

If the docker -compose is running in the background, you can use the below command to see the status.

docker -compose ps

Can I stop docker -compose command?
Yes, execute below command.
docker -compose down



Previous                                                    Next                                                    Home

No comments:

Post a Comment