วิธีใช้ Docker การสร้าง Docker Container

บทความนี้จะช่วยสอนและอธิบายพื้นฐานของการเริ่มต้นใช้งาน Docker

Creating your first Docker container

Docker creates virtual containers. ระบบคอนเทนเนอร์ของ Docker นั้นมีประสิทธิภาพมากเพราะทำงานกับคอมมิต วิธีนี้ช่วยประหยัดพื้นที่ และช่วยให้คุณเห็นการเปลี่ยนแปลงในคอนเทนเนอร์ ตัวอย่างเช่น หากคุณติดตั้ง Apache ในคอนเทนเนอร์ คุณสามารถสร้างคอมมิตโดยใช้ชื่อ "Installed Apache" เพื่อให้คุณรู้ว่าเกิดอะไรขึ้น


สิ่งแรกที่เราจะทำคือดึงจากที่เก็บ สมมติว่าคุณต้องการติดตั้ง Ubuntu ในคอนเทนเนอร์ คุณสามารถดึง Ubuntu จากที่เก็บ

docker pull ubuntu


อาจใช้เวลาสักครู่ หลังจากดาวน์โหลดทุกอย่างแล้ว คุณสามารถสร้างคอนเทนเนอร์ด้วยระบบปฏิบัติการนี้

docker run -i -t ubuntu /bin/bash


หรือกับ Debian เช่น

docker run -i -t debian /bin/bash

หากไม่พบ OS (ยังไม่ได้ดึง) ระบบจะดึงจาก Docker Hub โดยอัตโนมัติ


ตอนนี้คุณมี container คุณกำลังเรียกใช้ bash ในคอนเทนเนอร์ที่บางลงซึ่งจัดการโดย Docker ลองใช้คำสั่งทั่วไปของ Linux เพื่อให้เข้าใจถึงสภาพแวดล้อม


เมื่อคุณพิมพ์ exit เพื่อออกจากคอนเทนเนอร์และกลับสู่ OS หลัก การเปลี่ยนแปลงทั้งหมดของคุณจะหายไป ในการบันทึกการเปลี่ยนแปลงไปยังคอนเทนเนอร์ เราใช้การคอมมิต



Commits

เมื่อคุณสร้างคอนเทนเนอร์ Docker ชื่อโฮสต์ของคอนเทนเนอร์จะถูกสร้างขึ้นโดยอัตโนมัติ ตัวอย่างเช่น เมื่อฉันสร้างคอนเทนเนอร์ Ubuntu ใหม่ ฉันอาจได้รับชื่อโฮสต์ f7943e42aff0 นี่คือชื่อที่ Docker ตั้งให้กับคอนเทนเนอร์ของคุณ


ติดตั้งสิ่งที่คุณต้องการและตรวจสอบให้แน่ใจว่าทุกอย่างใช้งานได้ จากนั้นออกจากคอนเทนเนอร์ Docker ของคุณ

exit


We now need to commit; otherwise, all of your changes will be lost.

docker commit -a "William E." -m "Installed Apache" f7943e42aff0 apachesnapshot


The -a switch can be used to properly determine who authored that commit (who made the changes in the container). -m is the commit message. The f7943e42aff0 is the hostname of my container. In your case it will differ, as Docker generates them randomly. apachesnapshot is the name of your image.

You can view a list with all of the images on your local machine. The newest ones are at the top.

docker images


In order to start your Docker container with the changes, run:

docker run -t -i apachesnapshot /bin/bash


Using Dockerfiles

Dockerfiles can be used to make images with applications already installed. This makes it convenient to start a container without having to run a specific command. For example, if we want to create an image with the file ~/file.txt already created, we would use the following Dockerfile:

FROM ubuntu:14.04
MAINTAINER William E. <william@localhost>
RUN touch ~/file.txt


In order to create a Docker container with this Dockerfile, make a folder for your Dockerfile on your local machine (I used ~/files). Put the contents of your Dockerfile in a file called Dockerfile. You can now create an image with it by running:

docker build -t="test" .


This creates a Docker image from your Dockerfile script. You can now run your container. test is the same value as test in the docker build command.

docker run -t -i test /bin/bash

When the bash shell opens, you'll see that the ~/file.txt has already been created.

This is just a taste of the powerful environments that you can create using Docker. The Docker official manual goes into much further depth on these topics. At this point, you should be able to experiment running existing containers and begin to start imaging your own.

0
465