Build your own docker image
Method1 is quite old and not cleaned one . Its like a snapshot .
Method2 is clean one, we build from the scratch, and we maintain the docker file. This is one we are going to look in detail.
Method1:
- Launch the existing docker image (preferably official image such as ubuntu)
- Login to the docker and do your changes (such as installing the app, creating the files etc)
- Commit your changes in to new image
Let us see this method in detail.
Example.
Create the docker image with ssh server in Ubuntu.
Run the contrainer with ubuntu imagedocker run -i -t ubuntu bash
cloud@mkube:~$ sudo docker run -i -t ubuntu bash
root@3d507f55945f:/#
Install openssh server
root@3d507f55945f:/# apt-get update
......
root@3d507f55945f:/# apt-get install openssh-server
Configure & start the openssh server
root@3d507f55945f:/# mkdir /var/run/sshd
root@3d507f55945f:/# echo 'root:demo' | chpasswd
root@3d507f55945f:/# sed -i -e 's:UsePAM no:UsePAM yes:g' /etc/ssh/sshd_config
root@3d507f55945f:/# sed -i -e 's:PermitRootLogin without-password:PermitRootLogin yes:g' /etc/ssh/sshd_config
root@3d507f55945f:/# service ssh start
* Starting OpenBSD Secure Shell server sshd [ OK ]
root@3d507f55945f:/#
Note: username root, password demo
To Test the ssh login to the docker container:
To know the ip address of the container:
cloud@mkube:~$ sudo docker inspect 3d507f55945f
........
"IPAddress": "172.17.0.2",
.......
Now ssh to the IP:
cloud@mkube:~$ ssh [email protected]
[email protected]'s password:
To see the changes in the docker image happend(file changes with respect to base image) :
loud@mkube:~$ sudo docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3d507f55945f ubuntu "bash" 22 minutes ago Up 22 minutes dreamy_payne
cloud@mkube:~$ sudo docker diff 3d507f55945f
C /etc/alternatives
A /etc/alternatives/rcp
A /etc/alternatives/rcp.1.
.....
cloud@mkube:~$
To persist/save the changes:
docker commit <dockerid> <imagename>
cloud@mkube:~$ sudo docker commit 3d507f55945f suresh/sshserver
sha256:a0c78c0d6f1d980192d086513f8b54618c75f4df196a2ebe1e62c52392a324c4
cloud@mkube:~$
To see the new docker image created:
cloud@mkube:~$ sudo docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
suresh/sshserver latest a0c78c0d6f1d 53 seconds ago 215MB
ubuntu latest 2d696327ab2e 3 weeks ago 122MB
jenkins latest 7b8e864de864 3 weeks ago 812MB
hello-world latest 05a3bd381fc2 4 weeks ago 1.84kB
cloud@mkube:~$
Method2:
Write the docker configuration file and launch it directly.
Reference : https://docs.docker.com/engine/reference/builder/
Procedure:
- Create a docker file
- Build the docker image from the docker file
- Push your image in to docker repo(optional)
Simple Example:
Lets run my python application (https://github.com/sureshkvl/python-projects/blob/master/sms/sms.py in ubuntu trusty image. it exposes the 5000 Port and runs REST Server.
Download the sms.py & requirements.txt from github and keep it in the working dir.
Create docker file(Dockerfile):
FROM ubuntu:trusty
WORKDIR /app
COPY sms.py /app/
COPY requirements.txt /app/
RUN apt-get update && apt-get install -y python python-pip
RUN pip install -r requirements.txt
EXPOSE 5000
CMD ["python", "sms.py"]
Just List the file in the dir:
cloud@mkube:~/sms$ ls
Dockerfile requirements.txt sms.py
cloud@mkube:~/sms$
Build the docker image
cloud@mkube:~/sms$sudo docker build -t suresh/sms:v2 -f Dockerfile .
This will build the image and save as suresh/sms repository and tag v2.
To run a container from this image:
cloud@mkube:~/sms$ sudo docker run -d -p 5000:5000 suresh/sms:v2
24f82cea4706845f817e57b5062685994b1d9559b3d002b99d182cac9e5b031e
cloud@mkube:~/sms$ curl localhost:5000/students
{}
cloud@mkube:~/sms$
Cool. it works.
Method2-A:
This is same as Method2, but it automates the build process using Make tools as a Code. So the docker image build process will be maintained in version control system.
Refer this repo.
https://github.com/sureshkvl/kubernetes-examples/tree/master/docker-build
How to Push the Images to DockerHUB:
Login to dockerhub
sudo docker login
cloud@mkube:~/$ sudo docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: sureshkvl
Password:
Login Succeeded
sudo docker push <image>
cloud@mkube:~/docker-build$ sudo docker push sureshkvl/test
The push refers to a repository [docker.io/sureshkvl/test]
e8090bbd248d: Pushed
d11f970b132f: Pushed
854aaa736782: Pushed
4ade52c3fc78: Pushed
5bd3619c9a53: Pushed
7fb9ba64f896: Pushed
4e1e6ac5b9d6: Pushed
48daf661d621: Pushed
bf59e7acf5c4: Pushed
c47d9b229ca4: Pushed
1-1: digest: sha256:ee72455313c0955551661b5a6718131a0f865c4eb46f371c7035efcfc68bb694 size: 2403
e8090bbd248d: Layer already exists
d11f970b132f: Layer already exists
854aaa736782: Layer already exists
4ade52c3fc78: Layer already exists
5bd3619c9a53: Layer already exists
7fb9ba64f896: Layer already exists
4e1e6ac5b9d6: Layer already exists
48daf661d621: Layer already exists
bf59e7acf5c4: Layer already exists
c47d9b229ca4: Layer already exists
1-2: digest: sha256:ee72455313c0955551661b5a6718131a0f865c4eb46f371c7035efcfc68bb694 size: 2403
cloud@mkube:~/docker-build$