Docker安装配置
Docker安装及管理
yum install docker-io -y
vim /etc/sysconfig/docker#修改静像获取源地址
other_args="--registry-mirror=https://wgcscytr.mirror.aliyuncs.com"
docker search centos
docker pull centos
docker run -it --name myname centos#创建新的容器
docker ps#查询容器,-l 查询历史运行容器
cocker rmi centos#删除静像
docker rm -f centos #强制删除容器
docker start 容器ID#启动容器
docker restart 容器ID#重启容器
docker stop 容器ID#停止容器
docker kill 容器ID#强制停止容器
docker run -d centos#以后台模式运行容器
docker ps -a#查看所有容器
docker logs -t -f --tail 3 48d461c39327#查看指定容器的日志
ctl+p+q#退出容器但不关闭
docker attach 48d461c39327 #进入指定容器
docker exec -t 48d461c39327 ls -l /tmp#在宿主执行命令
MySQL容器安装及管理
docker pull mysql:5.6#从仓库获取静像
docker run -p 12345:3306 --name mysqltest -e MYSQL_ROOT_PASSWORD=123456 -d mysql:5.6#创建mysql容器
docker exec -it mysqltest /bin/bash#进入容器
mysql -uroot -p#连接mysql
Tomcat容器安装及管理
docker pull tomcat
docker run --name tomcat -p 8080:8080 -v /usr/local/tomcat:/usr/local/tomcat/webapps/test -d tomcat
docker exec -it tomcat /bin/bash
cd /usr/local/tomcat/webapps/ROOT
mv index.jsp index.jsp.bak#备份原tomcat主页
cd /usr/local/tomcat/webapps
cp test/index.html ROOT/index.html#使用新建主页
制作docker静像
mkdir /root/hello-docker
cd hello-docker
-----------------------------------
vi app.py
from flask import Flask from redis import Redis, RedisError import os import socket #Connect to Redis redis = Redis(host="redis",db=0,socket_connect_timeout=2,socket_timeout=2) app = Flask(__name__) @app.route("/") def hello(): try: visits = redis.incr("counter") except RedisError: visits = "<i>can not connect to Redis, counter disabled!</i>" html = "<h3>Hello {name}!</h3>" \ "<b>Hostname:</b> {hostname}<br/>" \ "<b>Visits:</b> {visits}" return html.format(name=os.getenv("NAME","world"),hostname=socket.gethostname(),visits=visits) if __name__ == "__main__": app.run(host="0.0.0.0",port=80)
-----------------------------------------------------
vi requirements.txt
Flask
Redis
-------------------------------------------------------
vi Dockerfile
# Use an official Python runtime as a parent image
FROM python:2.7-slim
# Set the working directory to /app
WORKDIR /app
# Copy the current directory contents into the container at /app
COPY . /app
# Install any needed packages specified in requirements.txt
RUN pip install --trusted-host pypi.python.org -r requirements.txt
# Make port 80 available to the world outside this container
EXPOSE 80
# Define environment variable
ENV NAME World
# Run app.py when the container launches
CMD ["python", "app.py"]
---------------------------------------------------------
构建静像
docker build --tag=friendlyhello .
查看构建静像
docker image ls
启动容器
docker run -p 80:80 friendlyhello
访问容器
curl 192.168.2.120
编写容器编排yml
------------------------------------------------------
vi docker-compose.yml
version: "3"
services:
web:
#replace username/repo:tag with your name and image details
image: friendlyhello:latest #自己制作的静像
deploy:
replicas: 5
resources:
limits:
cpus: "0.1"
memory: 50M
restart_policy:
condition: on-failure
ports:
- "80:80"
networks:
- webnet
networks:
webnet:
----------------------------------------------
初始化容器集群部署环境,如有多块网卡需要指定地址
docker swarm init --advertise-addr 192.168.2.120
使用compse文件批量部署容器
docker stack deploy -c docker-compose.yml getstartedlab
查看堆栈状态
docker stack services getstartedlab
查看堆栈服务
docker service ps getstartedlab_web
查看容器部署状态
docker container ls
查看服务
docker service ls
查看容器名称
docker container ls -q
增加容器副本
------------------------------------------------------
vi docker-compose.yml
version: "3"
services:
web:
#replace username/repo:tag with your name and image details
image: friendlyhello:latest #自己制作的静像
deploy:
replicas: 10 #将副本数据增加到10个
resources:
limits:
cpus: "0.1"
memory: 50M
restart_policy:
condition: on-failure
ports:
- "80:80"
networks:
- webnet
networks:
webnet:
----------------------------------------------
再次部署容器
docker stack deploy -c docker-compose.yml getstartedlab
查看容器状态
docker container ls
移除堆栈
docker stack rm getstartedlab
移除集群
docker swarm leave --force