0% found this document useful (0 votes)
29 views6 pages

Docker

The document discusses Docker concepts like containers, images, Dockerfile, Docker commands, Docker run options, volumes, networking and orchestration tools. It provides examples of common Docker commands to build, run, manage and deploy containers.

Uploaded by

suneel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views6 pages

Docker

The document discusses Docker concepts like containers, images, Dockerfile, Docker commands, Docker run options, volumes, networking and orchestration tools. It provides examples of common Docker commands to build, run, manage and deploy containers.

Uploaded by

suneel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Learn Docker with Hands On Coding

Exercises. For beginners in DevOps by


Mumshad Mannambeth
1. Introduction
2. Docker Commands
3. Docker Run
4. Docker Images
5. Docker Compose
6. Docker Registry
7. Docker Engine, Storage and Networking
8. Docker on Mac and Windows
9. Container Orchestration - Docker Swarm & Kubernetes

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,

OS -> kernel Docker -> containers


Docker can run any flavour of OS that supports the undelying kernel that docker is using.
Usually docker runs on Linux Kernel. So, any OS that supports Linux kernel can be created as
container, and docker can run those containers.
As docker utilizes Linux kernels, Windows containers are not supported.

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.

$ docker run mongodb

$ docker run redis

$ docker run nodejs

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.

Getting Started with Docker


There are two editions of docker.
1. Community edition
2. Enterprise edition - comes with addons like, image management, image security, unviersal
control plane for managing and orchestrating container runtimes.

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

Lists all the running containers and their basic information

$ docker ps -a

Shows the list of running containers along with the exited container.

$ docker rmi <name/id of image>

Removes the image. Make sure container is stopped before deleting the image.

$ docker images

Lists the available docker images.

$ docker pull <image-name>

Pull the image and save it locally, but it won't run the image.

$ docker run ubuntu

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.

$ docker run ubuntu sleep 5

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.

$ docker exec ubuntu cat /etc/hosts

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).

$ docker attach <starting 5 to 6 characters of docker ID>

Will attach the background running container back to the terminal.

$ docker run redis:4.0

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.

$ docker run -it kodekloud/simple-prompt-docker

i - stands for interactive mode


t - stands for terminal
With it option, we will have the container attaches terminal in interactive mode.

$ docker run -p <docker-host-port>:<docker-container-port> kodekloud/webapp

Maps docker host with docker container. So, that we can access the website that is started on the
container, on the docker host.

Eg: $ docker run -p 80:5000 kodekloud/webapp

$ docker run -v <docker-host-data-path>:<docker-container-data-path> mysql

Mounts volume on the docker host to the docker container.

$ docker inspect mysql

Shows complete details about the docker image mysql .

$ docker logs <image-name/id>

Shows container logs. stdout of the docker container.

Docker Images
Dockerfile
From Ubuntu

RUN apt-get update


RUN apt-get install python

RUN pip install flask


RUN pip install flask-mysql

COPY . /opt/source-code

ENTRYPOINT FLASK_APP=/opt/source-code/app.py flask run

$ docker build Dockerfile -t suneel/my-flask-app

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.)

$ docker push suneel/my-flask-app

Pushes the docker image to docker-hub

$ docker history suneel/my-flask-app

Shows the docker history of the docker build. The layered structure with size at each layer.

$ docker run -e APP_COLOR=blue simple-webapp-color

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.

You might also like