Container vs VM
A container runs natively on Linux and shares the kernel of the host machine with other
containers. It runs a discrete process, taking no more memory than any other executable, making
it lightweight.
By contrast, a virtual machine (VM) runs a full-blown “guest” operating system
with virtual access to host resources through a hypervisor. In general, VMs incur a lot of
overhead beyond what is being consumed by your application logic.
Docker Image
A Docker image contains everything it needs to run, independent of the Linux server on
which it lives: a copy of the operating system, a database, code, configuration files,
dependencies, and so on.
An image is a file that makes up just enough operating system to run a container inside it.
An image includes everything needed to run an application - the code or binary, runtimes,
dependencies, and any other filesystem objects required.
To see list of downloaded images, following is command -
PS C:\Windows\system32> docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest bf756fb1ae65 5 months ago 13.3kB
To download an image, use Docker pull
To upload an image, use Docker push
To remove an image, use Docker rmi <image_id> command
Docker Container
Fundamentally, a container is nothing but a running process, with some added encapsulation
features applied to it in order to keep it isolated from the host and from other containers.
Containers have 1 main process, which when stops, the container too stops.
One of the most important aspects of container isolation is that each container interacts with its
own private filesystem; this filesystem is provided by a Docker image.
Means - Containers are isolated from each other…if we create a container with an image and create
some new file, then the original image does not change…image will not have that file. That file will
only be visible inside that particular container.
So, If we create another container also with same image, we will not see that new file.
Docker run command takes an image and runs a container inside it as a process that does something
To see a list of running containers, following is the command -
PS C:\Windows\system32> docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
Use -all option to see all containers (stopped /running /all)
PS C:\Windows\system32> docker ps --all
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0685cf8d0ff9 hello-world "/hello" 37 minutes ago Exited (0) 37 minutes ago competent_moore
Container to Image
Docker commit command creates a new image out of a container
Run process inside a container
To start another process in a running container
Use following command -
Docker exec
Detached containers – we can start a container and leave it running…this is called a detached
container. Use flag -d to specify it as a detached container when running it.
Docker run -d
We can exit the container, but the container still is running in background.
To re-enter into the container, we use docker attach command.
Looking at container output –
To see at the output of container – following is the command –
Docker logs container_name
Kill and stop containers –
Docker kill – stops container
Docker rm – removes the container
Volumes –
Volumes are like virtual disk storage, which can be shared across containers
2 types –
Persistant – permanent storage
Ephemeral – only exists till data is being used by a cotainer
To share a volume when running container –
Docker run -v <the path of the volume>
Docker registry –
A registry is a piece of software that manages distribution of images, and stores metadata about
images such as tags, etc.
It basically is a service that listens on a port, 5000 usually
Hub.docker.com
Docker search
DockerFile –
Docker files are small programs that describe how to build a docker image from scratch
We run these files with Docker build command
Each step in a docker file builds over the image created in previous step. So each line is like docker
run…creates a container of its own, saves the image and exits on next line
Processes that start on one line will not be running on the next line…each line of statement is like
a new process
Environment variables do persist between docker lines
Each step in docker file is cached, so docker can skip steps that are cached already and do not
need to be built again..(if anything has not been changed)
Dockerfiles describe how to assemble a private filesystem for a container, and can also contain
some metadata describing how to run a container based on this image.
Dockerfile statements and syntax
FROM – specifies which image to download and start from. Must always be the first expression in
dockerfile
RUN – runs the command through the shell, waits for it to finish and saves the result
ADD – adds files from source to destination
ENV – sets environment variables
ENTRYPOINT -
CMD – it sets the program to run when the container starts
EXPOSE – maps port into a container
VOLUME – defines the volumes
WORKDIR – sets the directory that the container starts in
USER – Sets which user the statements in the container will run as