in this section, we will walk through the definition of what is the docker and how to build a single docker.
1. What is docker?
- is a platform for building, running, and sharing applications with a container. The application deploys by the container is call containerization.
- Docker supplies:
- Flexible: easily customize and set up containerization.
- Lightweight: less cost than a virtual machine (VMWare).
- Portable: build local, or on a server.
- Loosely coupled: a container is independent, self-sufficient, and encapsulated.
- Scalable: easy to create another container from the built image.
- Secure: the container could only accept the ENV coming from outside when it is set up to accept them.
2. What are image and container?
- image is a package built from a standard Dockerfile. It includes everything such as filesystem, environment, etc. to run an application.
- container is a process running that create from an image. We could have many containers running from an image.
3. Install Docker
4. Overview of Docker structure
- Common commands:
+ start stop docker daemon:
systemctl start/stop/restart docker
+ show full config
docker inspect redis
+ show log file path
docker inspect -f '{{json .LogPath}}' redis
+ show volume mounting
docker inspect -f '{{json .Mounts }}' mongodb
+ list all port binding
docker inspect --format='{{range $p, $conf := .NetworkSettings.Ports}} {{$p}} -> {{(index $conf 0).HostPort}} {{end}}' redis
+ list all port mapping
docker inspect --format='{{(index (index .NetworkSettings.Ports "8787/tcp") 0).HostPort}}' redis
5. Build an image
- Run Command
docker build [OPTIONS] FOLDER_PATH | URL | -
5.1 build from a specified file path
docker build - < [file path]5.2 build from Folder Path
# find Dockerfile of the folder then build
docker build .5.3 build from URL REPO
# clone the repo, go to folder and build file names Dockerfile
docker build myrepo.git#mybranch:folderhavingDockerfile5.4 build from TAR file
# download file, untar file then build file names Dockerfile
docker build http://server/context.tar.gz
- There are many helpful [options] supported by docker build command. See here. Some frequently using options:
* — build-arg: set ENV to image.
* — file, -f: specify the file name to build instead of default Dockerfile
* — memory, -m: the taking bytes memory limitation
* — memory-swap: the taking bytes memory swap limitation
* — no-cache: don’t use any cache from the old image.
* — pull: get the newest version of the image [FROM].
* — tag, -t: mark repo-name and tag for the image. -t my-ubuntu:v1
* — target: there are multiple stages inside dockerfile [FROM] and we only want to process one stage block.
FROM ubuntu AS stage1
echo "this is block stage1"
FROM hello-world AS stage2
echo "if target is stage2, this never be print because of error"
6. Manage image
- the sample Dockerfile
- Base Image: In an Dockerfile there is only one base-image. If there are many FROM, the last one would be used. If the base-image doesn’t exist, the docker will pull the new one.
- ImageID, Repo, and Tag:
As we can see here, from the imageID “bf…ae65” we have 2 repo:tag that are “hello-world:latest” and “hello-world:v1”.
- Docker manages Image version by mark “repo:tag”.
- build and create: docker build -t hello-world:v1 .
- If we want to remove the version we use command “docker rmi [repo:tag]”. It equivalent to untag the version out of the image.
- “docker rmi [imageID]” if there is only one VERSION of it.
- if the latest version of image is untagged, the image would be also deleted.
- The image version only can be untagged/deleted when no container is using it.
7. store an image
- Docker Hub supplies one free private repo.
- The repo must be private and we need to be login before pull/push. command “docker login” (“docker logout” after finishing)
- After tagging the version to the same name with your repo, run command to push it. ex: we have register repo named “peter/testing” and the version of image in our local is “ubuntu:v0”.
=> docker tag ubuntu:v0 peter/testing:ubuntu-v0
=> docker push peter/testing:ubuntu-v0
8. pull an image
- docker pull [OPTIONS] NAME[:TAG|@DIGEST]
- docker pull ubuntu:latest
this will pull the image ubuntu with tag latest
the same thing would happen in Dockerfile [FROM] - options support
9. run an image to a container
- Run Command
docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
docker run --name cont1 -i ubuntu:latest
- There are many helpful [options] supported by docker run command. See here. Some frequently using options:
* — detach, -d: run container in the background mode.
* — env, -e: add env to container
* — env-file: add env file
* — expose: mapping port outside to inside container. ex: 8080:80
* — interactive , -i: Keep STDIN open
* — ip: set ip for the process of container
* — memory, -m: memory limit
* — memory-swap: memory swap limit
* — name: container name
* — volume, -v: mapping folder outside to inside container
* — cidfile: write the containerID to this path
* — workdir, -w: set the current working directory of the container.
10. commit a container to an image
- docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]]
- options support
11. save an image/images to tar file
- docker save -o bk_image1.tar hello-world:v0
- docker save hello-world:v0 > bk_image1.tar
- docker save hello-world:v0 | gzip > bk_image1.tar.gz
12. load an image/images from a tar file
- docker load < bk_image1.tar
- docker load -i bk_image1.tar.gz