Difference between revisions of "IT-SDK-Docker"

From wiki.samerhijazi.net
Jump to navigation Jump to search
(Created page with "<pre> Link: http://docs.projectatomic.io/container-best-practices/#_abstract </pre>")
 
(run (Run a command in a new container))
 
(179 intermediate revisions by 3 users not shown)
Line 1: Line 1:
<pre>
+
=Ref.=
Link: http://docs.projectatomic.io/container-best-practices/#_abstract
+
* https://docs.docker.com/docker-hub/access-tokens/
 +
* https://cloud.google.com/container-registry/docs/pulling-cached-images#docker-ui
 +
* https://github.com/Philip-Scott/docker-workspace
 +
* VS-Code & Containers: https://code.visualstudio.com/docs/remote/containers
 +
* CheatSheet: https://www.docker.com/sites/default/files/d8/2019-09/docker-cheat-sheet.pdf
 +
* Source: https://docs.docker.com/v17.09/engine/reference/builder/
 +
* Link: http://docs.projectatomic.io/container-best-practices/#_abstract
 +
* Link: https://docs.docker.com/develop/develop-images/dockerfile_best-practices/
 +
* Website-CMD: (https://docs.docker.com/engine/reference/commandline/)
 +
* Website-TUT: (https://docs.docker.com/engine/tutorials/usingdocker/)
 +
* Portainer: (http://portainer.io/index.html) stammt aus UI-for-Docker (https://github.com/kevana/ui-for-docker)
 +
* Docker & Kubernetes: https://www.middlewareinventory.com/blog/deploy-docker-image-to-kubernetes/
 +
* Docker & Kubernetes: https://www.linode.com/docs/kubernetes/deploy-container-image-to-kubernetes/
 +
 
 +
=Mix=
 +
<pre class="code">
 +
docker info
 +
nano ~/.docker/config.json
 +
</pre>
 +
=Definitions=
 +
* '''Docker Compose''' is a tool for defining and running multi-container Docker applications
 +
 
 +
=Installation=
 +
=setting=
 +
<pre class="code">
 +
/etc/containers/nodocker
 +
</pre>
 +
==docker-compose==
 +
* Source: https://docs.docker.com/compose/install/
 +
<pre class="code">
 +
sudo curl -L "https://github.com/docker/compose/releases/download/1.25.3/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
 +
sudo chmod +x /usr/local/bin/docker-compose
 +
</pre>
 +
 
 +
==fedora==
 +
* https://docs.docker.com/engine/install/fedora/
 +
<pre class="code">
 +
sudo dnf remove docker docker-client docker-client-latest docker-common docker-latest docker-latest-logrotate docker-logrotate docker-selinux docker-engine-selinux docker-engine
 +
-----
 +
sudo dnf config-manager --add-repo https://download.docker.com/linux/fedora/docker-ce.repo
 +
sudo dnf install docker-ce docker-ce-cli containerd.io docker-compose-plugin
 +
-----
 +
sudo systemctl start docker
 +
sudo systemctl enable docker
 +
-----
 +
sudo groupadd docker
 +
sudo usermod -aG docker $USER
 +
</pre>
 +
 
 +
==ubuntu==
 +
* Source: https://docs.docker.com/engine/install/ubuntu/
 +
<pre class="code">
 +
sudo apt-get remove docker docker-engine docker.io containerd runc
 +
sudo apt-get update
 +
sudo apt-get install apt-transport-https ca-certificates curl gnupg-agent software-properties-common
 +
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
 +
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
 +
sudo apt-get update
 +
sudo apt-get install docker-ce docker-ce-cli containerd.io
 +
-----------
 +
sudo groupadd docker
 +
sudo usermod -aG docker $USER
 +
</pre>
 +
 
 +
=Life-Cycle=
 +
==build (Build an image from a Dockerfile)==
 +
<pre class="code">
 +
$ docker build -t $NAME_IMAGE:0.1 $LOCATION_DOCKERFILE
 +
---
 +
$ docker build -t hijazi/app:v1 .
 +
$ docker build -t hijazi/app:$(date +%Y%m%d.%H%M%S) .
 +
</pre>
 +
 
 +
==run (Run a command in a '''new''' container)==
 +
<pre class="text">
 +
[d]: Detached Mode, run container in Background.
 +
[i]: Keep STDIN open.
 +
[t]: Allocate a pseudo-TTY.
 +
[rm]: Remove container after exit.
 +
[v]: Mount a volume >>> $PATH_IN_CONTAINER:$PATH_IN_LOCAL.
 +
[p]: Mount a posrt >>> $PORT_IN_CONTAINER:$PORT_IN_LOCAL.
 +
</pre>
 +
<pre class="code">
 +
$ docker run --name $STRING --rm -it -d -p $PORT_CONTAINER:$PORT_LOCAL -v $PATH_CONTAINER:$PATH_LOCAL $NAME_IMAGE
 +
----------
 +
$ docker run --name linux --rm -it -d -p 80:80 -v /home/user:/sandbox alpine
 +
$ docker run -v C:/Users/admin:/home alpine              # In CMD-Windows
 +
$ docker run -v C:\\Users\\admin:/home alpine            # In mintty, git-bash
 +
----------
 +
$ docker run -it --rm ubuntu //bin/bash                  # In CMD-Windows
 +
$ winpty docker run -it --rm ubuntu //bin/bash          # In mintty, git-bash
 +
</pre>
 +
 
 +
==exec (Run a command in a '''running''' container)==
 +
<pre class="code">
 +
# In bash, cmd
 +
$ docker exec -it $NAME_CONTAINER bash
 +
$ docker exec -it $NAME_CONTAINER sh
 +
----
 +
# In mintty, git-bash
 +
$ winpty docker exec -it $NAME_CONTAINER bash
 +
$ winpty docker exec -it $NAME_CONTAINER sh
 +
</pre>
 +
 
 +
==start & stop==
 +
<pre class="code">
 +
$ docker start $NAME_CONTAINER    ## Start a stopped container.
 +
$ docker stop $NAME_CONTAINER    ## Stop a running container.
 +
</pre>
 +
==List/Stop/Remove all==
 +
<pre class="code">
 +
docker ps -aq                      ### List all containers (only IDs)
 +
docker images -aq.                ### List all images (only IDs)
 +
docker stop $(docker ps -aq)      ### Stop all running containers
 +
docker rm $(docker ps -aq)        ### Remove all containers
 +
docker rmi $(docker images -aq)    ### Remove all images
 +
</pre>
 +
 
 +
=Management=
 +
==images==
 +
<pre class="code">
 +
docker images -a                        # Liste all images
 +
docker tag hijazi/app:v1 registry/hijazi/app:v2
 +
docker tag 9d6e50edcaad hijazi/app:v3
 +
docker pull hijazi/app:v1
 +
docker pusch hijazi/app:v2
 +
docker rmi hijazi/app:v0                  # Remove images
 +
</pre>
 +
==container==
 +
<pre class="code">
 +
docker ps –a                            # Liste all Containers
 +
docker rm $NAME                        # Remove one or more containers
 +
decker rename $NAME_OLD $NEW_NAME      # Rename a container
 +
docker commit -m "Massage" -a "Creator" 9d6e50edcaad hijazi/app:v1  # Create a new image from a container's changes
 +
</pre>
 +
 
 +
==config==
 +
<pre class="code">
 +
docker ps -a -q
 +
docker stop $(docker ps -a -q)
 +
docker rm $(docker ps -a -q)
 +
----
 +
docker-machine ip
 +
docker container ls
 +
docker image ls
 +
docker volume ls
 +
----
 +
docker system prune          # Remove all Unused container
 +
docker system prune --all    # Remove all Unused container and all unused images
 +
docker system prune --volumes # Remove all Unused container and all unused volumes
 +
</pre>
 +
<pre class="code">
 +
curl https://registry-1.docker.io/v2/ && echo Works
 +
docker info | grep Proxy
 +
</pre>
 +
 
 +
=Dockerfile=
 +
==Ref==
 +
* https://docs.docker.com/engine/reference/builder/
 +
* https://developers.redhat.com/articles/2021/10/12/10-steps-better-dockerfile
 +
==Dockerfile-Definition==
 +
<pre class="code">
 +
FROM        Sets the base image for subsequent
 +
MAINTAINER  Sets the author field of the generated images
 +
RUN     Execute commands in a new layer on top of the current image and commit the results
 +
CMD     Allowed only once (if many then last one takes effect)
 +
LABEL     Adds metadata to an image
 +
EXPOSE     Informs container runtime that the container listens on the specified network ports at runtime
 +
ENV     Sets an environment variable
 +
ADD     Copy new files, directories, or remote file URLs from >> into the filesystem of the container
 +
COPY (this) Copy new files or directories >> into the filesystem of the container
 +
ENTRYPOINT  Allows you to configure a container that will run as an executable
 +
VOLUME     Creates a mount point and marks it as holding externally mounted volumes from native host or other containers
 +
USER     Sets the username or UID to use when running the image
 +
WORKDIR     Sets the working directory for any RUN, CMD, ENTRYPOINT, COPY, and ADD commands
 +
ARG     Defines a variable that users can pass at build-time to the builder using --build-arg
 +
ONBUILD     Adds an instruction to be executed later, when the image is used as the base for another build
 +
STOPSIGNAL  Sets the system call signal that will be sent to the container to exit
 +
</pre>
 +
 
 +
==Dockerfile-Template==
 +
<pre class="code">
 +
FROM debian:stretch-slim
 +
USER root
 +
LABEL maintainer="NGINX Docker Maintainers <docker-maint@nginx.com>"
 +
WORKDIR /app
 +
COPY nginx-repo.crt /etc/ssl/nginx/
 +
RUN apt-get update && apt-get upgrade -y
 +
# --------------------------------------------------------------
 +
# nginx config for OpenShift
 +
RUN chmod g+rwx /var/cache/nginx /var/run /var/log/nginx
 +
RUN sed -i.bak 's/listen\(.*\)80;/listen 8081;/' /etc/nginx/conf.d/default.conf
 +
RUN sed -i.bak 's/^user/#user/' /etc/nginx/nginx.conf
 +
# --------------------------------------------------------------
 +
EXPOSE 80
 +
STOPSIGNAL SIGTERM
 +
CMD ["nginx", "-g", "daemon off;"]
 +
USER 1001
 +
</pre>
 +
 
 +
=Docker-Images=
 +
==Image: Versions==
 +
* maven:3.6.3-jdk-8
 +
* maven:3.6.3-jdk-8-slim
 +
* gradle:6.8.0-jdk8
 +
* gradle:4.7.0-jdk8-alpine
 +
* nginx:alpine
 +
* node:10-alpine3.10
 +
* openjdk:8-alpine
 +
 
 +
==Image: Jenkins==
 +
* src: https://wiki.jenkins.io/display/JENKINS/Installing+Jenkins+with+Docker
 +
<pre class="code">
 +
export WORKSPACE=/workspace
 +
...
 +
sudo docker run --detach \
 +
--hostname jenkins.box-blue \
 +
--publish 49001:8080 \
 +
--name jenkins \
 +
--restart always \
 +
--volume $WORKSPACE/jenkins:/var/jenkins_home:z \
 +
--tty jenkins/jenkins
 +
</pre>
 +
 
 +
==Image: GitLab==
 +
* src: https://docs.gitlab.com/omnibus/docker/
 +
<pre class="code">
 +
export WORKSPACE=/workspace
 +
...
 +
sudo docker run --detach \
 +
  --hostname gitlab.box-blue \
 +
  --publish 443:443 \
 +
  --publish 80:80 \
 +
  --publish 22:22 \
 +
  --name gitlab \
 +
  --restart always \
 +
  --volume $WORKSPACE/gitlab/config:/etc/gitlab \
 +
  --volume $WORKSPACE/gitlab/logs:/var/log/gitlab \
 +
  --volume $WORKSPACE/gitlab/data:/var/opt/gitlab \
 +
  gitlab/gitlab-ce:latest
 +
...
 +
sudo docker exec -it gitlab /bin/bash
 +
</pre>
 +
 
 +
==Image: Postgres==
 +
</pre>
 +
* ref: https://hub.docker.com/_/postgres/
 +
* ref: https://quarkus.io/guides/reactive-sql-clients
 +
<pre class="code">
 +
docker run -d --ulimit memlock=-1:-1 -it --rm=true --memory-swappiness=0 --name postgres -e POSTGRES_USER=db -e POSTGRES_PASSWORD=db -e POSTGRES_DB=db_01 -p 5432:5432 postgres:10.5
 +
</pre>
 +
* ref: https://www.pgadmin.org/docs/pgadmin4/latest/container_deployment.html
 +
<pre class="code">
 +
docker pull dpage/pgadmin4
 +
docker run -p 80:80 \
 +
--name pgadmin \
 +
-e 'PGADMIN_DEFAULT_EMAIL=user@mail.com' \
 +
-e 'PGADMIN_DEFAULT_PASSWORD=password' \
 +
-d dpage/pgadmin4
 +
</pre>
 +
==Image: HiveMQ==
 +
* https://www.hivemq.com/downloads/docker/
 +
<pre class="code">
 +
docker run --rm --name mqtt-ce -p 8080:8080 -p 1883:1883 hivemq/hivemq-ce
 +
docker run --rm --name mqtt-pr -p 8080:8080 -p 1883:1883 hivemq/hivemq4
 +
--------------------------------------------------------------------
 +
http://localhost:8080
 +
User: admin
 +
Password: hivemq
 
</pre>
 
</pre>

Latest revision as of 12:40, 12 March 2025

Ref.

Mix

docker info
nano ~/.docker/config.json

Definitions

  • Docker Compose is a tool for defining and running multi-container Docker applications

Installation

setting

/etc/containers/nodocker

docker-compose

sudo curl -L "https://github.com/docker/compose/releases/download/1.25.3/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose

fedora

sudo dnf remove docker docker-client docker-client-latest docker-common docker-latest docker-latest-logrotate docker-logrotate docker-selinux docker-engine-selinux docker-engine
-----
sudo dnf config-manager --add-repo https://download.docker.com/linux/fedora/docker-ce.repo
sudo dnf install docker-ce docker-ce-cli containerd.io docker-compose-plugin
-----
sudo systemctl start docker
sudo systemctl enable docker
-----
sudo groupadd docker
sudo usermod -aG docker $USER

ubuntu

sudo apt-get remove docker docker-engine docker.io containerd runc
sudo apt-get update
sudo apt-get install apt-transport-https ca-certificates curl gnupg-agent software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io
-----------
sudo groupadd docker
sudo usermod -aG docker $USER

Life-Cycle

build (Build an image from a Dockerfile)

$ docker build -t $NAME_IMAGE:0.1 $LOCATION_DOCKERFILE
---
$ docker build -t hijazi/app:v1 .
$ docker build -t hijazi/app:$(date +%Y%m%d.%H%M%S) .

run (Run a command in a new container)

[d]: Detached Mode, run container in Background.
[i]: Keep STDIN open.
[t]: Allocate a pseudo-TTY.
[rm]: Remove container after exit.
[v]: Mount a volume >>> $PATH_IN_CONTAINER:$PATH_IN_LOCAL.
[p]: Mount a posrt >>> $PORT_IN_CONTAINER:$PORT_IN_LOCAL.
$ docker run --name $STRING --rm -it -d -p $PORT_CONTAINER:$PORT_LOCAL -v $PATH_CONTAINER:$PATH_LOCAL $NAME_IMAGE
----------
$ docker run --name linux --rm -it -d -p 80:80 -v /home/user:/sandbox alpine
$ docker run -v C:/Users/admin:/home alpine              # In CMD-Windows
$ docker run -v C:\\Users\\admin:/home alpine            # In mintty, git-bash
----------
$ docker run -it --rm ubuntu //bin/bash                  # In CMD-Windows
$ winpty docker run -it --rm ubuntu //bin/bash           # In mintty, git-bash

exec (Run a command in a running container)

# In bash, cmd 
$ docker exec -it $NAME_CONTAINER bash
$ docker exec -it $NAME_CONTAINER sh
----
# In mintty, git-bash
$ winpty docker exec -it $NAME_CONTAINER bash
$ winpty docker exec -it $NAME_CONTAINER sh

start & stop

$ docker start $NAME_CONTAINER    ## Start a stopped container.
$ docker stop $NAME_CONTAINER     ## Stop a running container.

List/Stop/Remove all

docker ps -aq                      ### List all containers (only IDs)
docker images -aq.                 ### List all images (only IDs)
docker stop $(docker ps -aq)       ### Stop all running containers
docker rm $(docker ps -aq)         ### Remove all containers
docker rmi $(docker images -aq)    ### Remove all images

Management

images

docker images -a                         # Liste all images
docker tag hijazi/app:v1 registry/hijazi/app:v2
docker tag 9d6e50edcaad hijazi/app:v3
docker pull hijazi/app:v1
docker pusch hijazi/app:v2
docker rmi hijazi/app:v0                  # Remove images

container

docker ps –a                            # Liste all Containers
docker rm $NAME                         # Remove one or more containers
decker rename $NAME_OLD $NEW_NAME       # Rename a container
docker commit -m "Massage" -a "Creator" 9d6e50edcaad hijazi/app:v1   # Create a new image from a container's changes

config

docker ps -a -q
docker stop $(docker ps -a -q)
docker rm $(docker ps -a -q)
----
docker-machine ip
docker container ls
docker image ls
docker volume ls
----
docker system prune           # Remove all Unused container
docker system prune --all     # Remove all Unused container and all unused images
docker system prune --volumes # Remove all Unused container and all unused volumes
curl https://registry-1.docker.io/v2/ && echo Works
docker info | grep Proxy

Dockerfile

Ref

Dockerfile-Definition

FROM        Sets the base image for subsequent
MAINTAINER  Sets the author field of the generated images
RUN	    Execute commands in a new layer on top of the current image and commit the results
CMD	    Allowed only once (if many then last one takes effect)
LABEL	    Adds metadata to an image
EXPOSE	    Informs container runtime that the container listens on the specified network ports at runtime
ENV	    Sets an environment variable
ADD	    Copy new files, directories, or remote file URLs from >> into the filesystem of the container
COPY (this) Copy new files or directories >> into the filesystem of the container
ENTRYPOINT  Allows you to configure a container that will run as an executable
VOLUME	    Creates a mount point and marks it as holding externally mounted volumes from native host or other containers
USER	    Sets the username or UID to use when running the image
WORKDIR	    Sets the working directory for any RUN, CMD, ENTRYPOINT, COPY, and ADD commands
ARG	    Defines a variable that users can pass at build-time to the builder using --build-arg
ONBUILD	    Adds an instruction to be executed later, when the image is used as the base for another build
STOPSIGNAL  Sets the system call signal that will be sent to the container to exit

Dockerfile-Template

FROM debian:stretch-slim
USER root
LABEL maintainer="NGINX Docker Maintainers <docker-maint@nginx.com>"
WORKDIR /app
COPY nginx-repo.crt /etc/ssl/nginx/
RUN apt-get update && apt-get upgrade -y
# --------------------------------------------------------------
# nginx config for OpenShift
RUN chmod g+rwx /var/cache/nginx /var/run /var/log/nginx
RUN sed -i.bak 's/listen\(.*\)80;/listen 8081;/' /etc/nginx/conf.d/default.conf
RUN sed -i.bak 's/^user/#user/' /etc/nginx/nginx.conf
# --------------------------------------------------------------
EXPOSE 80
STOPSIGNAL SIGTERM
CMD ["nginx", "-g", "daemon off;"]
USER 1001

Docker-Images

Image: Versions

  • maven:3.6.3-jdk-8
  • maven:3.6.3-jdk-8-slim
  • gradle:6.8.0-jdk8
  • gradle:4.7.0-jdk8-alpine
  • nginx:alpine
  • node:10-alpine3.10
  • openjdk:8-alpine

Image: Jenkins

export WORKSPACE=/workspace
...
sudo docker run --detach \
 --hostname jenkins.box-blue \
 --publish 49001:8080 \
 --name jenkins \
 --restart always \
 --volume $WORKSPACE/jenkins:/var/jenkins_home:z \
 --tty jenkins/jenkins

Image: GitLab

export WORKSPACE=/workspace
...
sudo docker run --detach \
  --hostname gitlab.box-blue \
  --publish 443:443 \
  --publish 80:80 \
  --publish 22:22 \
  --name gitlab \
  --restart always \
  --volume $WORKSPACE/gitlab/config:/etc/gitlab \
  --volume $WORKSPACE/gitlab/logs:/var/log/gitlab \
  --volume $WORKSPACE/gitlab/data:/var/opt/gitlab \
  gitlab/gitlab-ce:latest
...
sudo docker exec -it gitlab /bin/bash

Image: Postgres

docker run -d --ulimit memlock=-1:-1 -it --rm=true --memory-swappiness=0 --name postgres -e POSTGRES_USER=db -e POSTGRES_PASSWORD=db -e POSTGRES_DB=db_01 -p 5432:5432 postgres:10.5
docker pull dpage/pgadmin4
docker run -p 80:80 \
 --name pgadmin \
 -e 'PGADMIN_DEFAULT_EMAIL=user@mail.com' \
 -e 'PGADMIN_DEFAULT_PASSWORD=password' \
 -d dpage/pgadmin4

Image: HiveMQ

docker run --rm --name mqtt-ce -p 8080:8080 -p 1883:1883 hivemq/hivemq-ce
docker run --rm --name mqtt-pr -p 8080:8080 -p 1883:1883 hivemq/hivemq4
--------------------------------------------------------------------
http://localhost:8080
User: admin
Password: hivemq