Docker
Docker
Docker
To automate building a Docker image, you decribe the building steps in a Docker
manifesto called the Dockerfile.
This text file uses a set of instructions to describe which base image the new container
is based on,
what steps need to be taken to install various dependencies and applications
what files need to be present in the image, how they are made available to a container
what ports should be exposed, and
what command should run when a container starts, as well as a few other things
Create a text file called Dockerfile in your working directory and write the following content
in it:
FROM ubuntu:14.04
ENTRYPOINT ["/bin/echo"]
The FROM instruction tells you which image to base the new image off of.
Here you choose the ubuntu:14.04 image from the Official Ubuntu repository in Docker Hub.
The ENTRYPOINT instruction tells you which command to run when a container based on
this image is started. To build the image, issue a docker build command.
You are now ready to run this container, specifying the image ID of the freshly built image and passing
an argument to it (i.e., Hi Docker !):
$ docker run e778362ca7cf Hi Docker !
Hi Docker !
Amazing—you ran echo in a container! A container was started using the image that you built from this
two-line Dockerfile.
The container ran and executed the command defined by the ENTRYPOINT instruction. Once this
command was finished, the container job was done and it exited. If you run it again without passing an
argument, nothing is echoed:
Compose is a tool for defining and running multi-container Docker applications. With
Compose, you use a YAML file to configure your application’s services.
Then, with a single command, you create and start all the services from your configuration.
For example, suppose you had an application which required NGNIX and MySQL, you
could create one file which would start both the containers as a service without the need to
start each one separately.
Using Compose is basically a three-step process:
Define the services that make up your app in docker-compose.yml so they can
be run together in an isolated environment.
Run docker-compose up and Compose starts and runs your entire app.
Docker Tags
Essentially you can use metadata to recognize versions of your Docker images
and to save more proven duplicates or variations on a key compilation.
Basically speaking, Dockertags provide helpful information about an image
variation/version.
Use of Docker Tags
We tell the Docker daemon to fetch the Docker file present in the current
directory (that’s what the . at the end does). Next, we tell the Docker
daemon to build the image and give it the specified tag. If you run docker
images, you should see an image whose repository is username/image_name
and tag is tag_name.
Explicitly tagging an image through the tag command.
docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]
Docker takes care of the networking aspects so that the containers can
communicate with other containers and also with the Docker Host. If you do
an ifconfig on the Docker Host, you will see the Docker Ethernet adapter.
The Bridge network assigns IPs in the range of 172.17.x.x to the containers
within it. To access these containers from outside you need to map the ports
of these containers to the ports on the host.
Docker Networking
Finally, the None network keeps the container in complete isolation, i.e. they
are not connected to any network or container.
Docker Networking
Bridge Network
In terms of networking, a bridge network is a Link Layer device which forwards traffic
between network segments.
In terms of Docker, a bridge network uses a software bridge which allows containers
connected to the same bridge network to communicate, while providing isolation
from containers which are not connected to that bridge network.
The Docker bridge driver automatically installs rules in the host machine so that
containers on different bridge networks cannot communicate directly with each other.
Host Mode
In the ‘host’ mode, the containers run in the host network namespace and use
the host IP address.
To expose the container outside the host, the container uses a port from the
host’s port space.
This means that you need to manage the ports that containers attach to since
they are all sharing the same port space.
Bridge Mode
Because the containers are in their own namespace, they have their own port
space and don’t have to worry about port conflicts
Docker Networking
Docker Networking
User-defined bridge networks are best when you need multiple containers to
communicate on the same Docker host.
Host networks are best when the network stack should not be isolated from
the Docker host, but you want other aspects of the container to be isolated.
Overlay networks are best when you need containers running on different
Docker hosts to communicate, or when multiple applications work together
using swarm services.
Communicate Between Docker
Containers
Containers are designed to be isolated. But they can send and receive requests
to other applications, using networking.
Docker creates virtual networks which let your containers talk to each other. In
a network, a container has an IP address, and optionally a hostname.
You can create different types of networks depending on what you would like to
do.