Skip to content

Creating and Managing Container Images

Manually create

1. pull an image and run

docker container run -it \
    --name example \
    alpine:3.10 /bin/sh

2. add something

apk update && apk add iputils

3. see diff

docker container ls -a | grep example
docker container diff example

4. commit

docker container commit example this-is-my-alpine
docker image ls
docker image history this-is-my-alpine

Using Dockerfiles

Dockerfile example

FROM python:2.7
RUN mkdir -p /app
WORKDIR /app
COPY ./requirements.txt /app/
RUN pip install -r requirements.txt
CMD ["python", "main.py"]

FROM

FROM scratch
FROM centos:7

RUN

RUN yum install -y wget
RUN apt-get update && apt-get install -y wget
RUN mkdir -p /app && cd /app

COPY ADD

COPY . /app
COPY ./web /app/web
COPY sample.txt /data/my-sample.txt
ADD sample.tar /app/bin/
ADD http://example.com/sample.txt /data/

WORKDIR

WORKDIR /app/bin

CMD ENTRYPOINT

CMD command param1 param2
ENTRYPOINT ["ping"]
CMD ["-c","3","8.8.8.8"]

build from Dockerfile

prepare Dockerfile like this

FROM alpine:3.10
ENTRYPOINT ["ping"]
CMD ["-c","3","8.8.8.8"]
docker image build -t pinger -f Dockerfile .
docker container run --rm -it pinger
docker container run --rm -it pinger -w 5 127.0.0.1

complex example

Dockerfile

FROM node:12.5-stretch
RUN mkdir -p /app
WORKDIR /app
COPY package.json /app/
RUN npm install
COPY . /app
ENTRYPOINT ["npm"]
CMD ["start"]

multi-step builds

hello.c

#include <stdio.h>
int main (void)
{
    printf ("Hello, world!\n");
    return 0; 
}

Dockerfile

FROM alpine:3.7
RUN apk update && apk add --update alpine-sdk
RUN mkdir /app
WORKDIR /app
COPY . /app
RUN mkdir bin
RUN gcc -Wall hello.c -o bin/hello
CMD /app/bin/hello
docker image build -t hello-world .
docker image ls | grep hello-world

define the start command

ENTRYPOINT java -jar jenkins.war

ENTRYPOINT ./docker-entrypoint.sh

Saving images

docker image save -o ./backup/this-is-my-alpine.tar this-is-my-alpine

Loading images

docker image load -i ./backup/this-is-my-alpine.tar

Pushing images to a registry

docker image tag alpine:latest xxxxx/alpine:1.0
docker login -u xxxxx -p <password>
docker image push xxxxx/alpine:1.0

Image namespaces

<registry URL>/<User or Org>/<name>:<tag>