docker-compose 是一个用来把 docker 自动化的东西。
有了 docker-compose 你能够把全部繁复的 docker 操做全都一条命令,自动化的完成。php
用通俗的语言来讲,咱们平时操做 docker 仍是很原始的一系列动做,你手动使用 docker 的动做能够拆分红css
- 找到一个系统镜像 // docker search
- 安装好 vm 或者 virtual box // apt-get install docker
- 在 vm 中安装镜像 // docker run -d -it 你的镜像
- 略..
这是最小的动做, 若是你要映射硬盘,设置nat网络或者桥接网络,等等…你就要作更多的 docker 操做, 这显然是很是没有效率的。java
可是咱们写在 docker-compose.file 里面就很好了。 你只须要写好后 只运行一句docker-compose up
mysql
docker-compose ps
docker-compose logs
docker-compose port eureka 8761
docker-compose build
docker-compose start eureka
docker-compose stop eureka
docker-compose rm eureka
docker-compose up
docker-compose kill eureka
docker-compose scale user=3 movie=3
docker-compose run web bash
build: ./dir --------------- build: context: ./dir dockerfile: Dockerfile args: buildno: 1
command: bundle exec thin -p 3000 ---------------------------------- command: [bundle,exec,thin,-p,3000]
dns: 8.8.8.8 ------------ dns: - 8.8.8.8 - 9.9.9.9
dns_search: example.com ------------------------ dns_search: - dc1.example.com - dc2.example.com
environment:
RACK_ENV: development
SHOW: 'ture' ------------------------- environment: - RACK_ENV=development - SHOW=ture
env_file: .env --------------- env_file: - ./common.env
expose:
- "3000" - "8000"
image: java
network_mode: "bridge" network_mode: "host" network_mode: "none" network_mode: "service:[service name]" network_mode: "container:[container name/id]"
ports: # 暴露端口信息 - "宿主机端口:容器暴露端口" - "8763:8763" - "8763:8763"
links: # 指定服务名称:别名 - docker-compose-eureka-server:compose-eureka
volumes:
- /lib
- /var
--no-color 单色输出,不显示其余颜. -f, --follow 跟踪日志输出,就是能够实时查看日志 -t, --timestamps 显示时间戳 --tail 从日志的结尾显示,--tail=200
version: '2' services: web: build: . links: - "db:database" db: image: postgres
使用dockers-composeweb
新建 项目 userapisql
添加引用:docker
public class Users { [Key] public int Id { get; set; } public string Name { get; set; } public int Age { get; set; } public string Job { get; set; } public string Company { get; set; } public DateTime CreateTime { get; set; } }
添加AppDbContext 数据库
public class AppDbContext : DbContext { public AppDbContext(DbContextOptions<AppDbContext> options) : base(options) { } protected override void OnModelCreating(ModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); } public DbSet<Users> Users { get; set; } }
配置文件添加数据库链接json
"ConnectionStrings": { "MysqlConnection": "server=db;port=3306;database=mysql;userId=root;password=1234." }
Startupapi
ConfigureServices添加
services.AddDbContext<AppDbContext>(options => { options.UseMySQL(Configuration.GetConnectionString("MysqlConnection")); });
configrue 添加 InitDataBase(app);
private void InitDataBase(IApplicationBuilder app) { using (var scope = app.ApplicationServices.CreateScope()) { var userContext = scope.ServiceProvider.GetRequiredService<AppDbContext>(); userContext.Database.Migrate(); if (userContext.Users != null) { userContext.Users.Add(new Models.Users() { Age = 66, Company = "myCompany", CreateTime = DateTime.Now, Job = "chengxuyuan", Name = "xiaohong" }); userContext.SaveChanges(); } } }
新建 ADD-Migrations init
这里须要注意mysql有的时候不会生成__EFMigrationsHistory表
能够在AppDbContextModelSnapshot 文件里添加
modelBuilder.Entity("__EFMigrationsHistory", b => { b.Property<string>("MigrationId"); b.Property<string>("ProductVersion"); });
也能够本身在mysql中新建
CREATE TABLE `__EFMigrationsHistory` ( `MigrationId` nvarchar(150) NOT NULL, `ProductVersion` nvarchar(32) NOT NULL, PRIMARY KEY (`MigrationId`) );
新建文件Dockerfile.json
FROM mcr.microsoft.com/dotnet/core/aspnet:2.2-stretch-slim AS base WORKDIR /app EXPOSE 80 EXPOSE 443 FROM mcr.microsoft.com/dotnet/core/sdk:2.2-stretch AS build WORKDIR /src COPY ["User.Api.csproj", ""] RUN dotnet restore "./User.Api.csproj" COPY . . WORKDIR "/src/." RUN dotnet build "User.Api.csproj" -c Release -o /app FROM build AS publish RUN dotnet publish "User.Api.csproj" -c Release -o /app FROM base AS final WORKDIR /app COPY --from=publish /app . ENTRYPOINT ["dotnet", "User.Api.dll"]
新建文件docker-compose.yml
version: '3.3' services: db: image: mysql/mysql-server container_name: mysqldb command: mysqld --character-set-server=utf8 --collation-server=utf8_general_ci restart: always ports: - "3306:3306" environment: MYSQL_ROOT_PASSWORD: 1234. MYSQL_USER: root MYSQL_PASSWORD: 1234. volumes: - /d/docker/beta/mysql-init:/docker-entrypoint-initdb.d web: build: . container_name: aspnetcore ports: - "8004:80" depends_on: - db
init.sql 添加root 受权,并刷新权限:
ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY '1234.'; GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION; flush privileges;
cmd 到项目目录
执行 : docker-compose up
指定dockers ps
咱们看到项目已经发布成功了
浏览器看看