Moved documents out of repo into wiki

master
Jason Zhu 2021-02-04 23:50:15 +11:00
parent 9f6e9adbc7
commit 8b907cb663
4 changed files with 1 additions and 186 deletions

View File

@ -2,9 +2,7 @@
This repository is a playground for practising docker based programming following [docker-development-youtube-series](https://github.com/marcel-dempers/docker-development-youtube-series) This repository is a playground for practising docker based programming following [docker-development-youtube-series](https://github.com/marcel-dempers/docker-development-youtube-series)
* Topic 1: [Working with Dockerfiles (.NET, Golang, Python, NodeJS)](docs/working_with_dockerfiles.md) Please refer to Wiki for detailed documentation.
* Topic 2: [Working with code (.NET, Golang, Python, NodeJS)](docs/working_with_code.md)
* Topic 3: [Multistage & Layers](docs/multistage_layers.md)
## Reference ## Reference

View File

@ -1,88 +0,0 @@
# Multistage & Layers
Previously, in [Topic 2, Working with Code](working_with_code.md), we have dockerfile to create isolated DEV environment, but we still have to manually `dotnet new/build/run`
Target: Use multistage dockerfile to automatically setup DEV & PROD environment, where we only need to modify source code.
## What is Docker Image Layering?
dockerfile lay layers on top of another. So often changed layers should be in bottom to save image building time
### General DEV + PROD multistage dockerfile structure
```dockerfile
######## Dev stage #########
# Start from a base image (Layer 1)
FROM base-SDK-image-here
# install dependencies (Layer 2)
RUN pip install \npm install \go get
# Copy source code into image (Layer 3)
COPY source
# Run assembly to build app based on source code (Layer 4)
RUN build
```
Above is the dockerfile for a development environment. To product ready, we can use multistage. By giving above codes an alias, we can build multi-stage images
continue dockerfile
```dockerfile
####### Runtime stage #########
# Using a runtime image instead of full SDK (Layer 5)
FROM another-image
# Copy compiled app into runtime image to save space (Layer 6)
COPY output-from-stage-1
# Start app or entrypoint
RUN application
```
Benefit of this multistage, we can use debug tools in dev stage, but also keep product small in stage 2
## Modify dockerfile and docker-compose for multistage building
### Add build target in docker-compose
Adding build target in `build`, so docker-compose can be directly used to create DEV/PROD image.
```docker-compose
build:
context: ./c#
target: dev
```
### Modify dockerfile
```dockerfile
############# DEV IMAGE #################
# Define a dev stage using `as`
FROM mcr.microsoft.com/dotnet/core/sdk:2.2-stretch as dev
# Defining a working directory
RUN mkdir /app/
WORKDIR /app/
# ????
COPY ./src/work.csproj /app/work.csproj
RUN dotnet restore
# Copy the remaining source file
COPY ./src/ /app/
RUN mkdir /out/
RUN dotnet publish --no-restore --output /out/ --configuration Release
############## PRODUCT IMAGE #############
FROM mcr.microsoft.com/dotnet/core/sdk:2.2-stretch-slim as prod
RUN mkdir /app/
WORKDIR /app/
# Copy binary into folder
COPY --from=dev /out/ /app/
RUN chmod +x /app/
CMD dotnet work.dll
```

View File

@ -1,50 +0,0 @@
# Working with Codes
This tutorial talks about how to work inside container
* Update Docker Compose for dev need
* Entering dev container using docker compose
* Create source codes in dev container
* Installing dependencies
* C# `nuget`
* Golang `go get`
* Nodejs `npm install`
* Python `pip install`
* Compile and Run code
Benefit of dev with docker: decouple machine from dev environment, make host clean, and dev everywhere
## Create C# website and run webapp
### Update docker-compose file in csharp service
Adding following to services:
```Dockerfile
working_dir: /work
entrypoint: /bin/sh
stdin_open: true
tty: true
volumes:
- ./c#/src/:/work
ports:
- 5000:5000
```
where:
* `working_dir` set working directory in container
* `volumes` set for mounting volume, which we directly mount source code into container working directory
* `5000:5000`, 1st one is the port on host, 2nd one is the exposed port in container
### Build/Run dotnet image, and working it in VSCode
1. `docker-compose csharp`, where `csharp` is the service name defined in docker-compose file
2. `docker-compose run -d csharp` to start container
3. In VScode, select the running container and click **Attach Visual Studio Code**, otherwise the normal vscode don't have privilege to run it later
### Create & Run .NET webapp
1. In new created attached VSCode window `dotnet new webapp` to create .NET app source code, these source code are all `root` based
2. Modify the source code by adding `.UseUrl("http://*:5000")` to create a simple website in `Program.cs`
3. Build the webapp by `dotnet build` in attached VSCode
4. Run webapp by `dotnet run` in attached VSCode, we can now view the web in host via `localhost:5000`
Further detail can be referred in:
* [ASP.NET Core in a container](https://code.visualstudio.com/docs/containers/quickstart-aspnet-core)

View File

@ -1,45 +0,0 @@
# Working with Dockerfiles (.NET, Golang, Python, NodeJS)
## Three Problems for docker beginner
1. Programmer need be able to navigate through windows/mac/linux OS using cmd line fluently:
1. `ls/dir` for list files
2. `cd` for move through file systems
2. Working Directories
1. Show current working directory `echo $PWD`
2. Don't hardcode path to file in docker files
3. Understand *docker volume* `docker run -v hostPath:containerPath`
1. e.g. `docker run -it -v $PWD:/work -w /work alpine:3.9 /bin/sh`
## dockerfile
Before bring up docker container, we need first create `dockerfile` to describe the image that we want to use to create containers.
For different languages, `c#`, `golang` & `python` each has a separate dockerfile. Each describe the complete environment can be used to develop corresponding programs
## Docker compose
`Compose` is an **orchestration** tool that used to spinning up multi-container distributed applications, it shipped with Docker by default. It's not ideal for large system setup, but great for development purpose and testing
It's easy to define a multi-container app and accompanying services using **one single file**, and start it using single command. We can also build/push images to registry using `docker-compose`
The compose file is `docker-compose.yaml` in PWD
* Build image individually: `docker-compose build <service_name>`
* Build all images: `docker-compose build`
### Create & Push images built from docker-compose
1. Create image using `docker-compose.yaml` & `Dockerfile`
1. Write `Dockerfile` and `docker-compose.yaml`
2. Create image: `docker-compose build first-public-dockerhub-image` where
1. `first-public-dockerhub-image` is the service name of the image
2. Login Docker hub
1. `docker login`, without specify server address, it automatically connect to DockerHub
2. Enter Username & Password
3. Now computer has login DockerHub successfully
3. Go to Docker hub, create the `first-public-dockerhub-image` repository
4. Go to command line, push the image `docker push cruxligh/first-public-dockerhub-image`
References:
1. How to register a dockerhub registry: [Docker Hub QuickStart](https://docs.docker.com/docker-hub/)