Docker Chapter 2: Docker application deployment (mysql, tomcat, nginx, redis)

Docker application deployment

First, deploy MySQL

  1. Search mysql mirror
docker search mysql
  1. Pull mysql image
docker pull mysql:5.6
  1. Create containers, set port mapping, directory mapping
# 在/root目录下创建mysql目录用于存储mysql数据信息
mkdir ~/mysql
cd ~/mysql
docker run -id \
-p 3307:3306 \
--name=c_mysql \
-v $PWD/conf:/etc/mysql/conf.d \
-v $PWD/logs:/logs \
-v $PWD/data:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=123456 \
mysql:5.6
  • Parameter Description:
    • -p 3307: 3306 : Map the 3306 port of the container to the 3307 port of the host machine.
    • -v $ PWD / conf: /etc/mysql/conf.d : Mount conf / my.cnf in the current directory of the host to /etc/mysql/my.cnf of the container. Configuration directory
    • -v $ PWD / logs: / logs : Mount the logs directory under the current directory of the host to / logs of the container. Log directory
    • -v $ PWD / data: / var / lib / mysql : Mount the data directory under the current directory of the host to / var / lib / mysql of the container. Data directory
    • **-e MYSQL_ROOT_PASSWORD = 123456: ** Initialize the password of the root user.
  1. Enter the container and operate mysql
docker exec –it c_mysql /bin/bash
  1. Use external machine to connect mysql in the container
    Insert picture description here

Second, deploy Tomcat

  1. Search tomcat image
docker search tomcat
  1. Pull tomcat image
docker pull tomcat
  1. Create containers, set port mapping, directory mapping
# 在/root目录下创建tomcat目录用于存储tomcat数据信息
mkdir ~/tomcat
cd ~/tomcat
docker run -id --name=c_tomcat \
-p 8080:8080 \
-v $PWD:/usr/local/tomcat/webapps \
tomcat 
  • Parameter Description:

    • **-p 8080: 8080: ** Map the 8080 port of the container to the 8080 port of the host

      **-v $ PWD: / usr / local / tomcat / webapps: ** Mount the current directory in the host to the webapps of the container

  1. Use external machine to access tomcat

Insert picture description here

3. Deploy Nginx

  1. Search nginx mirror
docker search nginx
  1. Pull nginx image
docker pull nginx
  1. Create containers, set port mapping, directory mapping
# 在/root目录下创建nginx目录用于存储nginx数据信息
mkdir ~/nginx
cd ~/nginx
mkdir conf
cd conf
# 在~/nginx/conf/下创建nginx.conf文件,粘贴下面内容
vim nginx.conf
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;
}


docker run -id --name=c_nginx \
-p 80:80 \
-v $PWD/conf/nginx.conf:/etc/nginx/nginx.conf \
-v $PWD/logs:/var/log/nginx \
-v $PWD/html:/usr/share/nginx/html \
nginx
  • Parameter Description:
    • -p 80:80 : Map port 80 of the container to port 80 of the host machine.
    • -v $ PWD / conf / nginx.conf: /etc/nginx/nginx.conf : Mount /conf/nginx.conf in the current directory of the host to the container: /etc/nginx/nginx.conf. Configuration directory
    • -v $ PWD / logs: / var / log / nginx : Mount the logs directory under the current directory of the host to / var / log / nginx of the container. Log directory
  1. Use external machine to access nginx

Insert picture description here

Fourth, deploy Redis

  1. Search redis mirror
docker search redis
  1. Pull redis image
docker pull redis:5.0
  1. Create a container and set up port mapping
docker run -id --name=c_redis -p 6379:6379 redis:5.0
  1. Use an external machine to connect to redis
./redis-cli.exe -h 192.168.149.135 -p 6379
Published 10 original articles · Liked5 · Visits 368

Guess you like

Origin blog.csdn.net/wanglei19891210/article/details/105498142
Recommended