Docker starts common container commands and configuration instructions

Docker container related commands

Containers are created based on Docker images.

docker run [Options] imagerun container

docker run [Options] image

#参数说明
--name="名字"           指定容器名字
-d                     后台方式运行
-it                    使用交互方式运行,进入容器查看内容
-p                     指定容器的端口
	-p ip:主机端口:容器端口  配置主机端口映射到容器端口
	-p 主机端口:容器端口(常用)
	-p 容器端口
-P                     随机指定端口
-e					   环境设置
-v					   容器数据卷挂载

Run and enter the container centos

[root@localhost ~]# docker run -it centos /bin/bash
[root@ce2bbae9f151 /]# ls
bin  etc   lib	  lost+found  mnt  proc  run   srv  tmp  var
dev  home  lib64  media       opt  root  sbin  sys  usr

Enter the container , because usually our containers are run in the background, sometimes we need to enter the container to modify the configuration

  • docker exec -it 容器id /bin/bash
# docker exec 进入容器后开启一个新的终端,可以在里面操作
docker exec -it 容器id /bin/bash
  • docker attach 容器id
# docker attach 进入容器正在执行的终端
docker attach 容器id

exit container

exit 	# 停止容器并退出(后台方式运行则仅退出)
Ctrl+P+Q  # 不停止容器退出

docker psView running containers

# 查看当前正在运行的容器
docker ps 
     
-a   # 查看所有容器的运行记录
-n=? # 显示最近创建的n个容器
-q   # 只显示容器的id

docker start 容器idStart the container

docker start 容器id          # 启动容器
docker restart 容器id        # 重启容器
docker stop 容器id           # 停止当前运行的容器
docker kill 容器id           # 强制停止当前容器

docker logs 容器idView container running logs

docker logs -tf 容器id
docker logs --tail num 容器id  # num为要显示的日志条数

docker top 容器idView process information in the container

docker top 容器id

docker inspect 容器idView container metadata

docker inspect 容器id

Docker start mysql

# 1.启动mysql  设置密码、设置挂载数据卷
docker run -d -p 3306:3306 --name mymysql -v /home/mysql/conf:/etc/mysql/conf.d -v /home/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 mysql 
# 2.远程连接mysql服务,若无权限,进入mysql容器中修改远程连接权限
docker exec -it 36d4806c765a /bin/bash
# 登录mysql
mysql -u root -p
# 修改root 可以通过任何客户端连接
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '123456';

After successfully modifying the remote connection permission, you can connect to the database through a mysql client such as Navicat.

Docker start nginx

Starting nginx mount is different from other container mounts, and additional operations are required.

The docker mount is mounted at run time, so the configuration file must be copied first . If you do not copy and directly mount the container to start, it will find the configuration file from the mounted directory. If it cannot find it, it will fail to start.

1. 运行容器

docker run --name nginx -d -p 7777:80 nginx

2. 在宿主机上创建挂载目录

mkdir -p /home/docker/volumes/nginx/conf
mkdir -p /home/docker/volumes/nginx/log

3. 从容器中复制配置文件到挂载目录

docker cp nginx:/etc/nginx/nginx.conf /home/docker/volumes/nginx/conf/nginx.conf     #从容器中复制配置文件
docker cp nginx:/etc/nginx/conf.d /home/docker/volumes/nginx/conf/                   #从容器中复制配置文件夹
docker cp nginx:/usr/share/nginx/html /home/docker/volumes/nginx/               #从容器中复制html文件
docker cp nginx:/var/log/nginx /home/docker/volumes/nginx/log                   #从容器中复制日志文件

4. 删除nginx容器

docker stop nginx                               
docker rm nginx

5. 使用挂载的方式重新启动nginx

chmod 777 /home/docker/volumes/nginx/conf/nginx.conf
docker run -d -p 7777:80 --name nginx \
 -v /home/docker/volumes/nginx/conf/nginx.conf:/etc/nginx/nginx.conf \
 -v /home/docker/volumes/nginx/conf/conf.d:/etc/nginx/conf.d \
 -v /home/docker/volumes/nginx/html:/usr/share/nginx/html \
 -v /home/docker/volumes/nginx/log:/var/log/nginx \
 -e LANG=C.UTF-8 -e LC_ALL=C.UTF-8 \
 --privileged=true --restart=always nginx

Configuration instructions:

  • -e LANG=C.UTF-8 -e LC_ALL=C.UTF-8 is to solve nginx Chinese garbled characters
  • --privileged=true gives root privileges in the container
  • --restart=always starts automatically with docker

Docker start redis

docker run -p 6379:6379 -d --name myredis \
		   -v /home/docker_volume/redis/data:/data \
           -v /home/docker_volume/redis/conf/redis.conf:/etc/redis/redis.conf \
		   redis redis-server /etc/redis/redis.conf --appendonly yes
  • redis-server /etc/redis/redis.conf starts redis with the configuration file
  • appendonly yes enables redis persistence

Enter the redis container

docker exec -it myredis redis-cli

Docker start rabbitmq

docker run -it -d -p 5672:5672 -p15672:15672 --hostname my-rabbit --name my-rabbit \
		   -v /home/docker_volume/rabbit/:/var/lib/rabbitmq \
		   -e RABBITMQ_DEFAULT_USER=admin \
		   -e RABBITMQ_DEFAULT_PASS=admin rabbitmq:3-management

After the startup is successful, the browser visits http://ip:15672/ to enter the rabbitmq management interface.

Docker starts Kafka

Kafka needs to depend on Zookeeper

  1. start zookeeper
docker run -it -d --restart=always --name zookeeper -p 2181:2181 wurstmeister/zookeeper
  1. Start Kafka
docker run -d --restart=always --name kafka -p 9092:9092 \
 -e KAFKA_BROKER_ID=0 \
 -e KAFKA_ZOOKEEPER_CONNECT=10.1.7.102:2181/kafka \
 -e KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://10.1.7.102:9092 \
 -e KAFKA_LISTENERS=PLAINTEXT://0.0.0.0:9092 \
 -v /etc/localtime:/etc/localtime wurstmeister/kafka

Use Kafka Tool to connect:
insert image description here

Docker starts tomcat

docker run -it -d --name mytomcat -p 8090:8080 \
		   -v /home/docker_volume/tomcat/webapps/:/usr/local/tomcat/webapps tomcat

The webapps directory in the default image is empty, first put the project into the webapps directory, and then visit http://ip:8090/

# 进入tomcat容器
docker exec -it mytomcat /bin/bash
# 复制项目到webapps下
cp -rf webapps.dist/* webapps

Docker start jar package

If the jar package wants to run in docker, it needs to make the jar package into a mirror file, and then docker runs the mirror image. For a detailed tutorial on Dockerfile, please refer to the article: Docker Introduction and Advanced Part 3

1. Upload the jar package to Linux and write the Dockerfile

[root@localhost idea]# ls
demo-0.0.1-SNAPSHOT.jar  Dockerfile

2.Dockerfile file content

FROM java:8

# 将demo-0.0.1-SNAPSHOT.jar 复制 到容器中并重命名为 app.jar
COPY demo-0.0.1-SNAPSHOT.jar app.jar

CMD ["--server.port=8080"]

EXPOSE 8080

ENTRYPOINT ["java","-jar","app.jar"]

3.docker build build image

[root@localhost idea]# docker build -t springbootdemo:1.0 .
[root@localhost idea]# docker images
REPOSITORY       TAG                IMAGE ID       CREATED          SIZE
springbootdemo   1.0                d9648a49a226   50 seconds ago   661MB	

4. Run and test

[root@localhost idea]# docker run -d -p:8888:8080 --name mydemo springbootdemo:1.0
[root@localhost idea]# curl localhost:8888/hello
hello buckletime![root@localhost idea]# 

Docker starts PostgreSQL

# 拉取镜像
docker pull kartoza/postgis:11.0-2.5
# 运行容器
docker run -d -t --name postgresql -p 5432:5432 \
-e POSTGRES_USER=postgres \
-e POSTGRES_PASS=postgres \
-e ALLOW_IP_RANGE=0.0.0.0/0 \
-v /home/volumes/postgres/data:/var/lib/postgresql \
-v /home/volumes/postgres/temp:/tmp/tmp \
--restart always kartoza/postgis:11.0-2.5
# 安装postgis扩展
# 注意,扩展要加在数据库上,而非模式上
CREATE EXTENSION postgis;
CREATE EXTENSION postgis_topology;
CREATE EXTENSION fuzzystrmatch;
CREATE EXTENSION postgis_tiger_geocoder;
# 查看扩展是否安装成功
SELECT * FROM pg_extension;

Docker starts ElasticSearch

# 拉取镜像文件  
docker pull elasticsearch

# 创建挂载目录
mkdir -p /home/volumes/elasticsearch/config
mkdir -p /home/volumes/elasticsearch/data
mkdir -p /home/volumes/elasticsearch/plugins

# 创建并写入elasticsearch.yml配置,注意:http.host: 0.0.0.0 
echo "http.host: 0.0.0.0">>/home/volumes/elasticsearch/config/elasticsearch.yml

# 文件夹赋权
chmod -R 777 /home/volumes/elasticsearch/

# docker启动elasticsearch
docker run --name elasticsearch -p 9200:9200 -p 9300:9300 \
 -e "discovery.type=single-node" \
 -e ES_JAVA_OPTS="-Xms64m -Xmx128m" \
 -v /home/volumes/elasticsearch/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml \
 -v /home/volumes/elasticsearch/data:/usr/share/elasticsearch/data \
 -v /home/volumes/elasticsearch/plugins:/usr/share/elasticsearch/plugins \
 -d elasticsearch

Parameter Description:

-e "discovery.type=single-node":单例模式
-e ES_JAVA_OPTS="-Xms64m -Xmx128m":配置内存大小

Check whether elasticsearch is successfully installed, and visit with a browser: http://localhost:9200

insert image description here

Guess you like

Origin blog.csdn.net/weixin_45698637/article/details/124213429