I used docker at work a couple days ago, so here to give a short overview on what is docker and how to use it.
What is Docker?
Docker is a hot containerization platform which wraps all the dependencies in an execution system and is easy to pack and ship as you go. What makes Docker so popular? Comparing to virtual machines, Docker is more portable and efficient. In Docker, operating systems and kernels are abstracted and shared, applications and everything else (e.g. code, runtime, system tools, system libraries) run in isolated space.
Docker automates the process of delivering software applications in a repeatable way which allows effective distributed system to get more deployments in a cloud. Its lightweight nature makes it easy for developers to quickly create and run containered applications. In a container applications are isolated and run securely with complete dependancies. You can run many containers simultaneously on a host. Each container is created from a Docker image. Here are some concepts that you need to know.
|image||A read-only template containing a series of layers stacked on top of each other.|
|container||Running instance of an image.|
|registry||Centralized host to store/manage/provide docker images. Docker Hub is very popular and is hosted by docker.com.|
|Dockerfile||A text document that is essential to assemble an image. A Dockerfile contains a series of commands which are usually identical to the ordered ones when executing for in corresponding OS. Some typical instructions are listed below.|
Docker.com provides very detailed docs about:
After successfully installed docker on your machine, run
docker in terminal to list all its options and commands. There are around 40 commands.
It’s as simple as running
docker build with a Dockerfile to get an image built from scratch. In Dockerfiles, the proper format is like
INSTRUCTION arguments. Here it introduces the basic docker instructions.
||To specify base image, must be the first instruction.|
||To declare environment variables that can be reused.|
||To add and copy new files and directories to the container.|
||To execute commands. The difference between
Dockerfile is a starting point to begin using docker (the other is just to pull images from public registry). Several examples of Dockerfile syntax are mentioned in docker docs as well. Now you need to arm with knowledge of some docker commands.
Playing with Docker
docker build -t MYIMG .to build image from Dockerfile – don’t forget the dot at the end of command line.
docker tag MYIMG NAME[:TAG]to add tags to image.
docker rmi IMG1 IMG2 ...to remove images.
docker run IMGto create and start a container from an image. E.g.
docker run -it --name=SOMENAME IMG
-ifor interactive session,
-tfor terminal interface,
-pfor exposing port,
-vfor mounting volumes.
docker start/stop/restart CONTAINERto start/stop/stop and start a container.
docker attach CONTAINERto connect to a running container,
docker exec CONTAINER COMMANDto run a command in a container.
docker rm CONTAINER CONTAINER ...to remove.
docker imagesshows all images in the engine.
docker psshows running containers,
docker ps -ashow running and complete ones.
docker logs CONTAINERgets logs from container.
docker search NAMEto get pre-built image from registry.
docker pull REPOSITORY[:TAG]to pull image/repository from registry, or just pull with filename if from Docker Hub.
docker push REPOSITORY[:TAG]to push local image to registry.
docker commit CONTAINER [NAME[:TAG]]to save container’s change into a new image.
Extensive reading: More tips about docker’s best practices, security issues and other useful commands.