Docker application deployment

Article directory

  • 1. Deploy MySQL
  • 2. Deploy Tomcat
  • 3. Deploy Nginx
  • 4. Deploy Redis

Reminder: The following is the main text of this article, and the Docker series of learning will continue to be updated

1. Deploy MySQL

①Search and pull the mysql image

docker search mysql
docker pull mysql:5.7

②Create container directory mapping

# Create a mysql directory under the /root/docker-data directory
mkdir ~/docker-data/mysql
cd ~/docker-data/mysql

③Create and start the container, set port mapping: $PWD indicates the current host location, ie /root/docker-data/mysql

docker run -id \
-p 3307:3306 \
--name=c_mysql \
-v $PWD/conf/my.cnf:/etc/mysql/my.cnf \ #Custom configuration file path, don't write if you don't need it
-v $PWD/conf/conf.d:/etc/mysql/conf.d \
-v $PWD/logs:/var/log/mysql \
-v $PWD/data:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=123456 \ #Initialize root user password
mysql:5.7

Customize the content of my.cnf as follows:

[mysqld]
user=mysql
character-set-server=utf8
default_authentication_plugin=mysql_native_password
secure_file_priv=/var/lib/mysql
sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION
max_connections=1000

default-time-zone='+08:00'
innodb_buffer_pool_size = 128M
port = 3306
datadir=/var/lib/mysql
socket=/var/run/mysqld/mysqld.sock
pid-file=/var/run/mysqld/mysqld.pid

# Number of connection failures allowed.
max_connect_errors=10

#engine
default-storage-engine=INNODB

log-bin = mysql-bin
# #Set save time
expire_logs_days=7

# #Note that this item needs to be configured for 5.7 and later versions: server-id=123454 (custom, uniqueness guaranteed); server-id generally goes to the last three digits of ip
server-id=1
# #binlog format, there are 3 kinds of statement, row, mixed
binlog-format=ROW
# #Indicates that every time a write is performed, it will be synchronized with the hard disk, which will affect performance. When it is 0, it means that mysql will not perform disk flushing when the transaction is submitted, and it is determined by the system
sync-binlog=1

# ## Turn on slow sql
slow_query_log=on
slow_query_log_file=/var/lib/mysql/slow-query.log
long_query_time=1

[client]
default-character-set=utf8

[mysql]
default-character-set=utf8

④Enter the container and operate mysql

docker exec -it c_mysql /bin/bash
mysql -uroot -p123456

⑤Use an external machine to connect to mysql in the container

back to content…

2. Deploy Tomcat

①Search and pull tomcat image

docker search tomcat
docker pull tomcat:8

②Create container directory mapping

# Create a tomcat directory under the /root/docker-data directory
mkdir ~/docker-data/tomcat
cd ~/docker-data/tomcat

③Create and start the container, set port mapping

docker run -id --name=c_tomcat \
-p 8080:8080 \
-v $PWD:/usr/local/tomcat/webapps \
tomcat:8

④ Use an external machine to access tomcat

pwd # /root/docker-data/tomcat
mkdir test
cd test
touch a.html
vim a.html # <h1>Hello, I am dockerCloud tomcat<h1>

back to content…

3. Deploy Nginx

①Search and pull nginx image

docker search nginx
docker pull nginx

②Create container directory mapping

# Create nginx directory in /root/docker-data directory to store nginx data information
mkdir ~/docker-data/nginx
cd ~/docker-data/nginx
mkdir conf
cd conf
# Create nginx.conf file under conf, paste the following content
vim nginx.conf

Official nginx.conf configuration file:

user nginx;
worker_processes 1;

error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;


events {<!-- -->
    worker_connections 1024;
}


http {<!-- -->
    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    log_format main '$remote_addr - $remote_user [$time_local] "$request"'
                      '$status $body_bytes_sent "$http_referer"'
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log /var/log/nginx/access.log main;

    sendfile on;
    #tcp_nopush on;

    keepalive_timeout 65;

    #gzip on;

    include /etc/nginx/conf.d/*.conf;
}

③Create and start the container, set port mapping

docker run -id --name=c_nginx \
-p 80:80 \
-v $PWD/conf/nginx.conf:/etc/nginx/conf.d/default.conf \
-v $PWD/logs:/var/log/nginx \
-v $PWD/html:/usr/share/nginx/html\
nginx

④ Use an external machine to access nginx

a. At this time, the html directory of nginx is empty, and there is no proxy.

b. Call the index.html file in your own html directory

c. Use nginx as a reverse proxy

?If nginx acts as a proxy host application, please refer to: Linux installation nginx detailed tutorial nginx as a reverse proxy.

?If nginx wants to proxy applications in other containers, please refer to: Docker Compose builds multi-container applications and arranges Nginx + Springboot projects.

back to content…

4. Deploy Redis

①Search and pull the redis image

docker search redis
docker pull redis:4.0

②Create container directory mapping

mkdir ~/docker-data/redis
cd ~/docker-data/redis

③Create and start the container, set port mapping

You can start directly without mounting the data volume:

docker run -id --name=c_redis -p 6379:6379 redis:4.0

Mount the data volume:

docker run -id --name=c_redis\
-p 6379:6379 \
-v /$PWD/conf/redis.conf:/etc/redis.conf \
-v /$PWD/data:/data \
redis:4.0

Before starting, add /redis/conf/redis.conf custom configuration file to the host:

bind 0.0.0.0
port 6379
daemonize yes
logfile "6379.log"
dir /data
databases 16

# rdb related configuration
dbfilename dump-6379.rdb
# Compress data when rdb is saved
rdbcompression yes
# rdb file format verification
rdb checksum yes
stop-writes-on-bgsave-error yes
save 10 2

# aof related configuration
append only yes
appendfsync always
appendfilename appendonly-6379.aof

④Use an external machine to connect to redis

redis-cli.exe -h 1.15.76.95 -p 6379

back to content…

Summary:
Tip: Here is a summary of the article:
This article is a study of Docker, learned how to deploy applications in Docker, and deployed our commonly used development tools such as mysql, tomcat, nginx, redis, and configured data volumes for them. The following learning content will continue to be updated! ! !