Container

A container is a software package that includes everything needed to run it in any environment. Containers virtualize the operating system in this way and run anywhere, from a private data center to the public cloud or even a developer’s personal laptop. Every Google product, from Gmail to YouTube to Google Search, runs in containers. Containerization lets development teams move faster, deploy software more efficiently, and achieve unprecedented levels of scalability. Over the years, Google has accumulated a lot of experience running containerized workloads and has continually shared this knowledge with the community, from contributing the cgroup feature to the Linux kernel in the early days, to open-sourcing the design of its internal tooling as the Kubernetes project.
Key Features
Section titled “Key Features”A consistent environment
Section titled “A consistent environment”With containers, developers can create predictable environments that are isolated from other applications. A container can also include the software dependencies an application needs (such as specific versions of programming language runtimes and other software libraries). From the developer’s point of view, all of this is always consistent regardless of where the application is ultimately deployed. The natural result is improved productivity, because developers and IT operations teams spend less time chasing bugs and diagnosing environment differences and can focus on delivering new features to users. The number of bugs also decreases, because the assumptions developers make in their development and test environments hold true in production.
Runs almost anywhere
Section titled “Runs almost anywhere”Containers run in virtually any environment: Linux, Windows, and Mac operating systems, virtual machines, bare metal, a developer’s machine, data centers, on-premises environments, the public cloud, and more. This makes development and deployment far easier. Because the Docker image format for containers is so widely used, portability is also excellent. Whatever your software’s runtime environment, you can use containers.
Isolation
Section titled “Isolation”Containers virtualize CPU, memory, storage, and network resources at the OS level, giving developers an OS sandbox environment that is logically isolated from other applications.
| Container Pros | VM Pros | |
|---|---|---|
| Consistent runtime environment | ✓ | ✓ |
| Application sandboxing | ✓ | ✓ |
| Disk space savings | ✓ | |
| Low overhead | ✓ |
Key Technologies
Section titled “Key Technologies”Container Engine
Section titled “Container Engine”- Docker : a somewhat heavy process that runs as Root, which introduces security vulnerabilities
- Alternative engines : ContainerD, Cri-O, runC
Container Orchestration
Section titled “Container Orchestration”A platform that provides the NW, Storage, Resources, and other capabilities needed to run a Workload (Application) in Isolation mode through virtualization technology,
and that lets you manage the Life-Cycle of instances. It also provides the Tools and APIs needed for development/operations.
- kubernetes
- openshift
- dc/os + marathon framework
- docker swarm
Container Build
Section titled “Container Build”Dockerfile
Section titled “Dockerfile”Content in {..} are user-defined items
FROM {Container Registry URI}/{Project Name}/{Repository Name}:{Tag}
# run Shell Commands for installation, configuration, etc. needed by the container# multiple command blocks can be executedRUN {Shell Command}
USER {user:[group]}
# set the working/execution path# multiple command blocks can be executedWORKDIR {/the/workdir/path}
# set environment variables in the container OS# multiple command blocks can be executedENV {ENV-KEY} {Value}
# copy files into the container# multiple command blocks can be executedADD {src} {destination}
# copy files into the container# multiple command blocks can be executedCOPY {src} {destination}
# run Shell Commands for installation, configuration, etc. needed by the container# multiple command blocks can be executedRUN {Shell Command}
# expose the port used by the container to the outside.# ex) springboot port 8080EXPOSE 8080# specify additionally when exposing extra ports from the containerEXPOSE 9090
# optional default execution command for starting the containerENTRYPOINT {Shell Command}
# optionally extend the ENTRYPOINT command for starting the container# if there is no ENTRYPOINT, the full execution command can be specified# this area can be overridden by docker runCMD { Shell command extending to ENTRYPOINT }- FROM : the base/baseline OS setting for the container you are creating
. Container Registry URI : the container repository service address
. Project : denotes a container group; user permissions are typically managed at the Project level
. Repository : container storage space
. Tag: information used to distinguish detailed versions within the same container storage space . With AS [name] you can name the container in the Dockerfile so that you can specify the source location when copying with COPY —from - RUN : runs the Shell Command needed for the container build
- USER : specifies the user to apply when running ENTRYPOINT, CMD. The user must be added via RUN
- WORKDIR : specifies the execution path for RUN and ENTRYPOINT, CMD
- COPY : copies from source to destination. Using —from, you can specify another container designated by name as the source
- Add : performs the same function as COPY, and can specify the source as a URL; for tar files it automatically extracts and copies them
- ENTRYPOINT : optional; the command executed first when the container starts
- CMD : optional; a way to extend the command line that runs first when the container starts
If ENTRYPOINT exists, it is passed as an additional argument; if there is no ENTRYPOINT, you can specify the execution command directly
Container Build Configuration
Section titled “Container Build Configuration”-
Simple container development : a method for installing an already-built App, middleware, etc.
#Springboot jar exampleFROM openjdk:11# create the working directoryRUN mkdir /app# set the working/current path (same as cd)WORKDIR /appARG JAR_FILE=target/*.jarEXPOSE 8080COPY ${JAR_FILE} /app/app.jarENTRYPOINT ["java","-jar","/app/app.jar"] -
Development using Stage Build: used with GitAction, GitLab CI, Circle CI, etc.; when build environments vary, the Build container and the Runtime container are separated so that unnecessary content is excluded from the RunTime container, reducing size and speeding up startup (Bootstrap)
# declare the base container used for the build.# use as [Name] to name the build container for reuse as a sourceFROM maven:3.8.1-jdk-11 AS build# copy required files from local into the containerCOPY src /usr/src/app/srcCOPY pom.xml /usr/src/app# RUN mvn -f /usr/src/app/pom.xml clean dependency:go-offline -B# run the maven buildRUN --mount=type=cache,target=~/.m2 mvn -f /usr/src/app/pom.xml clean package -DskipTests# declare the base container for the target containerFROM openjdk:11# create the working directoryRUN mkdir /app# set the working/current path (same as cd)WORKDIR /app# copy required files from the build container to the target# 'build' is the container name declared aboveCOPY --from=build /usr/src/app/target/*.jar /app/app.jar# port to expose externallyEXPOSE 8080#execution commandENTRYPOINT ["java","-jar","/app/app.jar"]
Docker Command
Section titled “Docker Command”-
build
Terminal window docker build -t [repository uri]/[project name]/[container name]:[tag] .docker build .# if built without a tag, you can find the [image id] via docker images and tag it -
tag : a container alias, mainly used to distinguish versions.
Terminal window docker tag [image id] [repository uri]/[project name]/[container name]:[tag]docker tag [repository uri]/[project name]/[container name]:[old tag] [repository uri]/[project name]/[container name]:[new tag] -
push : saves the container to the server repository (Registry) (login required)
The server address and the repository uri of the tag must matchTerminal window docker push [repository uri]/[project name]/[container name]:[tag] -
run : runs the container
Terminal window docker run \# Detached after run \-d \--name [container instance name] \# volume mount \-v [local volume]:[container path to mount] \# declare container environment variables \-e "key=value" \# port mapping/forward \-p [local port]:[container port] \# Container Image URI to run \IMAGE[:TAG|@DIGEST] \# if specified, replaces the CMD block of the Dockerfile (optional) \[command for CMD(optional)] -
logs : check container stdout
- —follow, -f: follow the logs continuously
- —tail, -n : number of last log lines