Docker
- What is Docker?
- Why we need docker?
- How docker works?
What is Docker?
Docker is a tool to run application on isolated environment and consist environment on container.
Why we need docker?
Same environment among the team
Sandbox projects
Don't have to set up or install dependencies.
How docker works?
Container
container is running instance of "image". Container is one of the process of OS.
Image
image is template of the environment. It consists of OS, software, application code. You can find existing image on Docker hub.
Layer
If you create an image on top of other images, existing images will be layers of the image.
Docker file
Images defined by docker file. It is text file to declare the steps to set up image. For example, the command to install software on the environment, run shell command so on. After you run docker file, image will be built according to docker file.
Docker compose
Docker compose allows to have multiple environment. In case you are building micro services, you want to run images at the same time. Docker compose takes each services, or event only just client and backend environment.
https://www.youtube.com/watch?v=Qw9zlE3t8Ko
https://www.youtube.com/watch?v=JprTjTViaEA
Docker file commands
FROM
FROM command allows to download existing image from docker hub. This image will be an layer.
FROM: php:7.0-apache
COPY
Copy file
COPY src/ /var/www/html
CMD
run command
CMD ["python", "api.py"]
EXPOSE
Expose port number
EXPOSE 80
Docker command
run
docker run
command executes three command implicitly.docker pull
, docker create
,and docker start.
docker run hello-world
We can also add tag. You can find a tag on docker hub.
docker run hello-world:latest
docker run docker/whalesay:latest
=>
Unable to find image 'docker/whalesay:latest' locally // Checking if there is the image on local machine
latest: Pulling from docker/whalesay // Pulling the image from docker hub
e190868d63f8: Pull complete // Image Layer 1
909cd34c6fd7: Pull complete // Image Layer 2
0b9bfabab7c1: Pull complete // Image Layer 3
a3ed95caeb02: Pull complete // Image Layer 4
00bf65475aba: Pull complete // Image Layer 5
c57b6bcc83e3: Pull complete // Image Layer 6
8978f6879e2f: Pull complete // Image Layer 7
8eed3712d2cf: Pull complete // Image Layer 8
Digest: sha256:178598e51a26abbc958b8a2e48825c90bc22e641de3d31e18aaf55f3258ba93b
Status: Downloaded newer image for docker/whalesay:latest
Check an image is on local machine
Pull the Image from Docker. Pull each image layer and if it is on local machine, it won't be pulled.
Create an container from the image
Run it.
Options
detach mode
Run container on background.
docker run -d
port
Connect host port and container port.
-p <host port number>:<container port number>
delete container
A container will be saved on the local machine as default. The container can be deleted automatically after stopping a container by passing --rm
option.
--rm
pull
Get image from docker hub. Default tag is latest
but it does not guarantee you to pull "latest" version. It is just name of the tag.
docker pull <container_name>:<tag_name>
docker pull docker/whalesay:latest
=>
latest: Pulling from docker/whalesay
e190868d63f8: Already exists
909cd34c6fd7: Already exists
0b9bfabab7c1: Already exists
a3ed95caeb02: Already exists
00bf65475aba: Already exists
c57b6bcc83e3: Already exists
8978f6879e2f: Already exists
8eed3712d2cf: Already exists
Digest: sha256:178598e51a26abbc958b8a2e48825c90bc22e641de3d31e18aaf55f3258ba93b
Status: Image is up to date for docker/whalesay:latest
create
Create a container
docker create
start
Launch a container
docker start
build
Creating an image from docker file, which is called "image build". build context is the file required to be contained in an image. Build context is not saved in the image but sent to Docker daemon. The Dockerfile on build context will be loaded by default. Build cache is enabled when dockerfile is not changed.
docker build -t <tag_name> <build_context (file path)>
Without build cache option
docker build -t <tag_name> <build_context (file path)> --no-cache
docker build -t docker-whale .
=>
Sending build context to Docker daemon 2.048kB // Sending build context to Docker daemon
Step 1/3 : FROM docker/whalesay:latest
---> 6b362a9f73eb
Step 2/3 : RUN apt-get -y update && apt-get install -y fortunes
---> Running in 1f3d51cda818
Ign http://archive.ubuntu.com trusty InRelease
Get:1 http://archive.ubuntu.com trusty-updates InRelease [65.9 kB]
Get:2 http://archive.ubuntu.com trusty-security InRelease [65.9 kB]
Hit http://archive.ubuntu.com trusty Release.gpg
Hit http://archive.ubuntu.com trusty Release
Get:3 http://archive.ubuntu.com trusty-updates/main Sources [514 kB]
Get:4 http://archive.ubuntu.com trusty-updates/restricted Sources [6449 B]
Get:5 http://archive.ubuntu.com trusty-updates/universe Sources [253 kB]
Get:6 http://archive.ubuntu.com trusty-updates/main amd64 Packages [1348 kB]
Get:7 http://archive.ubuntu.com trusty-updates/restricted amd64 Packages [21.4 kB]
Get:8 http://archive.ubuntu.com trusty-updates/universe amd64 Packages [587 kB]
Get:9 http://archive.ubuntu.com trusty-security/main Sources [199 kB]
Get:10 http://archive.ubuntu.com trusty-security/restricted Sources [5050 B]
Get:11 http://archive.ubuntu.com trusty-security/universe Sources [88.9 kB]
Get:12 http://archive.ubuntu.com trusty-security/main amd64 Packages [924 kB]
Get:13 http://archive.ubuntu.com trusty-security/restricted amd64 Packages [18.1 kB]
Get:14 http://archive.ubuntu.com trusty-security/universe amd64 Packages [292 kB]
Hit http://archive.ubuntu.com trusty/main Sources
Hit http://archive.ubuntu.com trusty/restricted Sources
Hit http://archive.ubuntu.com trusty/universe Sources
Hit http://archive.ubuntu.com trusty/main amd64 Packages
Hit http://archive.ubuntu.com trusty/restricted amd64 Packages
Hit http://archive.ubuntu.com trusty/universe amd64 Packages
Fetched 4388 kB in 10s (432 kB/s)
Reading package lists...
Reading package lists...
Building dependency tree...
Reading state information...
The following extra packages will be installed:
fortune-mod fortunes-min librecode0
Suggested packages:
x11-utils bsdmainutils
The following NEW packages will be installed:
fortune-mod fortunes fortunes-min librecode0
0 upgraded, 4 newly installed, 0 to remove and 116 not upgraded.
Need to get 1961 kB of archives.
After this operation, 4817 kB of additional disk space will be used.
Get:1 http://archive.ubuntu.com/ubuntu/ trusty/main librecode0 amd64 3.6-21 [771 kB]
Get:2 http://archive.ubuntu.com/ubuntu/ trusty/universe fortune-mod amd64 1:1.99.1-7 [39.5 kB]
Get:3 http://archive.ubuntu.com/ubuntu/ trusty/universe fortunes-min all 1:1.99.1-7 [61.8 kB]
Get:4 http://archive.ubuntu.com/ubuntu/ trusty/universe fortunes all 1:1.99.1-7 [1089 kB]
debconf: unable to initialize frontend: Dialog
debconf: (TERM is not set, so the dialog frontend is not usable.)
debconf: falling back to frontend: Readline
debconf: unable to initialize frontend: Readline
debconf: (This frontend requires a controlling tty.)
debconf: falling back to frontend: Teletype
dpkg-preconfigure: unable to re-open stdin:
Fetched 1961 kB in 2s (931 kB/s)
Selecting previously unselected package librecode0:amd64.
(Reading database ... 13116 files and directories currently installed.)
Preparing to unpack .../librecode0_3.6-21_amd64.deb ...
Unpacking librecode0:amd64 (3.6-21) ...
Selecting previously unselected package fortune-mod.
Preparing to unpack .../fortune-mod_1%3a1.99.1-7_amd64.deb ...
Unpacking fortune-mod (1:1.99.1-7) ...
Selecting previously unselected package fortunes-min.
Preparing to unpack .../fortunes-min_1%3a1.99.1-7_all.deb ...
Unpacking fortunes-min (1:1.99.1-7) ...
Selecting previously unselected package fortunes.
Preparing to unpack .../fortunes_1%3a1.99.1-7_all.deb ...
Unpacking fortunes (1:1.99.1-7) ...
Setting up librecode0:amd64 (3.6-21) ...
Setting up fortune-mod (1:1.99.1-7) ...
Setting up fortunes-min (1:1.99.1-7) ...
Setting up fortunes (1:1.99.1-7) ...
Processing triggers for libc-bin (2.19-0ubuntu6.6) ...
Removing intermediate container 1f3d51cda818
---> 46031d0a9eb7
// Launching temporary container and run command. If it is finished, it will be added as a layer.
Step 3/3 : CMD /usr/games/fortune | cowsay
---> Running in ac8b33a262d7
Removing intermediate container ac8b33a262d7
---> 3b21fbc1deac
// Save and image
Successfully built 3b21fbc1deac
// Add tag name
Successfully tagged whale_test:latest
docker run -p 80:80 hello-world
Copy
Copy and paste a file from local to docker container or vice-versa.
docker cp
Process
Check running docker processes
docker ps
=>
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c2d15d6362f7 nginx "nginx -g 'daemon of…" 24 hours ago Up 24 hours 0.0.0.0:5000->80/tcp test-nginx-html
2ff1b990a5dc nginx "nginx -g 'daemon of…" 24 hours ago Up 24 hours 0.0.0.0:6000->80/tcp stoic_borg
3ab584e40d3f nginx "nginx -g 'daemon of…" 24 hours ago Up 24 hours 0.0.0.0:7000->80/tcp t-nginx
0b66067340cd sameersbn/redis:latest "/sbin/entrypoint.sh" 5 weeks ago Up 2 days 0.0.0.0:32768->6379/tcp dashboardapi_redis_1
delete
Delete docker image.
docker rmi -f koka0828us/node-ssr
Delete docker container.
docker rm
image
docker images
=>
REPOSITORY TAG IMAGE ID CREATED SIZE
dashboardapi_web latest fd818c87bbd5 5 weeks ago 1.03GB
kei/node-web-app latest 2756a4c339ec 5 weeks ago 1.03GB
<none> <none> 100b77935236 5 weeks ago 1.03GB
<none> <none> b24037d62480 5 weeks ago 1.03GB
node carbon db01382a641a 5 weeks ago 672MB
redis latest bfcb1f6df2db 5 weeks ago 107MB
mongo latest 14c497d5c758 6 weeks ago 366MB
hello-world latest e38bc07ac18e 2 months ago 1.85kB
sameersbn/redis latest 32eae00a0f53 4 months ago 133MB
docker/whalesay latest 6b362a9f73eb 3 years ago 247MB
Tag
Create alias and add tag on an image.
docker tag <original container name> <new container name>:<tag name>
docker tag docker/whalesay my_whale
=>
docker/whalesay latest 6b362a9f73eb 3 years ago 247MB
my_whale latest 6b362a9f73eb 3 years ago 247MB
Push Image to docker hub
// If it is necessary
docker tag hello-world koka0828us/node-ssr
docker push koka0828us/node-ssr
Nginx
docker run --name <container name> -d \
-p 8080:80 nginx
Hosting static website
Bind mount
Bind mount is bind the content on local to docker container. Add -v
option and pass three arguments separated by :
.
A: content origin absolute path
B: content on docker container absolute path
C: read and write (option)
-v <value A>:<value B>:<value C>
For example,
-v /some/content:/usr/share/nginx/html:ro
docker run --name test-nginx-html -v /Users/kei/Dev/learning/docker/test:/usr/share/nginx/html:ro -d -p 5000:80 nginx