Docker
Q: What is Docker?
Ans:
Docker is a tool designed to make it easier to create, deploy, and run applications by using containers. Containers allow a developer to package up an application with all of the parts it needs, such as libraries and other dependencies, and deploy it as one package. By doing so, thanks to the container, the developer can rest assured that the application will run on any other Linux machine regardless of any customized settings that machine might have that could differ from the machine used for writing and testing the code.
Q: What are useful Docker commands ?
Ans :
To list docker version
docker --version
To list available images
docker images
To list running containers
docker ps
To list all stopped/terminated/Exited containers
docker ps -a
To pull image
docker pull "image_name" or docker pull "image_name" :"tag name"
tagname is kind of version for that particular image
To run/create a container in interactive mode
docker run -itd image_name --name=name of your container process(bash)
ex: docker run -itd --name="container_name" "image_name" bash
To remove all stopped containers from the list
docker rm $(docker ps -a -q)
To Stop all running container at once
docker stop $(docker ps -a -q)
To remove image
docker rmi imagename
For all images at once -> docker rmi $(docker images)
To go inside a running container
docker exec -it "container_id/container_name" bash
To save/convert changes from container to image
docker commit "container_id" image_name:<tagname>
To save the current image to ship it to another device or hardware
docker save "image name" > "my_image_name".tar
To load the above image back to container after shipping
docker load < "my_image_name".tar
To export the running container to another hardware or device with existing configuration
docker export "container_name" > "my_export_image".tar
To import the container
docker import - "new_image_name" < my_export_image.tar
To run a webserver
docker run -itd --name=container_name -p 9999:80 image_name:tagname /usr/sbin/httpd -D FOREGROUND
To push docker image to your Docker Hub Account. Initially,
docker login
docker tag imagename yourdockerusername/imagename:tag
docker push yourdockerusername/imagename:tag
To list container file system from Host
docker exec "container_id" command
ex:- docker exec container_id ls /tmp/data0
or
docker exec container_id cat /etc/passwd
To see docker network
docker network ls
To see network information of the containers
docker network inspect bridge
To create out own network
docker network create --driver=bridge --subnet=10.10.0.0/24 "my_network"
To check our network
docker network inspect drivername
ex:- docker network inspect "your_network_name" (network name or network id)
To launch container in our network
docker run -itd --network=my_network --name=myweb -p 9999:80 httpd_image:aamirs /usr/sbin/httpd -D FOREGROUND
or
docker run -itd --network=my_network --name=mytest_conatiner centos
To delete a network
docker network rm "network_id or network_name"
Create a container with desired resources
docker run -itd --memory="250m" --cpus="0.02" --network=my_network --name=mywebserver -v /tmp/myefs/docker_volume/:/var/www/html -p 9999:80 mywebserver:1.0 /usr/sbin/httpd -D FOREGROUND
Docker Volume
To mount volume on containers -v source:destination
docker run -itd --name=container_name -v /tmp/webapp:/var/www/html image_name
How to create docker volume
docker volume create "volume_name"
by default it will be created under /var/lib/docker/volumes
To find history of docker images
docker history image_name
To find all the information about a container
docker inspect container_id
Docker Daemon logs
Below command will log docker logs only in RHEL based VM's , Other OS may have different location to log the docker logs.
cat /var/log/messages | grep docker
Docker container logs
cat /var/lib/docker/containers/<container_id>/<container_id>-json.log
Container config info files
cat /var/lib/docker/containers/container_id/config.v2.json
cat /var/lib/docker/containers/container_id/hostconfig.json
Q: What is Unix command to start Docker?
Ans: systemctl start docker
systemctl enable docker
systemctl status docker
Q: What is multi stage build in Docker
Ans:
With multi-stage builds, you use multiple FROM statements in your Dockerfile. Each FROM instruction can use a different base,
and each of them begins a new stage of the build. You can selectively copy artifacts from one stage to another, leaving behind everything you don’t want in the final image.
To show how this works, let’s adapt the Dockerfile from the previous section to use multi-stage builds.
# syntax=docker/dockerfile:1
FROM golang:1.16
WORKDIR /go/src/github.com/alexellis/href-counter/
RUN go get -d -v golang.org/x/net/html
COPY app.go ./
RUN CGO_ENABLED=0 go build -a -installsuffix cgo -o app .
FROM alpine:latest
RUN apk --no-cache add ca-certificates
WORKDIR /root/
COPY --from=0 /go/src/github.com/alexellis/href-counter/app ./
CMD ["./app"]
You only need the single Dockerfile. You don’t need a separate build script, either. Just run docker build.
docker build -t alexellis2/href-counter:latest .
The end result is the same tiny production image as before, with a significant reduction in complexity. You don’t need to create any intermediate images,
and you don’t need to extract any artifacts to your local system at all.
How does it work? The second FROM instruction starts a new build stage with the alpine:latest image as its base. The COPY --from=0 line copies just the built
artifact from the previous stage into this new stage. The Go SDK and any intermediate artifacts are left behind, and not saved in the final image.
Q:Name and explain the various Docker components.
Ans:
The three main Docker components are:
Docker Client. Performs Docker build pull and run operations to open up communication with the Docker Host. The Docker command then employs Docker API to call any queries to run.
Docker Host. Contains Docker daemon, containers, and associated images. The Docker daemon establishes a connection with the Registry. The stored images are the type of metadata dedicated to containerized applications.
Registry. This is where Docker images are stored. There are two of them, a public registry and a private one. Docker Hub and Docker Cloud are two public registries
available for use by anyone.
Q: What is a container?
Ans:
Containers are deployed applications bundled with all necessary dependencies and configuration files.
All of the elements share the same OS kernel. Since the container isn’t tied to any one IT infrastructure, it can run on a different system or the cloud.
Q:What’s the difference between virtualization and containerization?
Ans:
Virtualization is an abstract version of a physical machine, while containerization is the abstract version of an application.
Q:Describe a Docker container’s lifecycle.
Ans:
Although there are several different ways of describing the steps in a Docker container’s lifecycle, the following is the most common:
- Create container
- Run container
- Pause container
- Unpause container
- Start container
- Stop container
- Restart container
- Kill container
- Destroy container
Q: How do you find stored Docker volumes?
Ans:
Use the command: /var/lib/docker/volumes
Q:How do you check the versions of Docker Client and Server?
Ans: $ docker --version
Q:how you would create a container from an image.
Ans:
To create a container, you pull an image from the Docker repository and run it using the following command: $ docker run -itd <image_name>
Q: How about a command to stop the container?
Ans:
Use the following command: $ sudo docker stop container name
Q: How would you list all of the containers currently running?
Use the command: $ docker ps
Q: What’s involved in scaling a Docker container?
Ans:
Docker containers have the potential to be scaled to any level needed. Thanks to the platform’s flexibility, you can have anything from a few hundred to a few thousand,
to millions of containers, providing they all have continual, unconstrained access to the required memory and OS.
Q:List some of the more advanced Docker commands and what they do.
Some advanced commands include:
Docker info. Displays system-wide information regarding the Docker installation
Docker pull. Downloads an image
Docker stats. Provides you with container information
Docker images. Lists downloaded images
Q: Can you lose data stored in a container?
Any data stored in a container remains there unless you delete the container.
Q: What platforms can you run Docker on?
The Linux platforms are:
ArchLinux
CentOS 6+
CRUX 3.0+
Fedora 19/20+
Gentoo
openSUSE 12.3+
RHEL 6.5+
Ubuntu 12.04, 13.04 et al
Docker can also run on the following cloud-based platforms:
Amazon EC2
Amazon ECS
Google Compute Engine
Microsoft Azure
Rackspace
Q: Which is the best method for removing a container: the command “stop container” followed by the command “remove the container,” the rm command by itself?
Stop the container first, then remove it. Here’s how:
$ docker stop <coontainer_id>
$ docker rm -f <container_id>
Q: Can a container restart on its own?
Ans:
Since the default flag -reset is set to false, a container cannot restart by itself.
Q: How do Docker daemon and the Docker client communicate with each other?
You use a combination of Rest API, socket.IO, and TCP to facilitate communication.
Q: Can you implement continuous development (CD) and continuous integration (CI) in Docker?
Ans:
Yes, you can. You can run Jenkins on Docker and use Docker Compose to run integration tests.
Q: What is Docker image?
Ans:
The Docker image help to create Docker containers. You can create the Docker image with the build command. Due to this, it creates a container that
starts when it begins to run. Every docker images are stored in the Docker registry.
Q: What is Docker Engine?
Ans:
Docker daemon or Docker engine represents the server. The docker daemon and the clients should be run on the same or remote host, which can communicate through command-line client binary and full RESTful API.
Q:What are the common instruction in Dockerfile?
Ans:
The common instruction in Dockerfile are: FROM, LABEL, RUN, and CMD.
Q:How can you run multiple containers using a single service?
Ans:
By using docker-compose, you can run multiple containers using a single service. All docker-compose files uses yaml language.
Q: How to pull any image from url?
Ans:
1. Log in to Artifactory:
docker login artifactory.example.com
2. Pull the Docker image:
docker pull artifactory.example.com/docker-repo/my-app:latest
3 Once the pull is complete, you can verify the image is available locally by running:
docker images
Comments
Post a Comment