Docker common instructions usage and container installation

Write a blog to summarize the basic instructions of docker and container installation, and make a memo and summary.

Docker installation and use

1.Install docker

Installation documentation: https://docs.docker.com/install/linux/docker-ce/centos/

1. Uninstall docker before system

sudo yum remove docker \
                  docker-client \
                  docker-client-latest \
                  docker-common \
                  docker-latest \
                  docker-latest-logrotate \
                  docker-logrotate \
                  docker-engine

2. Install dependencies

sudo yum install -y yum-utils

3.docker warehouse address

sudo yum-config-manager \
    --add-repo \    https://download.docker.com/linux/centos/docker-ce.repo

4.Install docker

sudo yum install docker-ce docker-ce-cli containerd.io

5. Start docker

sudo systemctl start docker

6. View docker (root permissions required)

docker ps // 启动的容器
docker ps -all // 所有容器

7. Set docker to start automatically when booting

sudo systemctl enable docker

2.docker install mysql

1. Download the image file

docker pull mysql:5.7

2. Create an instance and start it

docker run -p 3306:3306 --name mysql --restart=always \
-v /mydata/mysql/log:/var/log/mysql \
-v /mydata/mysql/data:/var/lib/mysql \
-v /mydata/mysql/conf:/etc/mysql \
-e MYSQL_ROOT_PASSWORD=root \
-d mysql:5.7

Explanation: -p 3306:3306 maps the container port 3306 to the host port 3306

–restart starts the container when starting docker

-v /mydata/mysql/conf:/etc/mysql: Mount the configuration folder to the host

-v /mydata/mysql/log:/var/log/mysql: Mount the log folder to the host

-v /mydata/mysql/data:/var/lib/mysql: Mount the configuration folder to the host

-e MYSQL_ROOT_PASSWORD=root initialization password

3. Modify MYSQL configuration

vi /mydata/mysql/conf/my.cnf

[client]

default-character-set=utf8

[mysql]

default-character-set=utf8

[mysqld]

init_connect=‘SET collation_connection = utf8_unicode_ci’

init_connect=‘SET NAMES utf8’

character-set-server=utf8

collation-server=utf8_unicode_ci

skip-character-set-client-handshake

skip-name-resolve

4. Restart mysql

docker restart mysql

3.docker install redis

1. Download the image (if there is no version number, download the latest version)

docker pull redis 

2. Create redis configuration file

mkdir -p /mydata/redis/conf

touch /mydata/redis/conf/redis.conf

3.Install redis

docker run -p 6379:6379 --name redis --restart=always \
-v /mydata/redis/data:/data \
-v /mydata/redis/conf/redis.conf:/etc/redis/redis.conf \
-d redis redis-server /etc/redis/redis.conf

-d starts redis and sets the container to execute in the background, and reads the configuration file to start according to redis.conf

–restart starts the container when starting docker

4.redis persistence configuration

vi /mydata/redis/conf/redis.conf

Enter appendonly yes

5. Restart redis

docker restart redis

4.docker install nginx

1. Download the image

docker pull nginx

2. Create nginx configuration file

mkdir -p /mydata/nginx/conf

touch /mydata/nginx/conf/nginx.conf

3. Add configuration

events {

worker_connections 1024; ## Default: 1024

}

  • If you don’t add this configuration, nginx will report an error. I don’t know the reason.

    nginx: [emerg] no “events” section in configuration

4.Install nginx

docker run -p 8080:80 --name nginx \
--restart=always
-v /mydata/nginx/conf/nginx.conf:/etc/nginx/nginx.conf \
-v /mydata/nginx/conf/conf.d:/etc/nginx/conf.d \
-v /mydata/nginx/html:/usr/share/nginx/html \
-v /mydata/nginx/logs:/var/log/nginx \
-d nginx

5. If you do not set the configuration file to mount and start, you can start it directly.

docker run --name nginx-test -p 8080:80 -d nginx

5.docker install centos

1. Download the image

docker pull centos

2. Create centos container

docker run -it -d -p 8888:8888 --name centos --restart=always --privileged=true \
-v /mydata/centos/www:/www centos \

You can add other port mappings, such as port 80 to access the container's port 80, and you can freely choose to configure it.

3. Enter centos

docker exec -it centos /bin/bash

4. System initialization: Since docker is a pure version, we first need to upgrade it and install the necessary software.

yum check-update -y && yum update -y && yum install initscripts screen wget -y

6.Docker common instructions

sudo docker images // View all images

docker exec -it mysql /bin/bash // Enter the container

docker rm container-name// delete container

docker top container-name//Container occupancy rate is high

docker logs -f container-name// Container execution log

docker pull mysql|redis:xxx // Pull the docker source xxx is the version, do not add the latest latest by default

Guess you like

Origin blog.csdn.net/qq_40922616/article/details/120417250