Skip to content

Container

Interactive Diagram

새 탭 ↗

image

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.

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.

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.

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 ProsVM Pros
Consistent runtime environment✓✓
Application sandboxing✓✓
Disk space savings✓
Low overhead✓
  • Docker : a somewhat heavy process that runs as Root, which introduces security vulnerabilities
  • Alternative engines : ContainerD, Cri-O, runC

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

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 executed
RUN {Shell Command}
USER {user:[group]}
# set the working/execution path
# multiple command blocks can be executed
WORKDIR {/the/workdir/path}
# set environment variables in the container OS
# multiple command blocks can be executed
ENV {ENV-KEY} {Value}
# copy files into the container
# multiple command blocks can be executed
ADD {src} {destination}
# copy files into the container
# multiple command blocks can be executed
COPY {src} {destination}
# run Shell Commands for installation, configuration, etc. needed by the container
# multiple command blocks can be executed
RUN {Shell Command}
# expose the port used by the container to the outside.
# ex) springboot port 8080
EXPOSE 8080
# specify additionally when exposing extra ports from the container
EXPOSE 9090
# optional default execution command for starting the container
ENTRYPOINT {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 run
CMD { Shell command extending to ENTRYPOINT }
  1. 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
  2. RUN : runs the Shell Command needed for the container build
  3. USER : specifies the user to apply when running ENTRYPOINT, CMD. The user must be added via RUN
  4. WORKDIR : specifies the execution path for RUN and ENTRYPOINT, CMD
  5. COPY : copies from source to destination. Using —from, you can specify another container designated by name as the source
  6. Add : performs the same function as COPY, and can specify the source as a URL; for tar files it automatically extracts and copies them
  7. ENTRYPOINT : optional; the command executed first when the container starts
  8. 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
  1. Simple container development : a method for installing an already-built App, middleware, etc.

    #Springboot jar example
    FROM openjdk:11
    # create the working directory
    RUN mkdir /app
    # set the working/current path (same as cd)
    WORKDIR /app
    ARG JAR_FILE=target/*.jar
    EXPOSE 8080
    COPY ${JAR_FILE} /app/app.jar
    ENTRYPOINT ["java","-jar","/app/app.jar"]
  2. 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 source
    FROM maven:3.8.1-jdk-11 AS build
    # copy required files from local into the container
    COPY src /usr/src/app/src
    COPY pom.xml /usr/src/app
    # RUN mvn -f /usr/src/app/pom.xml clean dependency:go-offline -B
    # run the maven build
    RUN --mount=type=cache,target=~/.m2 mvn -f /usr/src/app/pom.xml clean package -DskipTests
    # declare the base container for the target container
    FROM openjdk:11
    # create the working directory
    RUN 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 above
    COPY --from=build /usr/src/app/target/*.jar /app/app.jar
    # port to expose externally
    EXPOSE 8080
    #execution command
    ENTRYPOINT ["java","-jar","/app/app.jar"]
  1. 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
  2. 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]
  3. push : saves the container to the server repository (Registry) (login required)
    The server address and the repository uri of the tag must match

    Terminal window
    docker push [repository uri]/[project name]/[container name]:[tag]
  4. 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)]
  5. logs : check container stdout

    • —follow, -f: follow the logs continuously
    • —tail, -n : number of last log lines