docker learning record

1.docker command

启动docker
systemctl start docker
检查docker状态
systemctl status docker
停止docker
systemctl stop docker
重启docker
systemctl restart docker
开机自动启动docker
systemctl enable docker
查看镜像
docker images
搜索镜像
docker search redis
拉取镜像(下载)
docker pull redis:5.0
删除镜像
docker rmi redis
查看容器
  运行容器  docker ps
  全部容器  docker ps -a
创建容器
docker run -it --name=c1 centos:7 /bin/bash
进入容器
docker exec -it c1 /bin/bash
启动容器
docker start c1
停止容器
docker stop c1
删除容器
docker rm c1
查看容器信息
docker inspect c1

Thinking: After the
docker container is deleted, will the data generated in the container also be destroyed?
Ca n't docker container and external machine directly exchange files? No
, what should I do if I want to exchange data between containers? Mounting a

Insert picture description here


data volume A
data volume is a directory or file on the host.
When the container directory and the data volume directory are bound, the modification of the other party will be synchronized immediately.
A data volume can be mounted by multiple containers at the same time.
One container can also be mounted. Data volume

配置数据卷(创建容器时)
docker run ... -v 宿主机目录(文件):容器内目录(文件)...
注意:目录必须是绝对路径,会自动创建文件夹
例如:
docker run -it --name=c1 -v /root/data:/root/data_container centos:7 /bin/bash

2. mysql deployment

Pull the image
docker pull mysql to
create a container, set port mapping, directory mapping

# 在/root目录下创建mysql目录用于存储mysql数据信息
mkdir ~/mysql
cd ~/mysql
docker run -id \
-p 3306: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.7
  • Parameter Description:
  • -p 3307:3306 : Map the 3306 port of the container to the 3306 port of the host.
  • -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 in the current directory of the host to the container's /logs. Log directory
  • -v $PWD/data:/var/lib/mysql : Mount the data directory in the current directory of the host to the container's /var/lib/mysql. Data directory
  • -e MYSQL_ROOT_PASSWORD=123456 : Initialize the password of the root user.

3. Deploy tomcat

  1. Search tomcat mirror
docker search tomcat
  1. Pull tomcat image
docker pull tomcat
  1. Create a container, set up 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 port 8080 of the container to port 8080 of the host

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

4. Deploy nginx

  1. Search nginx mirror
docker search nginx
  1. Pull nginx image
docker pull nginx
  1. Create a container, set up 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.
  • -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 in the current directory of the host to the container's /var/log/nginx. Log directory

5. Deploy redis

  1. Search for redis mirror
docker search redis
  1. Pull redis mirror
docker pull redis:5.0
  1. Create a container, set up port mapping
docker run -id --name=c_redis -p 6379:6379 redis:5.0

6.Dockerfile

Thinking:

  • What is the essence of Docker mirroring?
    Hierarchical file system
  • Why is a centos image in Docker only 200m, and how many Gs does an iso file of a centos operating system need?
    Reuse
  • Why is there only 500m of a tomcat image in Docker, but only 70m of a tomcat installation package?
    The base image
    Dockerfile is a text file used to make an image.