0% found this document useful (0 votes)
76 views11 pages

Docker Container Management Guide

Docker allows building, deploying and running applications by using containers. It manages containers that can contain a single application or service. The document discusses Docker concepts like images, containers, volumes and networks. It also explains Docker commands to build, run, manage and distribute containerized applications.

Uploaded by

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

Docker Container Management Guide

Docker allows building, deploying and running applications by using containers. It manages containers that can contain a single application or service. The document discusses Docker concepts like images, containers, volumes and networks. It also explains Docker commands to build, run, manage and distribute containerized applications.

Uploaded by

sabry ALzahar
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

Docker

=======
docker is containers manager
<!-- container can contain one Process only -->
docker download (bins & libs) only for any derstbution and share kernel with a host
service-oriented architecture : split any application work on docker to mote than
container and communicate with each other

Service-oriented architecture: distribute the app on multi containers

- Containers - Running instances of Docker images

docker :-
- engine
- image
- container
- volumes
- network
- registry
- store
- store
- commuinty (docker hub)
- plugins

- When you executed the command docker container run hello-world, it also did a
docker image pull behind the scenes to download the hello-world image.

docker run -> create a container


/var/lib/docker/, including images, containers, volumes, and networks, are
preserved. The Docker CE package is now called docker-ce.

-----------------------------------------------------------------------------------
---------

Docker Container
=================

- docker [container]run []-> optional


* --interactive -> -i
* --tty #terminal# -> -t
* --hostname= -> -h -> default hostmane == container_id
* --name=<value> || --name <value>
* --rm Automatically remove the container when it
exits
* --volume -> -v -> work like sshfs tpically -v /mnt/log:/var/log/nignx
* --publish #port# -> -p -> work like port_forwarding tpically -p 5000:80
#host_port:container_port#
By default, the port on the host is mapped to [Link], which means all IP
addresses. You can specify a particular IP address when you define the port
mapping, for example, -p [Link]:8080:80
* -p -> 80 == EXPOSE 80 connect a ramdom port (to know it use: docker ps -a)
on host to port 80 on container
* --detach -> -d -> still play however exit it => to connect with container
should use <docker exec>
* --user -u (root | 0 | sabry )
* --env -e -> environment variable
* --restart=always
* --mount type=bind,source="$(pwd)",target=/usr/share/nginx/html #Bind mounts
mean that any changes
made to the local file system are
immediately reflected in the
running container#
* <excute_command_inside_the_container> ls -la

- docker container (ls|ps) == docker ps -> run containers only


- docker container (ls|ps) -a == docker ps -a -> all containers
- docker container start <container_ID> -ia [interactive] [attach]
- docker container restart <container_ID>
- docker container pause <container_ID>
- docker container stop <container_ID>
- docker container exec <container_id> <command>
- docker container diff <container_id>
- docker container commit <container_id> -> add above base image
- docker container commit <container_id> --author [-a] sabry <new_image_name>
- docker container logs -> show logs
- docker container top -> show prosess
- docker container stop $(docker container ls --all [-a] --quiet [-q]) --all = up
+ existed --quiet = print id only
- docker container rm --force [-f] $(docker container ls -aq)
- Ctrl-P + Ctrl-q exit a container and still running

docer search coreos -> search in docker hub

-v /path:/path/in/container => mount /path at host machine with path in container


-v path:/path/in/container => create a volume path

-----------------------------------------------------------------------------------
---------
Docker Volume
==============

- docker volume
- create --name DataVolume1 -> free volume (not connected with any container)
- inspect
- ls
- prune
- rm

- --volumes-from Container4
- --volumes-from Container4:ro -> ro -> read only

create Create a volume


inspect Display detailed information on one or more volumes
ls List volumes
prune Remove all unused volumes
rm Remove one or more volumes

* if we create a volume first then we attach it with a container --rm not remove
this volume
* HOST_PATH and CONTAINER_PATH can be a folder or file. HOST_PATH must exist before
running this command

-----------------------------------------------------------------------------------
---------

Docker Image
=============

- docker image
pull <image_name> alpine
- docker image
ls
- docker image
inspect <image_name> alpine
- docker image
rm <image_id>
- docker image
tag [-t] <image_id> ourfiglet
- docker image
build -t hello:v0.1 . #DockerID/application_name:version#
<repository>/<image>:<tag>
default :
library/<image>:latest
- docker image history <image_id>
- docker image inspect --format "{{ json .[Link] }}" <image_id>
- docker image ls --filter [-f] reference="$DOCKERID/*"
- docker image build --file [-f] Dockerfile-v2 -t hello:v0.2 . as we do not use
the default name for our
Dockerfile, we use the -f option to point
towards the one we need to use
Notice that several lines of the output say Layer already exists. This is because
Docker will leverage read-only layers that are the same as any previously uploaded
image layers.

-----------------------------------------------------------------------------------
---------

Docker Network
===============

- create
- connect
- disconnect
- inspect
- ls
- prune
- rm

Usage: docker network COMMAND

Manage networks

Options:
--help Print usage

Commands:
connect Connect a container to a network
create Create a network
disconnect Disconnect a container from a network
inspect Display detailed information on one or more networks
ls List networks
prune Remove all unused networks
rm Remove one or more networks

Run 'docker network COMMAND --help' for more information on a command.

The bridge network is the default network for new containers. This means that
unless you specify a different network, all new containers will be connected to the
bridge network.

-----------------------------------------------------------------------------------
---------
Dockerfile
===========

* commands :-
- FROM
- RUN
- COPY
- WORKDIR
- ENTRYPOINT ["ping"] -> main command
- CMD ["[Link]"] -> can use as a command and can use as a argument
"parameter"

EX : docker container run ping:v0.1 [Link] -> [Link] is a parameter

ex :-

FROM alpine
RUN apk update && apk add nodejs
COPY . /app
WORKDIR /app the directory the container should
use when it starts up
CMD ["node","[Link]"] the commands which is run when the
container starts

docker image build -t hello:v0.1 .

* Lightweight Dstributions :-
- alpine -> apk -> package manager
- busybox
- coreos

.dockerignore

# comment
*/temp*
*/*/temp*
temp?
*.md
![Link]
-----------------------------------------------------------------------------------
---------

- when you <exit> from a container you will shutdown it


- when you create a container from image the container have automaticly <id> &&
<name>

* Terminology
----------------

- Layers - A Docker image is built up from a series of layers. Each layer


represents an instruction in the image’s Dockerfile. Each layer except the
last one is read-only.
- Dockerfile - A text file that contains all the commands, in order, needed to
build a given image. The Dockerfile reference page lists the various commands
and format details for Dockerfiles.
- Volumes - A special Docker container layer that allows data to persist and be
shared separately from the container itself. Think of volumes as a way to
abstract and manage your persistent data separately from the
application itself.

- docker :-
- login
- logout
- rm -> for containers
- rmi -> for images

- Docker Cloud
- Docker Store
- Docker Hub

* There are different ways to use containers. These include:

- To run a single task: This could be a shell script or a custom app.


- Interactively: This connects you to the container similar to the way you SSH into
a remote server.
- In the background: For long-running services like websites and databases.
-----------------------------------------------------------------------------------
---------

docker-compose
===============

- docker-compose build
- docker-compose up -d

- First Alpine Linux Containers


- Doing More With Docker Images
- Docker for Beginners - Linux
- Docker images deeper dive

- [Link] with SQL Server on Docker

/var/lib/docker/overlay2 folder where the image and container layers are stored

What are Moby and LinuxKit?

- Moby: An open framework to assemble specialized container systems without


reinventing the wheel.

and:

- LinuxKit, a toolkit for building custom minimal, immutable Linux distributions.

docker info

‫ملحظة ثانية مشروع كور أو أس‬


coreos
‫ أصبح تابع لمشروع‬atomic
‫تحت رعاية ريدهات‬

docker exec --ti x1 ps ax


- docker run -l user=12345 -d redis
- echo 'user=123461' >> labels && echo 'role=cache' >> labels
- docker run --label-file=labels -d redis
- LABEL vendor=Katacoda \
[Link]=0.0.5 \
[Link]-date=2016-07-01T10:47:29Z \
[Link]=Docker

- docker inspect -f "{{json .[Link] }}" rd


- docker inspect -f "{{json .[Link] }}" katacoda-label-example
- docker images --filter "label=vendor=Katacoda"
- docker -d \
-H unix:///var/run/[Link] \
--label [Link]="production" \
--label [Link]="ssd"

container_name:
property: value
- or options

docker-compose up -d
docker-compose up redis -d

docker-compose scale web=3

docker ps --format '{{.Names}} container is using {{.Image}} image'


docker ps --format 'table {{.Names}}\t{{.Image}}'
docker ps -q | xargs docker inspect --format '{{ .Id }} - {{ .Name }} - {{
.[Link] }}'

Start Docker Containers on Boot with Systemd

Floating ip
Usage: docker COMMAND

A self-sufficient runtime for containers

Options:
--config string Location of client config files (default
"/home/scrapbook/.docker")
-D, --debug Enable debug mode
-H, --host list Daemon socket(s) to connect to
-l, --log-level string Set the logging level
("debug"|"info"|"warn"|"error"|"fatal") (default "info")
--tls Use TLS; implied by --tlsverify
--tlscacert string Trust certs signed only by this CA (default
"/home/scrapbook/.docker/[Link]")
--tlscert string Path to TLS certificate file (default
"/home/scrapbook/.docker/[Link]")
--tlskey string Path to TLS key file (default
"/home/scrapbook/.docker/[Link]")
--tlsverify Use TLS and verify the remote
-v, --version Print version information and quit

Management Commands:
config Manage Docker configs
container Manage containers
image Manage images
network Manage networks
node Manage Swarm nodes
plugin Manage plugins
secret Manage Docker secrets
service Manage services
swarm Manage Swarm
system Manage Docker
trust Manage trust on Docker images
volume Manage volumes

Commands:
attach Attach local standard input, output, and error streams to a running
container
build Build an image from a Dockerfile
commit Create a new image from a container's changes
cp Copy files/folders between a container and the local filesystem
create Create a new container
diff Inspect changes to files or directories on a container's filesystem
events Get real time events from the server
exec Run a command in a running container
export Export a container's filesystem as a tar archive
history Show the history of an image
images List images
import Import the contents from a tarball to create a filesystem image
info Display system-wide information
inspect Return low-level information on Docker objects
kill Kill one or more running containers
load Load an image from a tar archive or STDIN
login Log in to a Docker registry
logout Log out from a Docker registry
logs Fetch the logs of a container
pause Pause all processes within one or more containers
port List port mappings or a specific mapping for the container
ps List containers
pull Pull an image or a repository from a registry
push Push an image or a repository to a registry
- rename Rename a container
restart Restart one or more containers
rm Remove one or more containers
rmi Remove one or more images
run Run a command in a new container
save Save one or more images to a tar archive (streamed to STDOUT by
default)
search Search the Docker Hub for images
start Start one or more stopped containers
- stats Display a live stream of container(s) resource usage statistics
[--no-stream]-> one_shot
stop Stop one or more running containers
tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
top Display the running processes of a container
unpause Unpause all processes within one or more containers
update Update configuration of one or more containers
version Show the Docker version information
wait Block until one or more containers stop, then print their exit codes

Usage: docker container COMMAND

Manage containers

Options:

Commands:
attach Attach local standard input, output, and error streams to a running
container
commit Create a new image from a container's changes
cp Copy files/folders between a container and the local filesystem
create Create a new container
diff Inspect changes to files or directories on a container's filesystem
exec Run a command in a running container
export Export a container's filesystem as a tar archive
inspect Display detailed information on one or more containers
kill Kill one or more running containers
logs Fetch the logs of a container
ls List containers
pause Pause all processes within one or more containers
port List port mappings or a specific mapping for the container
prune Remove all stopped containers
rename Rename a container
restart Restart one or more containers
rm Remove one or more containers
run Run a command in a new container
start Start one or more stopped containers
stats Display a live stream of container(s) resource usage statistics
stop Stop one or more running containers
top Display the running processes of a container
unpause Unpause all processes within one or more containers
update Update configuration of one or more containers
wait Block until one or more containers stop, then print their exit codes

you can use pipes and xargs. A pipe passes the output from one command into the
input of another while xargs allows you to provide this input as arguments to a

integration with vscode


------------------------
extentions:
- Docker {Microsoft} -> IntelliSense -> ctrl+space, tab,IntelliSense images search
at docker hub ,docker commands
- Docker Explorer {Jun Han} -> GUI control

- [r-brain]([Link]) -> { R + sql + python } online


- [google colab]([Link]
‫القادمة‬-‫المرة‬/)
- [google CLOUD DATAPROC on Cloud Platform]([Link]
hl=ar)
- [floydhub]([Link]

Common questions

Powered by AI

Docker ensures isolation between containers using a combination of Linux kernel features, such as namespaces and cgroups. Namespaces provide separate spaces for system resources like file systems, process trees, and network, while cgroups limit the amount of resources a container can use. This isolation prevents processes running in one container from interfering with those in another, ensuring security and stable environments, which is critical for running multiple applications on the same host without conflicts .

The command 'docker container run' facilitates creating and starting containers based on Docker images, which allows for isolated execution environments. Key options include '--interactive' (-i) for interactive mode, '--tty' (-t) for terminal allocation, '--name' to assign a custom name, and '--publish' (-p) to map container ports to host ports, crucial for network connectivity. Additionally, '--detach' (-d) enables running containers in the background, and '--rm' ensures containers are automatically removed after they exit .

Docker images are constructed using layers, each representing a distinct instruction from a Dockerfile. This layered approach allows Docker to reuse layers that have not changed, significantly optimizing the build process by leveraging cache. During a build, if Docker identifies unchanged layers from previous builds, it reuses them, thereby reducing the time and resources needed to rebuild images with recurring components. This efficiency is critical for rapid deployment and iteration in development environments .

A Dockerfile is a script consisting of a sequence of instructions for building a Docker image. Each instruction in the Dockerfile creates a layer of the image, with the Dockerfile specifying commands such as 'FROM' to set a base image, 'RUN' to execute commands, 'COPY' to add files, and 'CMD' to define the executable to run when a container starts. The importance of a Dockerfile lies in automation and consistency in image creation processes, thereby facilitating reproducibility and effective container management .

Docker Volumes are designed to manage persistent data in a Docker environment. They allow data to be stored separately from the container’s filesystem, ensuring data remains intact even if the container is destroyed. Volumes can be created using 'docker volume create' and attached to containers, including options for read-only access. This feature is essential for data management in scenarios where container data needs to persist or be shared across multiple containers .

Docker provides two main mechanisms for data persistence: Volumes and Bind mounts. Volumes are stored within Docker and are ideal for sharing data among multiple containers or when data consistency across restarts is critical, without dependence on a specific host's file system layout. Bind mounts link specific host directories to container paths, useful for scenarios requiring direct access to host filesystems for development or debugging purposes. Both mechanisms ensure that essential data survives container restarts .

'docker-compose' is a tool used for defining and running multi-container Docker applications. By using a YAML file to configure application services, networks, and volumes, 'docker-compose' allows users to manage complex containerized applications with single commands such as 'docker-compose up -d' to start services in the background. It simplifies orchestrating containers by ensuring all services are brought up in a defined order, essential for applications composed of multiple interconnected containers .

Docker provides various network management capabilities, including creating ('docker network create'), connecting ('docker network connect'), disconnecting ('docker network disconnect'), and removing networks ('docker network rm'). The default 'bridge' network automatically connects new containers unless another specific network is designated. This network simplifies communication between containers on a single Docker host, allowing them to communicate internally without explicit exposure of ports externally .

Docker's image management strategy includes 'pull', 'commit', and 'push' commands. 'Pull' downloads images from a registry, enabling the local creation of containers based on those images. 'Commit' creates a new image from a container's changes, allowing modifications or base images to be preserved. 'Push' uploads images to a registry, facilitating sharing and deployment. Together, these commands enable a comprehensive lifecycle management of Docker images across environments .

The 'docker exec' command allows administrators to run commands in an active container without restarting it. This facilitates real-time monitoring, debugging, and configuration changes. However, there are challenges: security concerns as it bypasses container isolation principles by allowing additional processes, and potential dependency issues if used to alter the container state in ways not reflected in the image source, leading to inconsistencies between development and production environments .

You might also like