– There are many ways to create an image, and users can obtain an existing image from the Docker Hub and update it, or create one with a local file system. First, create the image
There are many ways to create an image, and users can obtain an existing image from the Docker Hub and update it, or create one with a local file system. Second, modify the existing image 1. Start the container with the downloaded image first.
$ docker run-t-I training/sinatra/bin/bash
root@0b2616b0e5a8:/#
Note: Remember the ID of the container and use it later. 2. Add JSON and gem two apps to the container.
root@0b2616b0e5a8:/# Gem Install JSON
when this is over, we use Exit to exit, and now our container has been changed by us, using the Docker commit command to submit the updated copy.
$ sudo docker commit-m "Added json gem"-a "Docker Newbee" 0b2616b0e5a8 ouruser/sinatra:v2
4f177bd27a9ff0f6dc2a830403 925b5360bfe0b93d476f7fc3231110e7f71b1c
where-M to specify the description of the submission, as we use the version Control tool;-A can specify updated user information, followed by the ID of the container used to create the mirror, and finally specify the warehouse name and tag information for the target image. The ID information for this image is returned when the creation is successful.
use Docker images to view the newly created image.
$ sudo docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
Training/sinatra Latest 5bc342fa0b91 hours ago 446.7 MB
Ouruser/sinatra v2 3c59e02ddd1a 10 Hours ago 446.7 MB
ouruser/sinatra latest 5db5f8471261 ten hours ago 446.7 MB
after that, you can use the new mirror to start the container
$ sudo docker run-t-i ouruser/sinatra:v2/bin/bash
root@78e82f680994:/#
third, using Dockerfile to create the image
Using Docker commit to extend an image is simple, but not easy to share in one team. We can use the Docker build to create a new image. To do this, you first need to create a Dockerfile that contains instructions on how to create the mirror. Create a new directory and a Dockerfile
$ mkdir Sinatra
$ cd Sinatra
$ Touch Dockerfile
each instruction in the Dockerfile creates a layer of mirroring, for example:
# This was a comment from
ubuntu:14.04
maintainer Docker newbee <newbee@docker.com>
RUN apt-get-qq UPDA Te
run apt-get-qqy install ruby Ruby-dev
run gem install Sinatra
Dockerfile The basic syntax is
Use # to annotate
the From directive tells Docker which mirror to use as the basis
and then the maintainer's message.
the commands at the start of run will run in the creation, such as installing a package, where you can use Apt-get to install some software
You can use the Docker build to generate the image after writing the Dockerfile .
$ sudo docker build-t= "Ouruser/sinatra:v2". Uploading context 2.56 KB uploading context Step 0:from ubuntu:14.04---> 99ec81b80c55 step 1:maintainer Newbee & Lt;newbee@docker.com>---> Running in 7c5664a8a0c1---> 2fa8ca4e2a13 removing Intermediate container 7c5664a8a0 C1 Step 2:run APT-GET-QQ Update---> Running in b07cc3fb4256---> 50d21070ec0c removing Intermediate container b07cc3fb4256 Step 3:run apt-get-qqy install ruby Ruby-dev---> Running in a5b038dd127e Selecting previously Unselec
Ted Package Libasan0:amd64. (Reading database ...
11518 files and directories currently installed.)
Preparing to unpack .../libasan0_4.8.2-19ubuntu1_amd64.deb ...
Setting up Ruby (1:1.9.3.4) ...
Setting up ruby1.9.1 (1.9.3.484-2UBUNTU1) ... Processing triggers for Libc-bin (2.19-0UBUNTU6) ...---> 2acb20f17878 removing intermediate container a5b038dd127e St
EP 4:run gem install Sinatra---> Running in 5e9d0065c1f7 ... Successfully installed Rack-protection-1.5.3 successfully installed sinatra-1.4.5 4 gems installed---> 324104CDE6AD removing Intermediate container 5e 9D0065C1F7 successfully built 324104CDE6AD
where the-t flag is added to the tag to specify the user information for the new mirror. "." is the path (current directory) where the Dockerfile is located, or it can be replaced by a specific Dockerfile path.
You can see that the build process is performing an action. The first thing it needs to do is upload this Dockerfile content, because all the operations are based on Dockerfile. Then, the instructions in the dockfile are executed by one line. Each step creates a new container, executes the instructions in the container, and submits the changes (as in the case of the Docker commit described earlier). When all instructions have been executed, the final mirror ID is returned. All the containers produced by the intermediate steps are deleted and cleaned up.
Note that an image cannot exceed 127 levels
In addition, you can use the ADD command to copy the local file to the mirror, open the port externally with the EXPOSE command, and use the CMD command to describe the program that is running after the container starts. For example
# put my local Web site in myApp folder to/var/www
ADD myapp/var/www
# expose httpd port
expose
command to run
CMD ["/usr/sbin/apachectl", "-D", "FOREGROUND"]
You can now start a container with the newly created image.
$ sudo docker run-t-i ouruser/sinatra:v2/bin/bash
root@8196968dac35:/#
You can also use the Docker tag command to modify the label of the Mirror.
$ sudo docker tag 5db5f8471261 ouruser/sinatra:devel
$ sudo docker images Ouruser/sinatra
REPOSITORY Tag IMAGE ID CREATED VIRTUAL SIZE
ouruser/sinatra latest 5db5f8471261 hours ago 446.7 MB
Ouruser/sinatra devel 5db5f8471261 hours ago 446.7 MB Ouruser/sinatra v2 5db5f8471261 hours ago 446.7 MB
Iv. importing from a local file system
To import a mirror from the local file system, you can create it using the template for OpenVZ (the Pioneer Technology for container virtualization): The OpenVZ template download address is templates. For example, a ubuntu-14.04 image is downloaded first and then imported using the following command:
sudo cat ubuntu-14.04-x86_64-minimal.tar.gz |docker import-ubuntu:14.04
then view the newly imported image.
Docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
ubuntu 14.04 05ac7c0b9383 seconds ago 215.5 MB
v. Upload Image
The Docker Push command allows the user to upload the image they have created to the repository for sharing. For example, after the user has completed registration on the Docker Hub, they can push their own image into the repository.
$ sudo docker push Ouruser/sinatra the
push refers to a repository [Ouruser/sinatra] (len:1)
sending image list
pushing Repository Ouruser/sinatra (3 tags)
Loaded from: http://edu.cnzz.cn/201509/96952310.shtml