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
咱們看到項目已經發布成功了
瀏覽器看看