Docker overview
Similar to container isolation,
jar -- container (mysql,redis) -- publish to warehouse -- download from warehouse
Docker history
dotcloud was founded in 2010
2013 Docker open source
Docker1.0 was released on April 9, 2014
Virtual machines were used before
Development based on go language
Docker installation
#Uninstall old version yum remove docker \ docker-client \ docker-client-latest \ docker-common \ docker-latest \ docker-latest-logrotate \ docker-logrotate \ docker-engine #Required installation package yum install -y yum-utils #Set up a mirrored warehouse yum-config-manager \ --add-repo \ http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo #Update yum package index yum makecache fast #Install docker CE Community Edition and ee Enterprise Edition yum install docker-ce docker-ce-cli containerd.io #Start docker systemctl start docker #restart systemctl restart docker #View version docker version #Run Hello World docker run hello-world
#View mirror [root@VM-0-15-centos ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE hello-world latest feb5d9fea6a5 5 weeks ago 13.3kB
#Uninstall docker yum remove docker-ce docker-ce-cli containerd.io #Delete resource rm -rf /var/lib/docker rm -rf /var/lib/containerd
Review the Hello world process
Underlying principle
-
How does Docker work?
Docker is a client server system. The daemon of docker runs on the host. Access from the client through Socket
Ask!
When docker server receives the docker client instruction, it will execute this command! -
Why is Docker faster than Vm?
1. Docker has fewer abstraction layers than virtual machines. Because docker does not need Hypervisor to realize hardware resource virtualization, it runs in
The programs on the docker container directly use the hardware resources of the actual physical machine. Therefore, docker will increase in CPU and memory utilization
It has obvious advantages in efficiency.
2. docker uses the kernel of the host computer instead of the Guest OS.
Guest OS: system (OS) in VM (virtual machine);
HostOS: System in physical machine (OS);
Therefore, when creating a new container, docker does not need to reload an operating system kernel like the virtual machine. However, avoid guidance and addition
Loading the operating system kernel is a time-consuming and resource-consuming process. When creating a new virtual machine, the virtual machine software needs to load GuestOS and return
A new process is minute level. docker omits this complex process because it directly uses the host's operating system
It only takes a few seconds to create a docker container.
Common commands of Docker
Help command
docker version docker info docker <command> --help
Help documentation
Mirror command
#View mirror docker images docker images -q docker images -a [root@VM-0-15-centos ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE hello-world latest feb5d9fea6a5 5 weeks ago 13.3kB REPOSITORY Mirror warehouse source
#Search image [root@VM-0-15-centos ~]# docker search mysql NAME DESCRIPTION STARS OFFICIAL AUTOMATED mysql MySQL is a widely used, open-source relation... 11635 [OK] mariadb MariaDB Server is a high performing open sou... 4429 [OK] mysql/mysql-server Optimized MySQL Server Docker images. Create... 863 [OK] phpmyadmin phpMyAdmin - A web interface for MySQL and M... 362 [OK] centos/mysql-57-centos7 MySQL 5.7 SQL database server 91 #Optional, filter according to search --filter=STARS=3000 The searched image is STARS>3000
#Pull image docker pull mysql[:tag] tag Version number [root@VM-0-15-centos ~]# docker pull mysql Using default tag: latest # If you do not write tag, the default is latest latest: Pulling from library/mysql b380bbd43752: Pull complete # Layered download, the core federated file system of docker image f23cbf2ecc5d: Pull complete 30cfc6c29c0a: Pull complete b38609286cbe: Pull complete 8211d9e66cd6: Pull complete 2313f9eeca4a: Pull complete 7eb487d00da0: Pull complete 4d7421c8152e: Pull complete 77f3d8811a28: Pull complete cce755338cba: Pull complete 69b753046b9f: Pull complete b2e64b0ab53c: Pull complete Digest: sha256:6d7d4524463fe6e2b893ffc2b89543c81dec7ef82fb2020a1b27606666464d87#autograph Status: Downloaded newer image for mysql:latest docker.io/library/mysql:latest #Real address docker pull mysql #Equivalent to docker pull mysql[:latest] #Layered does not download duplicate files when the version changes
#delete mirror docker rmi -f mysql:5.7#Container name docker rmi -f 938b57d64674#Container ID docker rmi -f container ID container ID container ID container ID...#Delete multiple containers docker rmi -f $(docker images -aq)#Delete all containers
Container command
#Pull a mirror test docker pull centos
#Create a new container and start docker run [Optional parameters] image #Parameter description --name="Name" Container name -d Run in background mode -it Run interactively and enter the container to view the content -p (Lowercase) specifies the port of the container -p ip:Host port:Container port -p Host port:Container port -p Container port -P Randomly assigned port #test docker run -it centos /bin/bash # Enter container exit # sign out
#List all running containers -a #List currently running containers + bring out historically running containers -n=? #Show recent -q #Show id ONLY docker ps docker ps -a #List currently running containers + bring out historically running containers
exit #Stop the container and exit ctrl + P + Q #The container does not stop and exits
#Delete container docker rm container id#Delete the specified container. You cannot delete the running container. If you want to force the deletion, add - f docker rm -f $(docker ps -aq)#Delete all containers docker ps -a -q|xargs docker rm#Delete all containers
#Start and stop containers docker start container id #Start container docker restart container id #Restart container docker stop container id #Stop container docker kill container id #Force stop container
Common other commands
#Background startup container [root@VM-0-15-centos ~]# docker run -d centos cb597e7918560bb7ed2b9e7d068061e49a040e7a090f037d54747e6e56263144 [root@VM-0-15-centos ~]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES #Problem: found centos stopped #Reason: when docker runs in the background, there must be a foreground process. If docker finds that there is no application, it will automatically stop. After the container is started, it will stop immediately if it finds that it does not provide services
#view log docker logs -f -t --tail Display number of containers id docker logs -f -t container id #Write your own script docker run -d centos /bin/sh -c "while true;do echo kuangshen;sleep 1;done" #Show log docker logs -f -t --tail 10 06bea14ffa75
#View process information in the container docker top container id [root@VM-0-15-centos ~]# docker top 06bea14ffa75 UID PID PPID C STIME TTY TIME CMD root 19496 19477 0 21:13 ? 00:00:00 /bin/sh -c while true;do echo kuangshen;sleep 1;done root 20997 19496 0 21:18 ? 00:00:00 /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 1
#View the metadata of the image docker inspect container id [root@VM-0-15-centos ~]# docker inspect 06bea14ffa75 [ { "Id": "06bea14ffa75b860cdfe105f5045b1be83a6639eee9552ffc159ae01b401a5c4", "Created": "2021-11-07T13:13:56.785284074Z", "Path": "/bin/sh", "Args": [ "-c", "while true;do echo kuangshen;sleep 1;done" ], "State": { "Status": "running", "Running": true, "Paused": false, "Restarting": false, "OOMKilled": false, "Dead": false, "Pid": 19496, "ExitCode": 0, "Error": "", "StartedAt": "2021-11-07T13:13:57.071684542Z", "FinishedAt": "0001-01-01T00:00:00Z" }, "Image": "sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6", "ResolvConfPath": "/var/lib/docker/containers/06bea14ffa75b860cdfe105f5045b1be83a6639eee9552ffc159ae01b401a5c4/resolv.conf",
#Enter the currently running container #Method 1: enter the container when the container is running in the background docker exec -it container id /bin/bash [root@VM-0-15-centos ~]# docker exec -it 06bea14ffa75 /bin/bash [root@06bea14ffa75 /]# ps -ef UID PID PPID C STIME TTY TIME CMD root 1 0 0 13:13 ? 00:00:00 /bin/sh -c while true;do echo kuangshen;sleep 1;done root 719 0 0 13:25 pts/0 00:00:00 /bin/bash root 742 1 0 13:25 ? 00:00:00 /usr/bin/coreutils --coreutils-prog-shebang=sleep /usr/bin/sleep 1 root 743 719 0 13:25 pts/0 00:00:00 ps -ef #Mode 2: enter the container when the container is running in the background docker attach container id Executing current code... #difference docker exec #After entering the container, open a new terminal docker attach #Entering the terminal where the container is executing will not start a new process
#Copy files from container to host docker cp container id:In container path destination host path [root@VM-0-15-centos home]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES bad34ff90469 centos "/bin/bash" 31 seconds ago Up 30 seconds keen_einstein [root@VM-0-15-centos home]# docker attach bad34ff90469 [root@bad34ff90469 /]# cd /home/ [root@bad34ff90469 home]# ls [root@bad34ff90469 home]# touch test.java [root@bad34ff90469 home]# exit exit [root@VM-0-15-centos home]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES [root@VM-0-15-centos home]# docker cp bad34ff90469:/home/test.java /home [root@VM-0-15-centos home]# ls lighthouse redis test.java www #Copying is a manual process. In the future, the technology of -v volume can be used to realize automatic synchronization
Summary
practice
Installing nginx
[root@VM-0-15-centos home]# docker search nginx NAME DESCRIPTION STARS OFFICIAL AUTOMATED nginx Official build of Nginx. 15763 [OK] jwilder/nginx-proxy Automated Nginx reverse proxy for docker con... 2091 [OK] richarvey/nginx-php-fpm Container running Nginx + PHP-FPM capable of... 818 [OK] jc21/nginx-proxy-manager Docker container for managing Nginx proxy ho... 273 linuxserver/nginx An Nginx container, brought to you by LinuxS... 159 tiangolo/nginx-rtmp Docker image with Nginx using the nginx-rtmp... 143 [OK] jlesage/nginx-proxy-manager Docker container for Nginx Proxy Manager 143 [OK] alfg/nginx-rtmp NGINX, nginx-rtmp-module and FFmpeg from sou... 110 [OK] jasonrivers/nginx-rtmp Docker images to host RTMP streams using NGI... 95 [OK] nginxdemos/hello NGINX webserver that serves a simple page co... 76 [OK] privatebin/nginx-fpm-alpine PrivateBin running on an Nginx, php-fpm & Al... 60 [OK] nginx/nginx-ingress NGINX and NGINX Plus Ingress Controllers fo... 55 nginxinc/nginx-unprivileged Unprivileged NGINX Dockerfiles 54 staticfloat/nginx-certbot Opinionated setup for automatic TLS certs lo... 25 [OK] nginxproxy/nginx-proxy Automated Nginx reverse proxy for docker con... 24 nginx/nginx-prometheus-exporter NGINX Prometheus Exporter for NGINX and NGIN... 22 schmunk42/nginx-redirect A very simple container to redirect HTTP tra... 19 [OK] centos/nginx-112-centos7 Platform for running nginx 1.12 or building ... 15 centos/nginx-18-centos7 Platform for running nginx 1.8 or building n... 13 flashspys/nginx-static Super Lightweight Nginx Image 11 [OK] mailu/nginx Mailu nginx frontend 9 [OK] webdevops/nginx Nginx container 9 [OK] sophos/nginx-vts-exporter Simple server that scrapes Nginx vts stats a... 7 [OK] ansibleplaybookbundle/nginx-apb An APB to deploy NGINX 3 [OK] wodby/nginx Generic nginx 1 [OK] [root@VM-0-15-centos home]# docker pull nginx Using default tag: latest latest: Pulling from library/nginx b380bbd43752: Pull complete fca7e12d1754: Pull complete 745ab57616cb: Pull complete a4723e260b6f: Pull complete 1c84ebdff681: Pull complete 858292fd2e56: Pull complete Digest: sha256:644a70516a26004c97d0d85c7fe1d0c3a67ea8ab7ddf4aff193d9f301670cf36 Status: Downloaded newer image for nginx:latest docker.io/library/nginx:latest [root@VM-0-15-centos home]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE nginx latest 87a94228f133 3 weeks ago 133MB centos latest 5d0da3dc9764 7 weeks ago 231MB [root@VM-0-15-centos home]# docker run -d --name nginx01 -p 3344:80 nginx bd1dd8f51eb8efe2359ed1e21ede663f82fc22a630f83ef87a85f0b24aaa96e3 [root@VM-0-15-centos home]# docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES bd1dd8f51eb8 nginx "/docker-entrypoint...." 3 seconds ago Up 2 seconds 0.0.0.0:3344->80/tcp, :::3344->80/tcp nginx01 bad34ff90469 centos "/bin/bash" 33 minutes ago Up 16 minutes keen_einstein [root@VM-0-15-centos home]# curl localhost:3344 <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style> html { color-scheme: light dark; } body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style> </head> <body> <h1>Welcome to nginx!</h1> <p>If you see this page, the nginx web server is successfully installed and working. Further configuration is required.</p> <p>For online documentation and support please refer to <a href="http://nginx.org/">nginx.org</a>.<br/> Commercial support is available at <a href="http://nginx.com/">nginx.com</a>.</p> <p><em>Thank you for using nginx.</em></p> </body> </html> docker run -d --name nginx01 -p 3344:80 nginx#3344 is the host port number and 80 is the container port number
Install tomcat
docker run -it --rm tomcat:9.0 #Generally used for testing, delete after use docker run -d -p 3355:8080 --name tomca01 tomcat
Deploy es+kibana
#Because es consumes more memory, executing this command will crash docker run -d --name elasticsearch -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" elasticsearch:7.6.2 #Check the status of cpu, memory, etc docker stats #Limit memory docker run -d --name elasticsearch -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" -e ES_JAVA_OPTS="-Xms64m -Xmx512m" elasticsearch:7.6.2
Connect es using kibana
visualization
docker graphical interface management tool provides a background panel
docker run -d -p 8088:9000 --restart=always -v /var/run/docker.sock:/var/run/docker.sock --privileged=true portainer/portainer
Access test
42.193.51.130:8088
Commit image
docker commit docker commit -m="Description information submitted" -a="author" container id Target image name:TAG
test
#Start a tomcat docker run -it -p 8081:8080 tomcat #Copy webapps.dist to websapps cp -r webapps.dist/* webapps/ #Commit mirror [root@VM-0-15-centos ~]# docker commit -a="ghhgy" -m="add webapps app" e605cafee200 tomcat02:1.0 sha256:2a9f9f5e81867c0be15d11b88200ba76e8876f26506bf20c38e562283268b396 [root@VM-0-15-centos ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE tomcat02 1.0 2a9f9f5e8186 4 seconds ago 684MB tomcat latest b0e0b0a92cf9 2 weeks ago 680MB nginx latest 87a94228f133 3 weeks ago 133MB centos latest 5d0da3dc9764 7 weeks ago 231MB portainer/portainer latest 580c0e4e98b0 7 months ago 79.1MB elasticsearch 7.6.2 f29a1ee41030 19 months ago 791MB
Container data volume
If the data is in the container, the data will be lost after the container is deleted
Requirements: Data Persistence
Data can be stored locally
Directory mount
Container persistence and synchronization, and data can be shared between containers
docker run -v Native directory:Container directory docker run -it -v /home/ceshi:/home centos /bin/bash #It is similar to mounting the local directory to the container directory, and two-way binding
test
docker pull mysql:5.7 docker run -d -p 3310:3306 -v /home/mysql/conf:/etc/mysql/conf.d -v /home/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 --name mysql02 mysql:5.7
Named and anonymous mount
#Anonymous mount -v Path within container docker run -d -P --name nginx01 -v /etc/nginx nginx #View all volume s [root@VM-0-15-centos data]# docker volume ls DRIVER VOLUME NAME local a8af848d9eaef19d1f4250e67d4036f0dd6972d91d61d72370d363637f196cfb #Named mount [root@VM-0-15-centos data]# docker run -d -P --name nginx02 -v juming-nginx:/etc/nginx nginx 82c334c8dca81c12780d2b0698e27495dad4733b7fab15a44944d8ba6e892ff2 [root@VM-0-15-centos data]# docker volume ls DRIVER VOLUME NAME local a8af848d9eaef19d1f4250e67d4036f0dd6972d91d61d72370d363637f196cfb local juming-nginx #View path [root@VM-0-15-centos data]# docker volume inspect juming-nginx [ { "CreatedAt": "2021-11-08T16:59:02+08:00", "Driver": "local", "Labels": null, "Mountpoint": "/var/lib/docker/volumes/juming-nginx/_data", "Name": "juming-nginx", "Options": null, "Scope": "local" } ]
Most use named mount
# There are three kinds of Mount: anonymous mount, named mount and specified path mount -v In container path #Anonymous mount -v Volume name: path within container #Named mount -v /Host path: path within container #The docker volume ls attached to the specified path cannot be viewed
expand:
# Change read and write permissions through - v path in container: ro rw ro #readonly read only rw #readwrite readable and writable docker run -d -P --name nginx05 -v juming:/etc/nginx:ro nginx docker run -d -P --name nginx05 -v juming:/etc/nginx:rw nginx # As long as ro sees ro, it means that this path can only be operated through the host, and it cannot be operated inside the container!
Build the build file of docker image, command script
FROM centos VOLUME ["volume01","volume02"] CMD echo "----end----" CMD /bin/bash
--volumes-from list Mount volumes from the specified container(s) # Test, we passed the test just started docker run -d -p 3306:3306 -v /home/mysql/conf:/etc/mysql/conf.d -v /home/mysql/data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 --name mysql01 mysql:5.7 docker run -d -p 3307:3306 -e MYSQL_ROOT_PASSWORD=123456 --name mysql02 --volumes-from mysql01 mysql:5.7 # At this time, data synchronization between two containers can be realized!
For the transfer of configuration information between containers, the life cycle of data volume containers continues until no containers are used.
But once you persist to the local, the local data will not be deleted at this time!
DockerFile
1.DockerFile introduction
dockerfile is the file used to build the docker image! Command parameter script!
Construction steps:
1. Write a dockerfile file
2. docker build is called an image
3. docker run run image
4. docker push publishing image (DockerHub, Alibaba cloud warehouse)
However, many official images are basic packages without many functions. We usually build our own images!
Since the official can make images, so can we!
2.DockerFile construction process
Basics:
1. Each reserved keyword (instruction) must be uppercase
2. Perform top-down sequence
3. # indicates a comment
4. Each instruction will create and submit a new mirror layer!
Dockerfile is development oriented. If we want to publish projects and make images in the future, we need to write a dockerfile file, which is very simple
Single!
Docker image has gradually become the standard of enterprise delivery, which must be mastered!
DockerFile: build a file that defines all the steps and source code
DockerImages: build the generated image through DockerFile, and finally release and run the product.
Docker container: a container is an image that runs to provide services.
3.DockerFile instruction
# DockerFile common instructions FROM # Basic image, everything starts from here MAINTAINER # Who wrote the image, name + email RUN # Commands to run during image construction ADD # Step, tomcat image, this tomcat compressed package! Add content add same directory WORKDIR # Mirrored working directory VOLUME # Mounted directory EXPOSE # Reserved port configuration CMD # Specify the command to run when the container starts. Only the last one will take effect and can be replaced. ENTRYPOINT # Specify the command to run when the container starts, and you can append the command ONBUILD # When an inherited DockerFile is built, the ONBUILD instruction will be run to trigger the instruction. COPY # Similar to ADD, copy our files to the image ENV # Set environment variables during construction!
FROM scratch
Create centos
#1. Write dockerfile FROM centos MAINTAINER ghhgy<Your mailbox> ENV MYPATH /usr/local WORKDIR $MYPATH RUN yum -y install vim RUN yum -y install net-tools EXPOSE 80 CMD echo $MYPATH CMD echo "----end----" CMD /bin/bash #2. Build image docker build -f mydockerfile-centos -t mycentos:0.1 . #3. Run the test. The default working directory is changed to / usr/local. vim and ifconfig commands are supported #View mirror construction history [root@VM-0-15-centos dockerfile]# docker history 9956616c2ab2 IMAGE CREATED CREATED BY SIZE COMMENT 9956616c2ab2 5 minutes ago /bin/sh -c #(nop) CMD ["/bin/sh" "-c" "/bin... 0B b300db55d9c9 5 minutes ago /bin/sh -c #(nop) CMD ["/bin/sh" "-c" "echo... 0B e5c27ca58c9c 6 minutes ago /bin/sh -c #(nop) CMD ["/bin/sh" "-c" "echo... 0B b100a7b87ba1 6 minutes ago /bin/sh -c #(nop) EXPOSE 80 0B 680c65bf9616 6 minutes ago /bin/sh -c yum -y install net-tools 33.7MB 3d2b83702280 6 minutes ago /bin/sh -c yum -y install vim 75MB 80225a43f177 8 minutes ago /bin/sh -c #(nop) WORKDIR /usr/local 0B 7984a76b67ce 8 minutes ago /bin/sh -c #(nop) ENV MYPATH=/usr/local 0B a7c6e1014333 8 minutes ago /bin/sh -c #(nop) MAINTAINER ghhgy<super.pa... 0B 5d0da3dc9764 7 weeks ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0B <missing> 7 weeks ago /bin/sh -c #(nop) LABEL org.label-schema.sc... 0B <missing> 7 weeks ago /bin/sh -c #(nop) ADD file:805cb5e15fb6e0bb0... 231MB
Difference between CMD and ENTYPOINT
CMD # Specify the command to run when the container starts. Only the last one will take effect and can be replaced. ENTRYPOINT # Specify the command to run when the container starts, and you can append the command
Test CMD
[root@VM-0-15-centos dockerfile]# cat dockerfile-centos-cmd FROM centos CMD ["ls","-a"] [root@VM-0-15-centos dockerfile]# docker build -f dockerfile-centos-cmd -t cmdtest . [root@VM-0-15-centos dockerfile]# docker run cmdtest . .. .dockerenv bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var #Want to append a - l [root@VM-0-15-centos dockerfile]# docker run cmdtest -l docker: Error response from daemon: OCI runtime create failed: container_linux.go:380: starting container process caused: exec: "-l": executable file not found in $PATH: unknown. # cmd, - l replaces CMD["ls","-l"], - l is not a command, so an error is reported
Test ENTRYPOINT
[root@VM-0-15-centos dockerfile]# cat dockerfile-centos-entrypoint FROM centos ENTRYPOINT ["ls","-a"] [root@VM-0-15-centos dockerfile]# docker build -f dockerfile-centos-entrypoint -t entrypoint-test . [root@VM-0-15-centos dockerfile]# docker run entrypoint-test . .. .dockerenv bin dev etc home lib lib64 lost+found media mnt opt proc root run sbin srv sys tmp usr var [root@VM-0-15-centos dockerfile]# docker run entrypoint-test -l total 56 drwxr-xr-x 1 root root 4096 Nov 8 10:19 . drwxr-xr-x 1 root root 4096 Nov 8 10:19 .. -rwxr-xr-x 1 root root 0 Nov 8 10:19 .dockerenv lrwxrwxrwx 1 root root 7 Nov 3 2020 bin -> usr/bin drwxr-xr-x 5 root root 340 Nov 8 10:19 dev drwxr-xr-x 1 root root 4096 Nov 8 10:19 etc drwxr-xr-x 2 root root 4096 Nov 3 2020 home lrwxrwxrwx 1 root root 7 Nov 3 2020 lib -> usr/lib lrwxrwxrwx 1 root root 9 Nov 3 2020 lib64 -> usr/lib64 drwx------ 2 root root 4096 Sep 15 14:17 lost+found drwxr-xr-x 2 root root 4096 Nov 3 2020 media drwxr-xr-x 2 root root 4096 Nov 3 2020 mnt drwxr-xr-x 2 root root 4096 Nov 3 2020 opt dr-xr-xr-x 142 root root 0 Nov 8 10:19 proc dr-xr-x--- 2 root root 4096 Sep 15 14:17 root drwxr-xr-x 11 root root 4096 Sep 15 14:17 run lrwxrwxrwx 1 root root 8 Nov 3 2020 sbin -> usr/sbin drwxr-xr-x 2 root root 4096 Nov 3 2020 srv dr-xr-xr-x 13 root root 0 Nov 8 10:17 sys drwxrwxrwt 7 root root 4096 Sep 15 14:17 tmp drwxr-xr-x 12 root root 4096 Sep 15 14:17 usr drwxr-xr-x 20 root root 4096 Sep 15 14:17 var # In the case of ENTRYPOINT, - l splice after the ENTRYPOINT command
Many commands in Dockerfile are very similar. We need to understand their differences. Our best learning is to compare them and test them
Try the effect!
4. Actual combat: Tomcat image
1. Prepare mirror file
Prepare tomcat and jdk to the current directory and write README.
[root@VM-0-15-centos webapps]# cat /home/mytomcat/Dockerfile FROM centos MAINTAINER ghhgy<Your mailbox> COPY readme.txt /usr/local/readme.txt ADD jdk-8u211-linux-x64.tar.gz /usr/local/ ADD apache-tomcat-9.0.29.tar.gz /usr/local/ RUN yum -y install vim ENV MYPATH /usr/local WORKDIR $MYPATH ENV JAVA_HOME /usr/local/jdk1.8.0_211 ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar ENV CATALINA_HOME /usr/local/apache-tomcat-9.0.29 ENV CATALINA_BASH /usr/local/apache-tomcat-9.0.29 ENV PATH $PATH:$JAVA_HOME/bin:$CATALINA_HOME/lib:$CATALINA_HOME/bin EXPOSE 8080 CMD /usr/local/apache-tomcat-9.0.29/bin/startup.sh && tail -F /usr/local/apache-tomcat-9.0.29/logs/catalina.out
2. Build mirror
docker build -t diytomcat .
3. Run the mirror and mount the volume
docker run -d -p 8081:8080 -v /home/mytomcat/webapps/test:/usr/local/apache-tomcat-9.0.29/webapps/test -v /home/mytomcat/logs:/usr/local/apache-tomcat-9.0.29/logs diytomcat
4. Create a new WEB-INFO/web.xml and index.jsp in the local / home/mytomcat/webapps/test directory
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0" metadata-complete="true"> </web-app>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Rookie tutorial(runoob.com)</title> </head> <body> Hello World!<br/> <% out.println("Yours IP address " + request.getRemoteAddr()); %> </body> </html>
Enter the url of test to access
5. Publish your own image
1. Address https://hub.docker.com/
2. Make sure this account can log in
3. Login
[root@VM-0-15-centos test]# docker login --help Usage: docker login [OPTIONS] [SERVER] Log in to a Docker registry. If no server is specified, the default is defined by the daemon. Options: -p, --password string Password --password-stdin Take the password from stdin -u, --username string Username
#1. Login docker login -u ghhgy #2. Change the current dytomcat: latest tag to ghhgy/tomcat:1.0, otherwise you cannot push docker tag diytomcat:latest ghhgy/tomcat:1.0 #3. push image to dockerhub docker push ghhgy/tomcat:1.0