How to build your own ubuntu image with docker?
一. Build a ubuntu image and install sshd
1. Pull ubuntu
docker pull ubuntu:14.04
2. Create Dockerfile
FROM ubuntu:14.04 #update RUN apt-get update #install gcc RUN apt-get install -y gcc #install vim RUN apt-get install -y vim #install sshd RUN apt-get install -y openssh-server RUN mkdir /var/run/sshd #create user RUN useradd admin RUN echo 'admin:admin' |chpasswd #start sshd EXPOSE 22 CMD ["/usr/sbin/sshd", "-D"]
3. Build image
docker build -t ubuntu-sshd-admin .
Build then check image.
$ docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu-sshd-admin latest 82a9040e58aa 14 minutes ago 380MB
4. Run
docker run -d -p 222:22 ubuntu-sshd-admin
docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
d87645ff3fa2 ubuntu-sshd-admin "/usr/sbin/sshd -D" 7 minutes ago Up 7 minutes 0.0.0.0:222->22/tcp unruffled_mirzakhani
5. Client test
ssh
Use admin:admin to login, then you will find sshd is already started.
$ ps -elf | grep sshd
4 S root 1 0 0 80 0 - 15346 - 04:05 ? 00:00:00 /usr/sbin/sshd -D
4 S root 22 1 0 80 0 - 23138 - 04:13 ? 00:00:00 sshd: admin [priv]
5 S admin 33 22 0 80 0 - 23138 - 04:13 ? 00:00:00 sshd: [email protected]/0
0 S admin 37 34 0 80 0 - 2219 - 04:14 pts/0 00:00:00 grep sshd
$
二. Install more software
1. Create new Dockerfile
If you need more software in your own system, you can create more Dockerfile and build update version of image.
For example, if you want to install vim, you can write a new Dockerfile.vim
FROM ubuntu-sshd-admin
#install vim
RUN apt-get install -y vim
And now we have two dockerfiles.
2. Build a new version
docker build -t ubuntu-sshd-admin:0.1 . -f Dockerfile.vim
By exec the cmd above, we can build a new version of ubuntu-ssh-admin, we can check by this:
$ docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
ubuntu-sshd-admin 0.1 dbea46204d20 8 minutes ago 414MB
ubuntu-sshd-admin latest 82a9040e58aa 3 hours ago 380MB
Then we can restart the container and see vim is already installed in the ubuntu.
docker run -d -p 222:22 ubuntu-sshd-admin:0.1
三. Create a volume for ubuntu
1. create a volume
wangyachangdeMacBook-Pro:docker wangyachang$ docker volume create ubuntu
ubuntu
wangyachangdeMacBook-Pro:docker wangyachang$ docker volume inspect ubuntu
[
{
"CreatedAt": "2018-06-21T07:31:06Z",
"Driver": "local",
"Labels": {},
"Mountpoint": "/var/lib/docker/volumes/ubuntu/_data",
"Name": "ubuntu",
"Options": {},
"Scope": "local"
}
]
2. start ubuntu
Then we can start the ubuntu image by mount /world to the volume created before.
wangyachangdeMacBook-Pro:ubuntu wangyachang$ docker run -d -p 222:22 --mount type=volume,source=ubuntu,target=/world ubuntu-sshd-admin:0.2
da637a55cb71589f58397a446f00bd5703e7d219e6f12250062c7b13d1dbba48
We can can creat a file in the /world directory.
[email protected]:/# touch /world/data.txt
[email protected]:/# ls /world/data.txt
/world/data.txt
因為作者的docker是安裝在mac上的,mac上的docker是在一個虛擬機器中執行的,所以這裡訪問下面這個目錄是不存在的。
/var/lib/docker/volumes/ubuntu/_data
/Applications/Docker.app/Contents/MacOS/com.docker.driver.amd64-linux
四. 掛載一個主機目錄作為資料卷
使用 -v 標記也可以指定掛載一個本地的已有目錄到容器中去作為資料卷:
docker run -d -p 222:22 -v /Users/wangyachang/Documents/docker/ubuntu/ubuntu_data:/world_data ubuntu-sshd-admin:0.2
使用上面的命令,會把本機ubuntu_data目錄掛載到容器的world_data,實現了所有的資料同步。
參考文獻:
相關推薦
How to build your own ubuntu image with docker?
一. Build a ubuntu image and install sshd 1. Pull ubuntu docker pull ubuntu:14.04 2. Create Dockerfile FROM ubuntu:14.04 #updat
How to build your own Neural Network from scratch in Python
How to build your own Neural Network from scratch in PythonA beginner’s guide to understanding the inner workings of Deep LearningMotivation: As part of my
How To Build Your Own Lambda Deployer
How To Build Your Own Lambda DeployerIn my last article, we discussed replacing monolithic APIs with more nimble Serverless technology. We had a lot of que
How to build your own Twitter Sentiment Analysis Tool
In this article we will show how yo
How to build your own Facebook Sentiment Analysis Tool
In this article we will discuss how
How to write your own Virtual DOM
There are two things you need to know to build your own Virtual DOM. You do not even need to dive into React’s source. Or into source code of any other Vir
How to build your React Native team.
3. Managing your projectLeverage your existing Javascript and Web developers.Don’t make your web developers deal with Android build issues.Once you have yo
How to write your own Spring Boot starters
How to write your own Spring Boot startersIntroductionIn my first article, I would like to tell you how to deal with Spring Boot Configuration and Spring B
How to distribute your own Android library through jCenter and Maven Central from Android Studio
In Android Studio, if you wish to include any library to your application. You could just simply add a following line of dependency in mo
How to make your own Python dev
In simple terms, Raspberry Pi is a super cheap ($40) Linux based computer. That’s it. Seriously.It can do whatever you can imagine a normal Linux computer
How to build a Deep Learning Image Classifier for Game of Thrones dragons
Performance of most flavors of the old generations of learning algorithms will plateau. Deep learning, training large neural networks, is scalable and perf
How to build an awesome stocks spreadsheet with React 16
How to build an awesome stocks spreadsheet with React 16React 16 is the first version of React built on top of React’s new core architecture, codenamed “Fi
How to train your own FaceID CNN using TensorFlow Eager execution
Faces are everywhere — from photos and videos on social media websites, to consumer security applications like the iPhone Xs FaceID.Computer vision, applie
How to Visualize Your Recurrent Neural Network with Attention in Keras
Now for the interesting part: the decoder. For any given character at position t in the sequence, our decoder accepts the encoded sequence h=(h1,...,hT) as
How to Build an AWS DeepLens Project with Amazon SageMaker
Amazon Web Services is Hiring. Amazon Web Services (AWS) is a dynamic, growing business unit within Amazon.com. We are currently hiring So
Using Scrapy to Build your Own Dataset
Using Scrapy to Build your Own DatasetWeb Scraping (Scrapy) using PythonWhen I first started working in industry, one of the things I quickly realized is s
Build your OWN Apple iBeacon with a Raspberry Pi
Feature US department store Macy’s recently said it is implementing iPhone-based tracking tech the better to encourage browsing punters
build your own website 1 -- how to install Apache Server
tps href root res apache2 sys targe serve name Operating System: Ubuntu 16.04 Web Server ? most often used Web Server software is freely
轉載 -- How To Optimize Your Site With GZIP Compression
// 下面這篇文章講的非常不錯,看完了 https://betterexplained.com/articles/how-to-optimize-your-site-with-gzip-compression/ // Content-Encoding, 定義 fr
轉載 -- How To Optimize Your Site With HTTP Caching
https://betterexplained.com/articles/how-to-optimize-your-site-with-http-caching/ // Caching Tutorial for Web Authors and Webmasters // 下面