【Docker】docker
Docker
what is docker ?
- what is container and what problem it solved ?
- a way to package application with all the necessary dependencies and configration
- portable artifact, easily moved and shared around.
- makes development and deployment more efficient
- container repository
- develop application
Where do containers live ?
- container repository
- private repository
- public repository for docker
How containers improved our application development?
- before there is containers
- installation process different on each OS environment
- many steps where something can go wrong.
- after containers
- own islated environment
- package with all needed configration
- one command to install the app
- run same app with 2 different versions
How containers improved our application deployment?
- before there is containers
- configuration on the server needed
- dependency version conflicts
- texture guide of deployment
- misunderstandings
after containers
- develpoer and operations work together to package the application in a container
- No environmental configration needed on server (except docker runtime)
What is a container ?
- layers of images (多层 image 的堆积)
- Mostly Linux base Image because small in size.
- application image on the top
Docker Image VS. Docker container
- Docker image: the actual package, artifact, that can move around.
- Doker container: actual start the application, container is created
Installation process
More Info https://docs.docker.com/engine/install/
after all the process, it should be look like following
➜ ~ sudo docker run hello-world Hello from Docker! This message shows that your installation appears to be working correctly. To generate this message, Docker took the following steps: 1. The Docker client contacted the Docker daemon. 2. The Docker daemon pulled the "hello-world" image from the Docker Hub. (amd64) 3. The Docker daemon created a new container from that image which runs the executable that produces the output you are currently reading. 4. The Docker daemon streamed that output to the Docker client, which sent it to your terminal. To try something more ambitious, you can run an Ubuntu container with: $ docker run -it ubuntu bash Share images, automate workflows, and more with a free Docker ID: https://hub.docker.com/ For more examples and ideas, visit: https://docs.docker.com/get-started/
Docker basic commands
docker pull
➜ ~ docker pull redis
Using default tag: latest
latest: Pulling from library/redis
c229119241af: Downloading [===========> ] 7.161MB/31.38MB
c229119241af: Downloading [================> ] 10.39MB/31.38MB
5e59eaa723f1: Download complete
fd5ad7669819: Download complete
c229119241af: Downloading [===========================================> ] 27.25MB/31.38MB
20c7cfac25de: Download complete
c229119241af: Pull complete
bed41eb8190a: Pull complete
5e59eaa723f1: Pull complete
fd5ad7669819: Pull complete
566c064eef6e: Pull complete
20c7cfac25de: Pull complete
Digest: sha256:69a3ab2516b560690e37197b71bc61ba245aafe4525ebdece1d8a0bc5669e3e2
Status: Downloaded newer image for redis:latest
docker.io/library/redis:latesa
docker images
➜ ~ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
redis latest bba24acba395 7 days ago 113MB
hello-world latest feb5d9fea6a5 6 months ago 13.3kB```
docker run some-image
:
run the actual images in the container
➜ ~ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
redis latest bba24acba395 7 days ago 113MB
hello-world latest feb5d9fea6a5 6 months ago 13.3kB
➜ ~ docker run redis
1:C 05 Apr 2022 16:30:45.645 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1:C 05 Apr 2022 16:30:45.646 # Redis version=6.2.6, bits=64, commit=00000000, modified=0, pid=1, just started
1:C 05 Apr 2022 16:30:45.646 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
1:M 05 Apr 2022 16:30:45.647 * monotonic clock: POSIX clock_gettime
1:M 05 Apr 2022 16:30:45.648 * Running mode=standalone, port=6379.
1:M 05 Apr 2022 16:30:45.648 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
1:M 05 Apr 2022 16:30:45.648 # Server initialized
1:M 05 Apr 2022 16:30:45.648 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
1:M 05 Apr 2022 16:30:45.649 * Ready to accept connections
docker ps
docker ps = list runing containers
➜ ~ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a95d99752479 redis "docker-entrypoint.s…" 23 seconds ago Up 22 seconds 6379/tcp magical_saha
docker run -d some_image
the image will run in the background
docker stop some_image
stop the image
docker start some_image
start the image
➜ ~ docker run -d redis
5c1aa41848c6e868c653761a5bcc14d2f1e36b49d10b72f54ac12b8093d781ff
➜ ~ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5c1aa41848c6 redis "docker-entrypoint.s…" 8 seconds ago Up 7 seconds 6379/tcp goofy_shirley
➜ ~ dcoker stop 5c1aa41848c6
zsh: command not found: dcoker
➜ ~ docker stop 5c1aa41848c6
5c1aa41848c6
➜ ~ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
➜ ~
docker ps -a
show you all the history
➜ ~ docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
5c1aa41848c6 redis "docker-entrypoint.s…" 5 minutes ago Exited (0) 4 minutes ago goofy_shirley
2f393cc55bc0 redis "docker-entrypoint.s…" 6 minutes ago Exited (0) 6 minutes ago goofy_kilby
a95d99752479 redis "docker-entrypoint.s…" 22 hours ago Exited (0) 22 hours ago magical_saha
a4ab8c97a339 hello-world "/hello" 23 hours ago Exited (0) 23 hours ago cool_shannon
ea34afb525e4 hello-world "/hello" 23 hours ago Exited (0) 23 hours ago lucid_montalcini
docker run redis:4.0
this will pull the redis image and run it
➜ ~ docker run redis:4.0
Unable to find image 'redis:4.0' locally
4.0: Pulling from library/redis
54fec2fa59d0: Pull complete
9c94e11103d9: Pull complete
04ab1bfc453f: Pull complete
7988789e1fb7: Pull complete
8ce1bab2086c: Pull complete
40e134f79af1: Pull complete
Digest: sha256:2e03fdd159f4a08d2165ca1c92adde438ae4e3e6b0f74322ce013a78ee81c88d
Status: Downloaded newer image for redis:4.0
1:C 06 Apr 14:43:53.447 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1:C 06 Apr 14:43:53.447 # Redis version=4.0.14, bits=64, commit=00000000, modified=0, pid=1, just started
1:C 06 Apr 14:43:53.447 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
1:M 06 Apr 14:43:53.449 * Running mode=standalone, port=6379.
1:M 06 Apr 14:43:53.450 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
1:M 06 Apr 14:43:53.450 # Server initialized
1:M 06 Apr 14:43:53.450 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
1:M 06 Apr 14:43:53.451 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
1:M 06 Apr 14:43:53.451 * Ready to accept connections
redis and redis4.0 have same port, wht there is no conflict ?
➜ ~ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
23d9ba2f5d93 redis:4.0 "docker-entrypoint.s…" 7 seconds ago Up 6 seconds 6379/tcp thirsty_mclean
416268d863f3 redis "docker-entrypoint.s…" 12 seconds ago Up 12 seconds 6379/tcp gallant_mcclintock
we bind different verisons with same container port TO the different port in our host
➜ ~ docker run -d -p6000:6739 redis
63891be7dab2b4361ec9d5e49d2724be67ddd4ad22e279b9fdb560ac753e5ac8
➜ ~ docker run -d -p6000:6739 redis:4.0
33dd828903a50debd8c21e9e602d6821bb9a3049b895ac9d97739f7aae2bb12f
docker: Error response from daemon: driver failed programming external connectivity on endpoint brave_goldstine (a7057c58a6b85416b20460d6421ce9688939ad534b9236e0cf7ba6710e21b717): Bind for 0.0.0.0:6000 failed: port is already allocated.
// ↑↑↑↑↑ NOTE: if we bind the port to a same host port 6000 it will be a error
we bind redis → 6000
redis:4.0 → 6001
➜ ~ docker run -d -p6001:6739 redis:4.0
21e8f41daf0c7e20077686b0960ceb68e6e0e05156ffea2971d477de9e838fab
result
➜ ~ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
21e8f41daf0c redis:4.0 "docker-entrypoint.s…" 47 seconds ago Up 47 seconds 6379/tcp, 0.0.0.0:6001->6739/tcp, :::6001->6739/tcp vibrant_kirch
63891be7dab2 redis "docker-entrypoint.s…" About a minute ago Up About a minute 6379/tcp, 0.0.0.0:6000->6739/tcp, :::6000->6739/tcp intelligent_merkle
docker log [hash/name]
➜ ~ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
21e8f41daf0c redis:4.0 "docker-entrypoint.s…" 9 minutes ago Up 9 minutes 6379/tcp, 0.0.0.0:6001->6739/tcp, :::6001->6739/tcp vibrant_kirch
63891be7dab2 redis "docker-entrypoint.s…" 10 minutes ago Up 10 minutes 6379/tcp, 0.0.0.0:6000->6739/tcp, :::6000->6739/tcp intelligent_merkle
➜ ~ docker logs 21e8f41daf0c
1:C 06 Apr 14:56:23.749 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1:C 06 Apr 14:56:23.749 # Redis version=4.0.14, bits=64, commit=00000000, modified=0, pid=1, just started
1:C 06 Apr 14:56:23.749 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
1:M 06 Apr 14:56:23.751 * Running mode=standalone, port=6379.
1:M 06 Apr 14:56:23.751 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
1:M 06 Apr 14:56:23.751 # Server initialized
1:M 06 Apr 14:56:23.751 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
1:M 06 Apr 14:56:23.751 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
1:M 06 Apr 14:56:23.752 * Ready to accept connections
➜ ~ docker logs vibrant_kirch
1:C 06 Apr 14:56:23.749 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1:C 06 Apr 14:56:23.749 # Redis version=4.0.14, bits=64, commit=00000000, modified=0, pid=1, just started
1:C 06 Apr 14:56:23.749 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
1:M 06 Apr 14:56:23.751 * Running mode=standalone, port=6379.
1:M 06 Apr 14:56:23.751 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
1:M 06 Apr 14:56:23.751 # Server initialized
1:M 06 Apr 14:56:23.751 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
1:M 06 Apr 14:56:23.751 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
1:M 06 Apr 14:56:23.752 * Ready to accept connections
name the docker name by ourself
➜ ~ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
➜ ~ docker run -d -p6000:6037 --name older-redis redis:4.0
6c421f78bedb26963708fcc2fd8a369c58ac3c7fadc5083390756a2bd8bbba3c
➜ ~ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6c421f78bedb redis:4.0 "docker-entrypoint.s…" 3 seconds ago Up 2 seconds 6379/tcp, 0.0.0.0:6000->6037/tcp, :::6000->6037/tcp older-redis
➜ ~
docker exec -it 6c421f78bedb /bin/bash
go to the interactive terminal to debug the container
➜ ~ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6c421f78bedb redis:4.0 "docker-entrypoint.s…" 2 minutes ago Up 2 minutes 6379/tcp, 0.0.0.0:6000->6037/tcp, :::6000->6037/tcp older-redis
➜ ~ docker exec -it 6c421f78bedb /bin/bash
root@6c421f78bedb:/data# ls
root@6c421f78bedb:/data# pwd
/data
root@6c421f78bedb:/data# cd /
root@6c421f78bedb:/# ls
bin boot data dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
root@6c421f78bedb:/# env
HOSTNAME=6c421f78bedb
REDIS_DOWNLOAD_SHA=1e1e18420a86cfb285933123b04a82e1ebda20bfb0a289472745a087587e93a7
PWD=/
HOME=/root
REDIS_VERSION=4.0.14
GOSU_VERSION=1.12
TERM=xterm
REDIS_DOWNLOAD_URL=http://download.redis.io/releases/redis-4.0.14.tar.gz
SHLVL=1
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
_=/usr/bin/env
OLDPWD=/data
root@6c421f78bedb:/# exit
exit