How to Use Docker: Creating Your First Docker Container

轉自: https://www.vultr.com/docs/how-to-use-docker-creating-your-first-docker-container

This tutorial explains the basics of getting started with Docker. I assume that you already have Docker installed. Steps in this tutorial will work on any Linux distribution that is compatible with Docker (CentOS, Ubuntu, etc).
Creating your first Docker container
Docker creates virtual containers. Docker's container system is very efficient because it works with commits. This saves space, and allows you to see changes to the container. For example, if you install Apache in a container, you can create a commit with the name "Installed Apache" so you know exactly what happened.
The first thing we'll do is pull from a repository. Say that you want to install Ubuntu in a container, you can pull Ubuntu from the repository:
docker pull ubuntu

Be patient, as this can take a while. After everything has been downloaded, you can create a container with this OS:
docker run -i -t ubuntu /bin/bash

Or with Debian, for example:
docker run -i -t debian /bin/bash

If it can't find the OS (not pulled yet) it will automatically pull it from Docker Hub.
Effectively, you now have a container! You are running bash in the slimmed down container that is managed by Docker. Try running some common Linux commands to get a feel for the environment.
When you type exit
to exit the container and return to your main OS, all of your changes will be gone. To save changes to a container, we use commits.
Commits
When you create a Docker container, its hostname is automatically generated. For example, when I create a new Ubuntu container, I might get the hostnamef7943e42aff0
. This is the name that Docker has given to your container.
Install what you want on it, and make sure everything works. Then exit your Docker container:
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. Thef7943e42aff0
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.04MAINTAINER William E. william@localhostRUN 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.

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

  • Introduction Docker is an application that simplifies the...
    ???木?燚??閱讀 696評論 0 1
  • 前天是2.14日,也就是西方的情人節(jié)。對于有另一半的人來說,這或許是一個值得紀念且高興的日子。但我卻收到好友小Z分...
    橘貓暖暖閱讀 502評論 0 1
  • 看過劉同的這本書,當然要去支持一下這部電影。 電影和前幾部青春的電影差不多,都有些懷舊的感覺,男女主角第一眼都不覺...
    請叫我一聲A小Q閱讀 255評論 0 0
  • 這幾天都在接觸一些小青年 他們的人生經(jīng)歷是那么的相同 都是小的時候學習還不錯 上了初中也還行 到了高中就一落千丈 ...
    卓越自我87閱讀 333評論 0 1
  • 真正意義上的喜歡和愛就一次,那是在高考復習班的時候,自己沒想到會被擦肩而過的那個身影記念很久,很久... 小心寫好...
    早一閱讀 214評論 0 0

友情鏈接更多精彩內容