Docker
Last updated
Was this helpful?
Last updated
Was this helpful?
Provide light-weight virtual environment with suitable version for program to execute in order to run the program smoothly and prevent conflict due to version issue
Registry: the link that used to get docker image ,which is similar with git repository
Image: a template of docker for creating container
Container: a light-weight virtual environment that run the instance of docker image
Initially, we need one physical server for one application, which is expensive and cannot make good use of all of the memory and CPU
After virtualization, several virtual machines is isolated from host operation system which contains their own guest operation system and share CPU and memory from the computer
Therefore, different operation system can be existed in a server to run multiple applications
Docker containers which creates a virtual environment can run at the same time by using the same host operation system
Therefore, the boot up time will be shorter and becomes light-weight compared with virtual machine
The image registry provides the portability, so that the image is easily to be reused across different machines
Pull the Image from registry
Create new instance of container based on docker image
Start the instance
Execute the instance
Syntax
Example
If the version is not defined, the default version will be latest
Result
Syntax
-d means run in background
-p defines the port run in local
-v defines the location of volume to backup the data in local, so that the data can be shared between different containers
Example
If the name is not defined, the name will be generated randomly
Result
From the list, we can see that there is 2 instances that we created with their name and container id
Syntax
Example
Result
Syntax
Example
Result
Syntax
Example
Syntax
Example
Result
Commit the container
Check whether new image is created or not and its image id
Edit the name of new image
Register an account and create new repository on dockerHub
Login
Change the name
Example
Push to registry
Result
Login to gcp
Change the name of image and push google cloud container registry
Initially, we need to execute the container and then generate new image, which is inconvenient.
Instead, we can write a script on docker file in order to automatize the process
It is a material of making a template (docker image)
FROM : from the original source of image
RUN: execute the command
COPY: copy file to specific directory
WORKDIR: cd into specific directory
CMD: execute the program on container, such as: node index.js
Edit Dockerfile
Create New Image by executing Dockerfile
Create + Start + Execute the container , and then see the result
we can see that hello.txt is here
ENTRYPOINT
should be defined when using the container as an executable.
CMD
should be used as a way of defining default arguments for an ENTRYPOINT
command or for executing an ad-hoc command in a container.
CMD
will be overridden when running the container with alternative arguments.
ENTRYPOINT can be changed using the --entrypoint
flag, but this should rarely be necessary for container images being used in the way they were intended.
No ENTRYPOINT
ENTRYPOINT exec_entry p1_entry
ENTRYPOINT ["exec_entry", "p1_entry"]
No CMD
error, not allowed
/bin/sh -c exec_entry p1_entry
exec_entry p1_entry
CMD ["exec_cmd", "p1_cmd"]
exec_cmd p1_cmd
/bin/sh -c exec_entry p1_entry
exec_entry p1_entry exec_cmd p1_cmd
CMD exec_cmd p1_cmd
/bin/sh -c exec_cmd p1_cmd
/bin/sh -c exec_entry p1_entry
exec_entry p1_entry /bin/sh -c exec_cmd p1_cmd
In a big scale, we may need to execute several containers, it is time-wasting to create and start them one by one.
By writing a script docker-compose.yml, the process can be executed automatically
Writing docker-compose.yml
Execute the command
Result
2 instances of container with 2 different images are created and started
When creating volume, the folder will be created on the machine to host the data
Attach the volume to connect with path declared on the containerized environment
The same volume is shared among the above commands, so the result
Create a docker file with meaningless entry point, so that we can execute the container to debug