Docker
Docker
Introduction
Each docker container will have its own processes, network and mounts like any other
operating system. Containers sits on docker layer which further sits on top of OS kernel. It is
like,
Docker vs VMs
It's not an either container or VM situation. In industry, both are used together.
In industry if you have thousands of containers, you will often see that containers
provisioned on virtual docker hosts, that way we can utilize the advantages of both
technologies.
We can use the advantage of virtualization, to easily provision or decommission docker host
as required. At the same time, we can make use of the benefits of docker, to easily provision
applications and quickly scale them as required.
There are lots of versions of containerized applications readily available in a public docker
repository called docker hub or docker store .
Once you install docker on your host, bringing the image is as easy as running the docker
run command
$ docker run ansible # will download *ansible* docker image from docker
registry (dockerhub) to the host on which docker is installed and will run the
container. If *ansible* image is available on the docker host, it will start
running the container as it is already downloaded.
Container vs Image
Docker Image is a package or a template which is used to create one or more containers.
Containers are running instances of images that are isolated and have their own
environment and set of processes. A number of processes have been dockerized already.
If you are not able to find the image you need, you can create your own image and push it
docker-hub making it available publicly.
Developers use Dockerfile to create a docker image. So that ops teams can use this image to
deploy the application.
Docker Commands
$ docker run ngnix
Runs the ngnix image. If it finds the ngnix image in local file system, it will start it, otherwise, it will
pull the image ngnix image from the docker hub and starts it.
$ docker ps
$ docker ps -a
Shows the list of running containers along with the exited container.
Removes the image. Make sure container is stopped before deleting the image.
$ docker images
Pull the image and save it locally, but it won't run the image.
When you run Ubuntu image, it will simply exit. Because you haven't told it to execute any
comman inside it. You can do so using the below command.
When the container starts, this will run the sleep command and goes to sleep for 5 seconds post
the sleep command completes the container stops.
When the docker container is running, we can execute any command in the container as shown
above. This command will print the contents of hosts file in the Ubuntu container.
Docker Run
$ docker run -d kodekloud/simple-webapp
Runs the container in detached mode (in background).
If you just give redis , it will pull the latest image of the redis. But if you want to pull a particular
version of redis then you can specify the tag. Here 4.0 is the tag which is the redis version. The
default tag is latest . The tag latest is associated with the latest version of that software.
Maps docker host with docker container. So, that we can access the website that is started on the
container, on the docker host.
Docker Images
Dockerfile
From Ubuntu
COPY . /opt/source-code
Use the docker file to build the docker image with name suneel/my-flask-app. All the layers are
cached. So, incase if any step fails the docker build starts from the point of failure as previous
layers are cached.
What you can containerize? You can containerize everything (browser, software etc.)
Shows the docker history of the docker build. The layered structure with size at each layer.
Pass environment variables from docker host to docker container using -e flag. You can find the
running docker containers configuration options using docker inspect simple-webapp-color
and look at the, Config --> Env .
# app.py
import os
from flask import Flask
app = Flask(__name__)
color = os.environ.get("APP_COLOR")
@app.route("/")
def main():
print(color)
return render_template("hello.html", color=color)
if __name__ == "__main__":
app.run(host="0.0.0.0", port=8080)
Command vs Entrypoint
Key points
Running a docker image is like running a process and once the process is
completed/stopped/crashed the container will exit automatically though you haven't
stopped explicitly. A container lives only as long as the process inside it is alive.
Port Mapping: Docker host (Docker engine) is the local system, the system on which we run
the docker command to start the docker container. If a web-server is running on a docker
container, we want to access it in our system browser. How to do that? we can map the port
on the container to the docker host port and access through docker host port.