一、Docker安装
(1)备份更新源文件
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
#替换阿里云镜像源
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
#生成缓存
yum makecache
#运行更新系统
yum update
(2)yum安装docker
#删除旧版本docker
sudo yum remove -y docker \
docker-common \
container-selinux \
docker-selinux \
docker-engine
sudo yum install -y yum-utils
sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
sudo yum-config-manager --enable docker-ce-edge
sudo yum makecache fast
sudo yum -y install docker-ce
sudo systemctl enable docker
sudo systemctl start docker
运行下面命令,测试docker是否正常
sudo docker -v
(3)配置docker 阿里加速器(百度如何申请),修改docker存储位置为/home/dockerfiles(默认为/var/lib/docker),修改docker0默认网段
执行 vim /etc/docker/daemon.json
{
"graph":"/home/dockerfiles",
"registry-mirrors":["https://xxx.mirror.aliyuncs.com"],
"bip":"10.10.10.1/24"
}
sudo systemctl daemon-reload
sudo systemctl restart docker
sudo curl -L https://github.com/docker/compose/releases/download/1.16.1/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
#执行下面语句查看版本
docker-compose --version
二、Docker常用资源
(1)镜像仓库
docker官方仓库 https://store.docker.com/
阿里云仓库 https://dev.aliyun.com/search.html
(2)github
https://github.com/
三、Docker常用命令
功能 |
命令 |
列出运行中的容器 |
docker ps |
列出所有容器(包括停止的) |
docker ps -a |
拉回镜像 |
docker pull mysql:5.7 |
启动容器 |
docker run -d -p 3306:3306 -e 'password=12345' -v /hostvolume:/insidevolume --name='test' registry/imagename:tag |
进入容器 |
docker exec -it xxx /bin/bash |
显示日志 |
docker logs -f xxx |
删除某个容器 |
docker rm -f xxx |
删除所有容器 |
docker rm -f `docker ps -a -q` |
列出容器网关 |
docker network list |
查看容器网关 |
docker network inspect xxx |
更多命令说明查看https://docs.docker.com/engine/reference/run/
四、docker镜像制作
Dockerfile制作镜像
打包为war的项目的dockerfile文件样例
FROM tomcat:7
RUN rm -rf /usr/local/tomcat/webapps/ROOT/*
COPY target/myweb.war /tmp/
RUN unzip /tmp/myweb.war -d /usr/local/tomcat/webapps/ROOT \
&& ls /usr/local/tomcat/webapps/ROOT
CMD ["catalina.sh", "run"]
spring boot项目的dockerfile文件样例
FROM frolvlad/alpine-oraclejdk8:slim
ADD target/spring-boot-test.jar /app.jar
VOLUME /var/log/testlog
ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar /app.jar" , "--spring.profiles.active=docker"]
打包命令
#!/bin/bash
mvn clean package -Dmaven.test.skip=true
VERTAG="1.0.0.001"
if [ -n "$1" ]
then
verTag=$1
else
verTag=$VERTAG
fi
echo "docker build and push ${verTag}"
docker build ./ -t {镜像仓库地址}/helloworld:$verTag
五、docker-compse
官方指南
https://docs.docker.com/compose/overview/
常用命令
功能 |
命令 |
列出运行中的容器 |
docker-compose ps |
检查docker-compose文件 |
docker-compose config |
运行容器 |
docker-compose up -d |
显示日志 |
docker-compose logs -f |
停止容器 |
docker-compose stop |
删除容器 |
docker-compose down |
例子
version: '3'
services:
db:
image: mysql:5.7
volumes:
- ./mysql-data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: somewordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
networks:
- wordpress-net
wordpress:
depends_on:
- db
image: wordpress:latest
ports:
- "8000:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
networks:
- wordpress-net
networks:
wordpress-net:
driver: bridge
ipam:
driver: default
config:
- subnet: 10.200.1.0/24
网友评论