Docker is a virtualization technology which is used to bundle and run our applications. As against hardware level virtualization offered by likes of VMWare, Virtualbox, AWS, docker offers a light weight alternative. The reason is that in container virtualization technology, which is used in docker, we make use of underlying OS and add a thin layer of application virtualization over that. In a nutshell we need not have entire OS stack in the virtual image.
In this guide we shall look into docker in a hands-on way, so we shall need a docker environment. We can easily install docker in Linux, Mac and Windows 10 OSes. For Windows 8/7 there are alternatives available.
The best part is that we need not have docker installed to play with docker. Yes, we have the convenience of playing with it, from our browser. All we need to do, is to create a free docker hub account. Using the account we can login to online docker "lab" - https://labs.play-with-docker.com
In the web application click "+ Add new instance" to open a new shell with docker environment enabled. We can now play with the environment.
For running docker we need docker image. Images are obtained from Docker repository which is similar to a GIT repository. It has same concepts of local repo and global repo. So one is comfortable with GIT, they will be comfortable with Docker repo concepts too.
We can use a ready-made docker image or else build our own, from scratch or else from other images (Note: Even to build from scratch we can use a docker image called “scratch” !). To build a docker image,
- Create a docker file (Docker defaults the filename of same to Dockerfile)
- Build a docker image with aforementioned docker file. Docker shall automatically pull dependent images from remote global repo (if it is not found cached in local repo).
- Once built the image is pushed to the local repo.
- Optionally publish the image to Docker central repo (docker hub) using docker account.
- If pushed to global repository (Using one’s docker account) it can be pulled from anywhere into local repo.
- To run the image, instruct docker to run specific image from local repo. The image is not found in local repo, it will pull the image from central repo.
2. Docker Image
2.1. Build a docker image
We can build docker image from a docker file. A simple docker file looks as below
FROM alpine
COPY hello.txt /hello.txt
RUN echo "Hello from RUN" >> /hello.txt
ENTRYPOINT ["cat", "/hello.txt"]
COPY hello.txt /hello.txt
RUN echo "Hello from RUN" >> /hello.txt
ENTRYPOINT ["cat", "/hello.txt"]
The explanation of above docker file is as below
- FROM alpine : Build custom image from base image alpine (A lightweight linux distribution)
- COPY : Copy from host file system into docker image (Note: We need to create the file in host system - As explained below)
- RUN: Run a built-in command in temporary image. The changes affected by the command shall reflect permanently in docker image built.
- ENTRYPOINT : Instructs the built image to execute a command at startup.
# Create a hello.txt to be copies by docker COPY command below
echo "Hello pre-generated in host" >> hello.txt
# Create the Dockerfile to build docker image
cat >> Dockerfile << EOF
FROM alpine
COPY hello.txt /hello.txt
RUN echo "Hello from RUN" >> /hello.txt
ENTRYPOINT ["cat", "/hello.txt"]
EOF
echo "Hello pre-generated in host" >> hello.txt
# Create the Dockerfile to build docker image
cat >> Dockerfile << EOF
FROM alpine
COPY hello.txt /hello.txt
RUN echo "Hello from RUN" >> /hello.txt
ENTRYPOINT ["cat", "/hello.txt"]
EOF
Then build using docker build command
docker build –t <tagname> <directory_containing_Dockerfile>
Assuming that Dockerfile is in current directory (.),
docker build -t MyDockerImage .
The above command shall create a new custom image with tag name MyDockerImage in local Docker repository.
One can optionally tag existing docker images with another tag name. This is useful if we are to have a simple tag name for local image and proper repo tag name (reponame/tagname) for the image to be published to our repo.
docker tag <existing_tag_in_repo> <repo/tagname>
In our example lets tag image built as described in section above
docker tag MyDockerImage mydockerrepo/MyDockerImage
2.3. Push local image to central repo
One can optionally push the docker image to central repository so that it can be accessed from anywhere. Docker offers 1 private repo and unlimited public repos. To push the docker image from local repo to central repo
Login to the central repo
docker login
Use docker push command.
docker push <repo-name/image-name>
docker push mydockerrepo/MyDockerImage
docker pull mydockerrepo/MyDockerImage
3. Docker Container
The docker container is used to run a docker image.
docker run mydockerrepo/MyDockerImage
The docker containers can be listed using docker ps command.
To list active containers :
docker ps
To list all containers:
docker ps -a
docker rm $(docker ps -a -q)
docker ps -a -q | xargs docker rm
To be cont...
No comments:
Post a Comment