4th. Using Docker images and warehouses
Review:
Review how to use Docker run to create the most basic container
$sudo Docker run-i-T--name another_container_mum Ubuntu/bin/bash
[Email protected]:/#
This command launches a new container called Another_container_mum, which is based on the Ubuntu mirror
Like and will start the bash Shell
---------------------------------------------------------------------------------------------
4.1 What is a Docker image
4.2 List Mirrors
You can see that I have three mirrors here.
Where Ubuntu is my morning download of Ubuntu base image, Paulcos11/docker-tutorial is downloaded in addition
A user-uploaded image. But I do not know how the time CREATED this column is not allowed.
The image is downloaded from the repository. The image is saved in the warehouse. And the warehouse exists in the registry. The default registry is made by the Do
Cker Company operates a public Registry service, which is Dcoker Hub.
Each mirror library can hold many images, for example, we look at other images in the Ubuntu repository,
But using Docker images to see all of the Ubuntu Docker images
sudo docker images
You can use Docker pull to download a base image
[#12 #[email protected] ~] $sudo Docker pull Ubuntu
There are two types of warehouses in Docker Hun: User warehouses and top-level warehouses. Users ' warehouses are mirrored by Docker users
Created, and the top-level repositories are managed by people inside Docker. The user repository is named by the user name and the warehouse name two
components, such as: paulcos11/docker-tutorial, user name: PAULCOS11, warehouse name: Docker-tutor
Ial, its relative top-level warehouse contains only the warehouse name parts, such as Ubuntu,fedora. Top-level warehouses are made up of Docker public
And is managed by selected vendors who can provide a high-quality base image.
4.3 Pull image
Use Docker images to view the mirrors on the local Docker host. If you want to be able to see only in the mirror list
To the content of an image, such as Fedora, by following the Docker images command with the specified image name
Now, for example:
Using Docker pull to draw a mirror
[#15 #[email protected] ~] $sudo Docker pull Fedora
Using default Tag:latest
Latest:pulling from Library/fedora
6888fc827a3f:pull Complete
9bdb5101e5fc:downloading [===================>] 28.63 mb/74.33 MB
4.4 Finding a mirror
My local mirror image is:
[#1 #[email protected] ~] $sudo Docker Images
[sudo] password for cloudsoar:
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
Paulcos11/docker-tutorial latest e37931352714 8 days ago 587.8 MB
Ubuntu latest 8ed581e3fa7a One days ago 188 MB
The fedora image is still not downloaded at this time. No hurry. Let's practice the other things first, in paulcos11/do
Use the Docker Run command in the cker-tutorial image to create a container from docker-tutorial.
[#2 #[email protected] ~] $sudo Docker run-i-T Paulcos11/docker-tutorial/bin/bash
[Email protected]:/#
As you can see, a new container has been started from the paulcos11/docker-tutorial image.
4.5 Building the image
There are two ways to build the Docker image:
Use the Docker commit command.
Use the Docker build command and the Dockerfile file.
4.5.1 Creating a Docker Hub account
Create your own account in hub.docker.com, after registration, by receiving confirmation email activation, the following can be measured
Try the account you just registered to work. To log in to the Docker hub, you can use the Docker login command. Such as
Here to see that I was registered successfully, the user name can only be a combination of letters or arrays. Use Docker login below to verify
My account:
[#4 #[email protected] ~] $sudo Docker Login
Username:zhiyewang
Password:
Email: [Email protected]
Warning:login Credentials Saved In/home/cloudsoar/.docker/config.json
Login succeeded
You can see that my account login was successful.
4.5.2 creating a mirror using the Docker commit command
Here I create a new image based on the Ubuntu image that I downloaded earlier.
First, I start a container in this base image
[#5 #[email protected] ~] $sudo Docker run-i-T Ubuntu/bin/bash
[Email protected]:/#
Install Apache as a Web server in a container to run. So every time you use Apache, you don't have to
Re-install Apache.
[Email protected]:/# apt-get-y Install apache2
Reading Package Lists ... Done
Building Dependency Tree
[Email protected]:/# apt-get-y Install vim
I like to use Vim, and install a vim at the same time, wow ka ka.
In order to complete this work, you need to exit exit and then execute the Docker commit command.
It is important to note that the Docker commit commits a difference between the mirror that created the container and the current state of the container
The part that makes the update very lightweight. Here you can see the results I created.
If you are running a container like a mirror you just created, you can use the Docker Run command
[#16 #[email protected] ~] $sudo Docker run-t-I Zhiyewang/apache2/bin/bash
[Email protected]:/#
4.5.3 using Dockerfile to build images
In fact, all the information is not recommended to use the Docker commit method to build the image, but instead use the Dockerfile definition
File and the Docker build command to build the image.
Our first Dockerfile.
Now that we create a directory and create the initial Dockerfile inside, we will create a simple Web service that contains
The Docker image of the server.
Here we create a directory named Static_web to hold the Dockerfile, this directory is our build ring
(build environment), Docker calls this environment a context or build context (build
context). Docker uploads the build context and files and directories in that context to the doc when the mirror is built
Ker daemon process. This allows the Docker daemon to directly access any code you want to store in the image.
Here is an example of a Dockerfile that uses Dockerfile to build a Docker image that can serve as a WEB server.
1 # version:0.0.1
2 from ubuntu:14.04
3 maintainer Zhiyewang "[email protected]"
4 RUN apt-get Update
5 RUN apt-get install-y nginx
6 RUN Echo ' Hi, I am in your container ' \
7 >/usr/share/nginx/html/index.html
8 EXPOSE 80
Command explanation:
The Dockerfile consists of a series of instructions and parameters. Each instruction is capitalized and needs to be followed by a parameter. Docker
File is executed from top to bottom in order.
Dockerfile supports annotations, all of which begin with a well sign.
The From ubuntu:14.04 specifies Ubuntu 14.04 as the base image for each
Executes an instruction to make changes to the container. automatically specifies a docker commit-like operation and submits a new image
Layer and proceed to the next instruction.
The maintainer instruction tells Docker who the author of the image is, and the author's e-mail address.
Next we execute the three run instructions, which run the specified command in the current image. We pass the Run
The APT warehouse was updated, the Nginx package was installed, and then the/usr/share/nginx/html/index.html file was created,
By default, run execution is executed in the shell using/bin/sh-c. If you don't support the shell or you don't want to shell
can use the exec format of the run
RUN ["Apt-get", "Install", "Y", "Nginx"]
The next EXPOSE instruction tells Docker that the application within the container will use Docker's specified port. But
Docker does not automatically open this port, but instead needs to use Docker run to specify which side to open when running the container
Mouth.
4.5.4 building a new image based on Dockerfile
Run Dockerfile
[#34 #[email protected] ~] $CD static_web/
[#35 #[email protected] ~/static_web] $sudo docker build-t= "Zhiyewang/static_web".
Sending build context to Docker daemon 2.048 KB
Step 1:from ubuntu:14.04
14.04:pulling from Library/ubuntu
Digest:sha256:45b23dee08af5e43a7fea6c4cf9c25ccf269ee113168c19722f87876677c5cb2
status:downloaded newer image for ubuntu:14.04
---> 8ed581e3fa7a
Step 2:maintainer Zhiyewang "[Email protected]"
---> Running in 7806118624b7
---> c5aed3a8ff95
Removing intermediate container 7806118624b7
...
Processing triggers for sgml-base (1.26+NMU4UBUNTU1) ...
---> ddc8935b098a
Removing intermediate container c81405d28e41
Step 5:run Echo ' Hi, I am in your container ' >/usr/share/nginx/html/index.html
---> Running in f0049e284208
---> 6a7a53f6e78a
Removing intermediate container f0049e284208
Step 6:expose 80
---> Running in 2a0714253002
---> e97eb7ef0136
Removing intermediate container 2a0714253002
Successfully built e97eb7ef0136
This uses the-t parameter to set the warehouse and name for the new image. The warehouse is Zhiyewang, the name is Static_web, can also be
Add a label to the image as part of the build process by "Mirror Name: Label"
sudo docker build-t= "Zhiyewang/static_web:v1".
If no label is customized, Docker automatically sets a latest label for the image.
The above command last. Tell us to go to the current path to find the Dockerfile file. You can also specify a source for a Git repository
Address to specify the location of the Dockerfile. For example
sudo docker build-t= "Zhiyewang/static_web:v1" \
[Email Protected]:zhiyewang/docker-static_web
This assumes that the Dockerfile file exists in the directory of the Git repository. I have not registered a Git account to do it.
Viewing the Docker build process found that the build context has been uploaded to the Docker daemon:
Sending build context to Docker daemon 2.048 KB
After that, you can see that each of the Dockerfile is executed sequentially, and the final result of the build process returns a new
The ID of the image, which is e97eb7ef0136, and Docker submits the execution results for each step.
What happens when the 4.5.4 command fails?
For example, we write the 4th step of the package name Nginx Ngin
[#41 #[email protected] ~/static_web] $sudo docker build-t= "Zhiyewang/static_web".
[sudo] password for cloudsoar:
Sending build context to Docker daemon 2.048 KB
Step 1:from ubuntu:14.04
---> 8ed581e3fa7a
Step 2:maintainer Zhiyewang "[Email protected]"
---> Using cache
---> c5aed3a8ff95
Step 3:run apt-get Update
---> Using cache
---> 40FA5CD1C3D2
Step 4:run apt-get install-y ngin
---> Running in 86e3dbaadf20
Reading Package Lists ...
Building Dependency Tree ...
Reading state information ...
E:unable to locate package Ngin
The command '/bin/sh-c apt-get install-y ngin ' returned a Non-zero code:100
Found to be wrong. Let's debug the reason for the failure. With Docker run, based on this build so far
The final step to success is to create a container whose ID is 40fa5cd1c3d2, with the following code:
[#42 #[email protected] ~/static_web] $sudo Docker run-t-I 40fa5cd1c3d2/bin/bash
[Email protected]:/#
Then we run the 4th step in this container:
[Email protected]:/# apt-get install-y Ngin
Reading Package Lists ... Done
Building Dependency Tree
Reading state information ... Done
E:unable to locate package Ngin
The package name was found to be incorrect.
We can run Apt-get install-y nginx again in this container, this time enter the correct package name to locate the problem
, if the problem is resolved, you can exit the container, modify the Dockerfile file with the correct package name, and then
Build.
This article to here. Next, continue to learn dockerfile and build the cache.
Fourth. Using Docker images and warehouses