May 22, 2021 Docker From entry to practice
There are many ways to create a mirror, and users can get an existing image from Docker Hub and update it, or they can create one with the local file system.
Start the container with the downloaded image first.
$ sudo docker run -t -i training/sinatra /bin/bash
root@0b2616b0e5a8:/#
Note: Remember the ID of the container and you'll use it later.
Add json and gem apps to the container.
root@0b2616b0e5a8:/# gem install json
When it's over, we exit with exit, and now our container has been
docker commit
command to submit the updated copy.
$ sudo docker commit -m "Added json gem" -a "Docker Newbee" 0b2616b0e5a8 ouruser/sinatra:v2
4f177bd27a9ff0f6dc2a830403925b5360bfe0b93d476f7fc3231110e7f71b1c
Where
-m
the submitted description information, just like the version control tools we use,
-a
can specify the updated user information, followed by the ID of the container used to create the mirror, and finally the warehouse name and tag information for the target mirror.
The image's ID information is returned when it is created successfully.
Use
docker images
view newly created images.
$ sudo docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
training/sinatra latest 5bc342fa0b91 10 hours ago 446.7 MB
ouruser/sinatra v2 3c59e02ddd1a 10 hours ago 446.7 MB
ouruser/sinatra latest 5db5f8471261 10 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:/#
Using
docker commit
to extend a mirror is simple, but not convenient for sharing in a team. W
e can
docker build
to create a new image.
To do this, you first need to create a Dockerfile that contains instructions on how to create a mirror.
Create a new directory and a Dockerfile
$ mkdir sinatra
$ cd sinatra
$ touch Dockerfile
Each instruction in Dockerfile creates a layer of mirroring, such as:
# This is a comment
FROM ubuntu:14.04
MAINTAINER Docker Newbee <newbee@docker.com>
RUN apt-get -qq update
RUN apt-get -qqy install ruby ruby-dev
RUN gem install sinatra
Dockerfile's basic syntax is
#
to comment
FROM
directive tells Docker which mirror to use as the basis
RUN
instructions at the beginning of run run in creation, such as installing a package, where some software is installed using apt-get
Once you've written Dockerfile, you
docker build
to generate a mirror.
$ 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 <[email protected]>
---> Running in 7c5664a8a0c1
---> 2fa8ca4e2a13
Removing intermediate container 7c5664a8a0c1
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 unselected 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
Step 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 5e9d0065c1f7
Successfully built 324104cde6ad
Where
-t
is added to specify the user information for the new image.
"." is the path in which Dockerfile is located (the current directory), or it can be replaced with a specific Dockerfile path.
You can see that the build process is performing an action. T
he first thing it does is upload this Dockerfile content, because all the operations are done according to Dockerfile. T
he instructions in Dockfile are then executed one by one. E
ach step creates a new container in which instructions are executed and modifications are committed (just like
docker commit
earlier). W
hen all instructions have been executed, the final mirror id is returned.
All intermediate steps result in containers being deleted and cleaned.
Note that a mirror cannot exceed 127 layers
In addition,
ADD
files to the mirror with the ADD command, open ports externally with the
EXPOSE
command,
CMD
run after the container starts, and so on.
For example
# put my local web site in myApp folder to /var/www
ADD myApp /var/www
# expose httpd port
EXPOSE 80
# the command to run
CMD ["/usr/sbin/apachectl", "-D", "FOREGROUND"]
You can now start a container with a newly created image.
$ sudo docker run -t -i ouruser/sinatra:v2 /bin/bash
root@8196968dac35:/#
You can also
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 11 hours ago 446.7 MB
ouruser/sinatra devel 5db5f8471261 11 hours ago 446.7 MB
ouruser/sinatra v2 5db5f8471261 11 hours ago 446.7 MB
Note: For more usage, please refer to the Dockerfile section.
To import a mirror from your local file system, you can create it using a template from openvz, a pioneer in container virtualization: openvz's template download address is templates.
For example, download a ubuntu-14.04 image first, and then import using the following command:
sudo cat ubuntu-14.04-x86_64-minimal.tar.gz |docker import - ubuntu:14.04
Then look at the newly imported mirror.
docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
ubuntu 14.04 05ac7c0b9383 17 seconds ago 215.5 MB
The
docker push
users to upload images they create to the repository for sharing.
For example, after a user completes registration on Docker Hub, they can push their own mirror 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)