Docker – basics & commands

Docker is a platform for developers and sysadmins to develop, deploy, and run applications with containers. 

Containerization is increasingly popular because containers are:
    Flexible: Even the most complex applications can be containerized.
    Lightweight: Containers leverage and share the host kernel.
    Interchangeable: You can deploy updates and upgrades on-the-fly.
    Portable: You can build locally, deploy to the cloud, and run anywhere.
    Scalable: You can increase and automatically distribute container replicas.
    Stackable: You can stack services vertically and on-the-fly.

Images and containers
A container is launched by running an image. An image is an executable package that includes everything needed to run an application–the code, a runtime, libraries, environment variables, and configuration files.

A container is a runtime instance of an image–what the image becomes in memory when executed (that is, an image with state, or a user process). You can see a list of your running containers with the command, docker ps, just as you would in Linux.

Dockerfile defines what goes on in the environment inside your container. Access to resources like networking interfaces and disk drives is virtualized inside this environment, which is isolated from the rest of your system, so you need to map ports to the outside world, and be specific about what files you want to “copy in” to that environment. 

# Use an official Python runtime as a parent image
FROM python:2.7-slim

# Set the working directory to /app
WORKDIR /app

# Copy the current directory contents into the container at /app
ADD . /app

# Install any needed packages specified in requirements.txt
RUN pip install --trusted-host pypi.python.org -r requirements.txt

# Make port 80 available to the world outside this container
EXPOSE 80

# Define environment variable
ENV NAME World

# Run app.py when the container launches
CMD ["python", "app.py"]

What happens when you run your docker image?

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

 

Docker Commands

## List Docker CLI commands
docker
docker container –help
## Display Docker version and info
docker –version
docker version
docker info
## Execute Docker image
docker run hello-world
## List Docker images
docker image ls
## List Docker containers (running, all, all in quiet mode)
docker container ls
docker container ls –all
docker container ls -aq
docker build -t friendlyhello .  # Create image using this directory's Dockerfile
docker run -p 4000:80
friendlyhello  # Run "friendlyname" mapping port 4000 to 80
docker run -d -p 4000:80friendlyhello         # Same thing, but in detached mode
docker container ls                                # List all running containers
docker container ls -a             # List all containers, even those not running
docker container stop <hash>           # Gracefully stop the specified container

docker container kill <hash>         # Force shutdown of the specified container
docker container rm <hash>        # Remove specified container from this machine
docker container rm $(docker container ls –
a -q)         # Remove all containers
docker image ls -a                             # List all images on this machine
docker image rm <image id>            # Remove specified image from this machine
docker image rm $(docker image ls –
a -q)   # Remove all images from this machine
docker login             # Log in this CLI session using your Docker credentials
docker tag <image> username/repository:tag  # Tag <image> for upload to registry
docker tag
friendlyhello mail2koka/python-docker-started:part1
docker push username/repository:tag            # Upload tagged image to registry
docker run username/repository:tag                   # Run image from a registry
docker run -p 4000:80 mail2koka/python-docker-started:part1
docker container stop 6c88aafa5ce7                     # Stop the running container

 
Login into Image by running the container  -> docker run -it –entrypoint /bin/bash {image:tag}

Login into a running container –> docker exec -it {image:tag} bash

Docker Hub / Docker cloud 


 

Scale Application & Enabling load balancing

 A docker-compose.yml file is a YAML file that defines how Docker containers should behave in production.

 A single container running in a service is called a task.

version: "3"
services:
  web:
    # replace username/repo:tag with your name and image details
    image: mail2koka/python-docker-started:latest
    deploy:
      replicas: 5
      resources:
        limits:
          cpus: "0.1"
          memory: 50M
      restart_policy:
        condition: on-failure
    ports:
      - "80:80"
    networks:
      - webnet
networks:
  webnet:

 

— Step by Step to scale and enable application 
docker swarm init
docker stack ls                                            # List stacks or apps
docker stack deploy -c <composefile> <appname>  # Run the specified Compose file
  docker stack
deploy -c docker-compose.yml getstartedlab
docker service ls                 # List running services associated with an app
docker service ps <service>                  # List tasks associated with an app
  docker service ps getstartedlab_web
docker inspect <task or container>                   # Inspect task or container
docker container ls -q                                      # List container IDs
docker stack rm <appname>                             # Tear down an application
  docker stack rm
getstartedlab
docker swarm leave –force      # Take down a single node swarm from the manager

 

References :  https://docs.docker.com/get-started/ 

Leave a Reply

Your email address will not be published. Required fields are marked *