Showing posts with label docker. Show all posts
Showing posts with label docker. Show all posts

Saturday, 9 May 2020

Docker Network: example 2: Connecting to sql server

This is continuation to my previous post. In this post I am going to explain how to connect to MySQL server running in one container from other.

Step 1: Create a network.
Execute below command.
docker network create my_network_1
$docker network create my_network_1
75bee5bf8384a1631267ddfe91dc30e4a61b563a05af6a56f275b04d0d932eb5
Step 2: You can confirm the network creation by listing all the networks.
$docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
f2fb9b25f38f        bridge              bridge              local
f0b207369485        host                host                local
75bee5bf8384        my_network_1        bridge              local
7e1bfc494e53        none                null                local
Step 3: Add MySQL container to the network my_network_1.
docker run --rm -d -e MYSQL_ROOT_PASSWORD=tiger --net my_network_1 --name mysql_container_1 mysql:latest
$docker run --rm -d -e MYSQL_ROOT_PASSWORD=tiger --net my_network_1 --name mysql_container_1 mysql:latest
6876928fe4be475a014b93e203c189f4c6f71d36964e77964b5e28cf1029c22b
Step 4: Add another MySQL container to the network my_network_1 and connect to the first mySQL container from it.

docker run -it --rm -e MYSQL_ROOT_PASSWORD=tiger123 --net my_network_1 --name mysql_container_2 mysql:latest /bin/bash
$docker run -it --rm -e MYSQL_ROOT_PASSWORD=tiger123 --net my_network_1 --name mysql_container_2 mysql:latest /bin/bash
root@7ec9b132b955:/#

Connect to ‘mysql_container_1’ using mysql command.
mysql -h mysql_container_1 -u root -p

root@7ec9b132b955:/# mysql -h mysql_container_1 -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.20 MySQL Community Server - GPL

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>


Previous                                                    Next                                                    Home

Docker Network: Communicate between containers

‘docker network’ command is used to establish communication between containers.

Step 1: Create a network.
docker network create my_network_1
$docker network create my_network_1
4e7b04536608fd84cc1bfad3fb45205063417df181f72f18b8eb3b6cae825a0d


Step 2: List all the networks.
docker network ls
$docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
f2fb9b25f38f        bridge              bridge              local
f0b207369485        host                host                local
4e7b04536608        my_network_1        bridge              local
7e1bfc494e53        none                null                local

Step 3: Add a container to the network ‘my_network_1’.

sleep.py
import time
while True:
    time.sleep(2)

Execute the below command to start the container container_1.

docker run --rm -d --net my_network_1 --name container_1 -v $(pwd):/src python:3 python3 /src/sleep.py

--rm: Remove the container once the command finish execution.
-d: Run the container in background, so we can't see all the log messaages of docker in terminal.
--net my_network_1: Add the container to network my_network_1.
--name container_1: Name of the container is container_1.
-v $(pwd):/src: Mount current directory to /src folder.
python:3: Image name with tag
python3 /src/sleep.py:  Command to run

$docker run --rm -d --net my_network_1 --name container_1 -v $(pwd):/src python:3 python3 /src/sleep.py
32e743cbe40f49ed9f581e2f5e4ff93f765df6ecb1724f1063b0f09ff91b9ab6

You can execute the command ‘docker container ls’ to confirm that the container is up and running.
$docker container ls
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
32e743cbe40f        python:3            "python3 /src/sleep.…"   19 seconds ago      Up 18 seconds                           container_1

Step 4: Add the second container to the network ‘my_network_1’.

docker run --rm -it --net my_network_1 --name container_2 python:3 /bin/bash

Above command runs the container container_2 with image python:3 in interactive mode.
$docker run --rm -it --net my_network_1 --name container_2 python:3 /bin/bash
root@052aac3ac7c4:/#

Since both the containers container_1, container_2 are in the same network, you can ping the container ‘container_1’ from container_2.

root@052aac3ac7c4:/# ping container_1
PING container_1 (172.18.0.2) 56(84) bytes of data.
64 bytes from container_1.my_network_1 (172.18.0.2): icmp_seq=1 ttl=64 time=0.086 ms
64 bytes from container_1.my_network_1 (172.18.0.2): icmp_seq=2 ttl=64 time=0.174 ms
64 bytes from container_1.my_network_1 (172.18.0.2): icmp_seq=3 ttl=64 time=0.168 ms
64 bytes from container_1.my_network_1 (172.18.0.2): icmp_seq=4 ttl=64 time=0.274 ms
64 bytes from container_1.my_network_1 (172.18.0.2): icmp_seq=5 ttl=64 time=0.170 ms
64 bytes from container_1.my_network_1 (172.18.0.2): icmp_seq=6 ttl=64 time=0.166 ms
^C
--- container_1 ping statistics ---
6 packets transmitted, 6 received, 0% packet loss, time 104ms
rtt min/avg/max/mdev = 0.086/0.173/0.274/0.054 ms

Come out of the container by executing ‘exit’ command.

Stop the container ‘container_1’.

$docker container ls
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
32e743cbe40f        python:3            "python3 /src/sleep.…"   6 minutes ago       Up 6 minutes                            container_1
$
$docker container stop container_1

container_1
$
$
$docker container ls
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

Remove the network ‘my_network_1’ by executing the below command.
docker network rm my_network_1

$docker network rm my_network_1
my_network_1
$
$docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
f2fb9b25f38f        bridge              bridge              local
f0b207369485        host                host                local
7e1bfc494e53        none                null                local
$



Previous                                                    Next                                                    Home

Docker: Nginx container: hello world

In this post, I am going to explain how to run nginx container and serve html pages.

Step 1: Create HTML pages.

index.html
<html>
 <head>
  <title>Hello World</title>
 </head>

 <body>
  <h1>Hello World</h1>
 </body>
</html>

welcome.html
<html>
 <head>
  <title>Welcome</title>
 </head>

 <body>
  <h1>Welcome Dear Customer!!!!!!</h1>
 </body>
</html>

Step 2: Execute the below command to start Nginx container.

docker run --rm -v $(pwd):/usr/share/nginx/html -p 1234:80 --name my_nginx nginx:latest

--rm: Remove this container once the command execution done.
-v $(pwd):/usr/share/nginx/html: Mount current working directory to /usr/share/nginx/html folder.
-p 1234:80: Whatever the request comes to localhost 1234 port, redirect it to container port 80.
--name my_nginx: 'my_nginx' is the name of container.
nginx:latest: Pull latest version of nginx.

$docker run --rm -v $(pwd):/usr/share/nginx/html -p 1234:80 --name my_nginx nginx:latest
Unable to find image 'nginx:latest' locally
latest: Pulling from library/nginx
54fec2fa59d0: Already exists 
4ede6f09aefe: Pull complete 
f9dc69acb465: Pull complete 
Digest: sha256:86ae264c3f4acb99b2dee4d0098c40cb8c46dcf9e1148f05d3a51c4df6758c12
Status: Downloaded newer image for nginx:latest

Open the url ‘http://localhost:1234’, you will see the content of index.html file.


Open the url ‘http://localhost:1234/welcome.html’ to see the content of welcome.html file.

Login to the container my_nginx
Using ‘docker exec’ command, you can login to the container my_nginx.

docker exec -it my_nginx /bin/bash

$docker exec -it my_nginx /bin/bash
root@0973badab5e5:/#



Previous                                                    Next                                                    Home

Docker: Run MySQL in a container

Step 1: Open a terminal and execute the below command.

docker run -d -e MYSQL_ROOT_PASSWORD=tiger mysql:latest

-d: Run the container in background, so we can't see all the log messages of docker in terminal.
-e: Used to set Environment Variables.
$docker run -d -e MYSQL_ROOT_PASSWORD=tiger mysql:latest
Unable to find image 'mysql:latest' locally
latest: Pulling from library/mysql
54fec2fa59d0: Pull complete 
bcc6c6145912: Pull complete 
951c3d959c9d: Pull complete 
05de4d0e206e: Pull complete 
319f0394ef42: Pull complete 
d9185034607b: Pull complete 
013a9c64dadc: Pull complete 
42f3f7d10903: Pull complete 
c4a3851d9207: Pull complete 
82a1cc65c182: Pull complete 
a0a6b01efa55: Pull complete 
bca5ce71f9ea: Pull complete 
Digest: sha256:61a2a33f4b8b4bc93b7b6b9e65e64044aaec594809f818aeffbff69a893d1944
Status: Downloaded newer image for mysql:latest
122d27c2a3467700df7fbf20f0b919da2cade9eadb30c745b35b42fbec1dff6f

Step 2: Run MySQL command in the running container using 'docker exec'

Syntax
docker exec -it {container_id/container_name} {command_to_execute}

Get all the running containers
$docker container ls
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                 NAMES
122d27c2a346        mysql:latest        "docker-entrypoint.s…"   54 seconds ago      Up 53 seconds       3306/tcp, 33060/tcp   elastic_tesla
As you see ‘elastic_tesla’ is the container name. Now execute below command.

docker exec -it elastic_tesla mysql -h localhost -u root -p

Enter the password as tiger.

$docker exec -it elastic_tesla mysql -h localhost -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.20 MySQL Community Server - GPL

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

Note
a. You can specify container name using –name option.

docker run -d --name krishna_sql -e MYSQL_ROOT_PASSWORD=tiger mysql:latest


Previous                                                    Next                                                    Home

Docker: Run the bash shell in interactive mode

Below command pulls python image if not exist and runs the bash shell in interactive mode.

docker run -it --rm python:3 /bin/bash

-it: Run the container in interactive mode.
--rm: Remove the container after its execution
/bin/bash: Command to be executed.

$docker run -it --rm python:3 /bin/bash
root@8e7afceedc1e:/#

root@8e7afceedc1e:/# pwd
/
root@8e7afceedc1e:/# ls -a
.  ..  .dockerenv  bin boot  dev  etc home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
root@8e7afceedc1e:/#

Execute the command ‘exit’ to come out of the container.

root@8e7afceedc1e:/# exit
exit
$


Previous                                                    Next                                                    Home

Docker : Run Python Interpreter

Execute below command to open python interpreter.
docker run -it --rm python:3

-it: Run the container in interactive mode.
--rm: Remove the container after its execution.

Since I am not specified the command to run, it runs python interpreter by default.
$docker run -it --rm python:3
Python 3.8.2 (default, Apr 23 2020, 14:22:33) 
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

Now, you can execute any python valid commands in the interpreter.
>>> print("Hello World")
Hello World
>>> 
>>> 10 + 11
21
>>>

Execute the command exit() to come out of interpreter prompt.
>>> exit()
$


Previous                                                    Next                                                    Home

Docker: Python: Hello World

Step 1: Create hello.py file
hello.py
print("Hello World")

Step 2: Execute the below command to download python image and run hello.py file.
docker run -v $(pwd):/src --rm python:3 python /src/hello.py

-v $(pwd):/src: Mount current working directory to /src folder of container
--rm: Remove the container after its execution.
python:3: I want to use the python image with tag 3
python hello.py: Run hello.py file.

$docker run -v $(pwd):/src --rm python:3 python /src/hello.py
Unable to find image 'python:3' locally
3: Pulling from library/python
90fe46dd8199: Pull complete 
35a4f1977689: Pull complete 
bbc37f14aded: Pull complete 
74e27dc593d4: Pull complete 
4352dcff7819: Pull complete 
deb569b08de6: Pull complete 
98fd06fa8c53: Pull complete 
7b9cc4fdefe6: Pull complete 
512732f32795: Pull complete 
Digest: sha256:ad7fb5bb4770e08bf10a895ef64a300b288696a1557a6d02c8b6fba98984b86a
Status: Downloaded newer image for python:3
Hello World

Since I do not have python:3 image in my system, docker downloads the image for the first time. Next time onwards, it will not download python:3 image and use the image from local registry.
$docker run -v $(pwd):/src --rm python:3 python /src/hello.py
Hello World
$
$docker run -v $(pwd):/src --rm python:3 python /src/hello.py
Hello World

You can see list of images using ‘docker image ls’ command.

$docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
python              3                   4f7cd4269fa9        9 days ago          934MB



Previous                                                    Next                                                    Home

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

Docker: Spring boot: Hello World

Step 1: Create a directory ‘helloworld’.

Step 2: Navigate to ‘helloworld’ folder and clone following git repository ‘https://github.com/harikrishna553/docker_spring_boot.git’.
$mkdir helloworld
$
$cd helloworld/
$
$git clone https://github.com/harikrishna553/docker_spring_boot.git
Cloning into 'docker_spring_boot'...
remote: Enumerating objects: 14, done.
remote: Counting objects: 100% (14/14), done.
remote: Compressing objects: 100% (8/8), done.
remote: Total 14 (delta 0), reused 14 (delta 0), pack-reused 0
Unpacking objects: 100% (14/14), done.
$
$
$ls
docker_spring_boot

Step 3: Create Dockerfile in helloworld folder.
$touch Dockerfile
$
$ls
Dockerfile  docker_spring_boot

Dockerfile
FROM maven:3.5.2-jdk-8-alpine AS maven_build
  
COPY ./docker_spring_boot /data/docker_spring_boot

WORKDIR /data/docker_spring_boot

RUN ["mvn", "clean", "install"]

CMD ["java", "-jar", "target/dockerHello-1.jar"]

Step 4: Build the image.

Navigate to the folder where Dockerfile is located and execute the below command.
docker build -t spring_hello .

$docker images
REPOSITORY                         TAG                 IMAGE ID            CREATED             SIZE
spring_hello                       latest              baef228e1fa2        3 minutes ago       571MB
sleep_me                           latest              49913f3eae6d        2 hours ago         88.9MB

Step 5: Run the container by executing below command.

docker run -p 9090:1234 spring_hello

$docker run -p 9090:1234 spring_hello

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.6.RELEASE)

2019-08-31 15:58:54.192  INFO 1 --- [           main] com.sample.app.App                       : Starting App v1 on 24d1cf90bf04 with PID 1 (/data/docker_spring_boot/target/dockerHello-1.jar started by root in /data/docker_spring_boot)
2019-08-31 15:58:54.198  INFO 1 --- [           main] com.sample.app.App                       : No active profile set, falling back to default profiles: default
2019-08-31 15:58:55.786  INFO 1 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 1234 (http)
2019-08-31 15:58:55.827  INFO 1 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2019-08-31 15:58:55.827  INFO 1 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.21]
2019-08-31 15:58:55.995  INFO 1 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2019-08-31 15:58:55.995  INFO 1 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1718 ms
2019-08-31 15:58:56.419  INFO 1 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2019-08-31 15:58:56.860  INFO 1 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 1234 (http) with context path ''
2019-08-31 15:58:56.866  INFO 1 --- [           main] com.sample.app.App                       : Started App in 3.231 seconds (JVM running for 3.909)
2019-08-31 15:58:59.712  INFO 1 --- [nio-1234-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2019-08-31 15:58:59.712  INFO 1 --- [nio-1234-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2019-08-31 15:58:59.723  INFO 1 --- [nio-1234-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 11 ms

Open the url ‘http://localhost:9090/’ in browser, you can see below kind of screen.


Previous                                                    Next                                                    Home

Docker Registry

Docker registry is the place where all docker images are published.

As you see the above image, all the clients can publish their images to the docker registry and at the same time, clients can pull images from the docker registry.

When you try to run a container, docker engine checks whether the image is available locally or not, if the image is not available locally, then docker engine pull the image from docker hub (Default one is docker.io).
$docker run alpine
Unable to find image 'alpine:latest' locally
latest: Pulling from library/alpine
9d48c3bd43c5: Pull complete 
Digest: sha256:72c42ed48c3a2db31b7dafe17d275b634664a708d901ec9fd57b1529280f01fb
Status: Downloaded newer image for alpine:latest
docker.io

Each organization can configure their own internal private docker registry.


Previous                                                    Next                                                    Home