Code Snippet

Just another Code Snippet site

[Docker] How-To

Install on CentOs (7)

sudo tee /etc/yum.repos.d/docker.repo <<-'EOF'
[dockerrepo]
name=Docker Repository
baseurl=https://yum.dockerproject.org/repo/main/centos/7/
enabled=1
gpgcheck=1
gpgkey=https://yum.dockerproject.org/gpg
EOF
yum install docker-engine
systemctl enable docker.service
systemctl start docker
docker run --rm hello-world

Install Docker Compose

curl -L https://github.com/docker/compose/releases/download/1.8.0/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
docker-compose --version

To init/start a docker compose :

docker-compose build 
docker-compose up -d

New :

wget -qO- https://get.docker.com/ | sh
usermod -aG docker $(whoami)
systemctl enable docker.service
systemctl start docker.service
yum install epel-release
yum install -y python-pip
pip install docker-compose
yum upgrade python*

,


54 thoughts on “[Docker] How-To

  • Olivier says:

    Enter running container :

    docker exec -it <CONTAINER_NAME/ID> bash
  • Olivier says:

    Issue with IPv4/IPv6 port forwarding :
    https://www.gesellix.net/post/docker-and-ipv6-on-ubuntu-13-10-saucy-salamander/

    In /etc/sysctl.conf :

    net.ipv6.conf.all.forwarding=1
    

    then reboot the machine or :

    sysctl -p /etc/sysctl.conf
    
  • Olivier says:

    With error like :
    Get https://registry-1.docker.io/v2/: dial tcp: lookup registry-1.docker.io on …

    check :
    https://docs.docker.com/engine/admin/systemd/#http-proxy

  • Olivier says:

    stop & delete all containers & images :

    docker stop $(docker ps -a -q)
    docker rm -f $(docker ps -a -q)
    docker rmi $(docker images -q)
    
  • Olivier says:

    to remove unused containers:

    docker rm $(docker ps -qa --no-trunc --filter "status=exited")

    to remove unused images :

    docker rmi $(docker images --filter "dangling=true" -q --no-trunc)

    https://stackoverflow.com/questions/32723111/how-to-remove-old-and-unused-docker-images

    To remove untagged images :

    docker images --no-trunc | grep '<none>' | awk '{ print $3 }' | xargs -r docker rmi
    • Olivier says:

      Remove :
      – all unused images
      – all stopped containers
      – all volumes not used by at least one container
      – all networks not used by at least one container

      docker system prune --volumes --all --force
      docker system prune --volumes --force
  • Olivier says:

    Commit a container to an image

    docker commit <CONTAINER ID> <IMAGE_NAME>
    

    Export the image to a file

    docker save <IMAGE_NAME> > /path/to/img_file.tar
    

    Import the image :

    docker load < /path/to/img_file.tar
    
  • Olivier says:

    Allow unsecure registry (CentOS7)

    echo '{ "insecure-registries":["SERVER_IP:SERVER_PORT"] }' > /etc/docker/daemon.json
    service docker restart
    
  • Olivier says:

    Default docker home :

    /var/lib/docker
  • Olivier says:

    Centos : Change docker daemon settings :
    edit:

    /usr/lib/systemd/system/docker.service

    To change home :

    ExecStart=/usr/bin/dockerd --graph {{docker_home}}

    To change storage driver :

    ExecStart=/usr/bin/dockerd --storage-driver=devicemapper

    To increase default image size :

    ExecStart=/usr/bin/dockerd --storage-opt dm.basesize=20G
  • Olivier says:

    Add another user to docker group :

    groupadd docker
    usermod -aG docker $USER
    
    • Olivier says:

      The mechanism by which adding a user to group docker grants permission to run docker is to get access to the socket of docker at /var/run/docker.sock. If the filesystem that contains /var/run has been mounted with ACLs enabled, this can also be achieved via ACLs.

      setfacl -m user:username:rw /var/run/docker.sock
  • Olivier says:

    Network :

    Create / Delete a network

    docker network ls
    docker network create MY_NETWORK_NAME
    docker network rm MY_NETWORK_NAME
    

    Create a container for this network :

     docker run --network=MY_NETWORK_NAME ...
  • Olivier says:

    Create a tunnel to the Docker network (allow resolution of containers’s hostnames from host)

    #!/usr/bin/env bash
    
    # Create SSH-SERVER container
    docker run -d --name ssh-server -p 32222:22 corbinu/ssh-server
    
    # Add SSH key
    cat ~/.ssh/id_rsa.pub | docker exec -i ssh-server /bin/bash -c "cat >> /root/.ssh/authorized_keys"
    
    # Start sshuttle
    sshuttle --dns --daemon -vNHr root@127.0.0.1:32222
    

    if the containers are in a dedicated network, don’t forget to move the ssh-server in the same one (with –network=NETWORK_NAME)

  • Olivier says:

    Inspect an image :

    docker inspect IMAGE_NAME/IMAGE_TAG
  • Olivier says:

    Tag and push :

    docker tag [OPTIONS] IMAGE[:TAG] [REGISTRYHOST/][USERNAME/]NAME[:TAG]
    docker push NAME[:TAG]
    

    example =

    docker tag 518a41981a6a myRegistry.com/myImage
    docker push myRegistry.com/myImage
    
  • Olivier says:

    Dockerfile : Add SSH Server (centos/RHEL)

    FROM centos:centos7
    
    RUN yum -y update && yum install -y initscripts openssh openssh-server openssh-clients passwd sed
    RUN sshd-keygen
    RUN sed -i “s/#UsePrivilegeSeparation.*/UsePrivilegeSeparation no/g” /etc/ssh/sshd_config && sed -i “s/UsePAM.*/UsePAM no/g” /etc/ssh/sshd_config
    
    # setup default user
    # RUN useradd admin -G wheel -s /bin/bash -m
    # RUN echo ‘admin:welcome’ | chpasswd
    # RUN echo ‘%wheel ALL=(ALL) ALL’ >> /etc/sudoers
    
    # expose ports for ssh
    EXPOSE 22
    CMD    [“/usr/sbin/sshd”, “-D”]
    
  • Olivier says:

    CentOS : Upgrade docker version :

    Remove previous versions

    yum remove docker \
                      docker-client \
                      docker-client-latest \
                      docker-common \
                      docker-latest \
                      docker-latest-logrotate \
                      docker-logrotate \
                      docker-selinux \
                      docker-engine-selinux \
                      docker-engine
    

    Install dependencies

    yum install -y yum-utils \
      device-mapper-persistent-data \
      lvm2
    

    Add new repo

    yum-config-manager \
        --add-repo \
        https://download.docker.com/linux/centos/docker-ce.repo
    

    Install Docker CE

    yum install -y docker-ce
    
  • Olivier says:

    CentOS / Docker / LVM :

    https://docs.docker.com/storage/storagedriver/device-mapper-driver/#configure-direct-lvm-mode-for-production

    Steps =
    – add a new disk
    – create a partition + table partition (gparted)

    pvcreate /dev/sdc1
    
    vgcreate docker /dev/sdc1
    
    lvcreate --wipesignatures y -n thinpool docker -l 95%VG
    lvcreate --wipesignatures y -n thinpoolmeta docker -l 1%VG
    
    lvconvert -y --zero n -c 512K --thinpool docker/thinpool --poolmetadata docker/thinpoolmeta
    
    lvchange --metadataprofile docker-thinpool docker/thinpool
    
    lvs -o+seg_monitor
    

    edit /etc/docker/daemon.json :

    {
    "storage-driver": "devicemapper",
    "storage-opts": [
        "dm.thinpooldev=/dev/mapper/docker2-thinpool",
        "dm.use_deferred_removal=true",
        "dm.use_deferred_deletion=true",
        "dm.basesize=20G"
    ],
    "insecure-registries":["....lan:5000", "....lan:5000", "....lan"],
    "debug": true
    }
    

    delete all docker content (/var/lib/docker)
    restart service

  • Olivier says:

    Docker behind a proxy (Centos7 & systemctl)

    systemctl enable docker.service
    vi /etc/systemd/system/multi-user.target.wants/docker.service
    
    [Service]
    Environment="HTTP_PROXY=http://1.2.3.4:8080" "HTTPS_PROXY=http://1.2.3.4:8080" "NO_PROXY=*.local1.lan,*.local2.lan"
    
  • Olivier says:

    Dockerfile, execute command using ENV value :

    FROM ...
    
    ENV FOO "BAR"
    
    ADD start.sh /start.sh
    RUN chmod +x /start.sh
    ENTRYPOINT ["/start.sh"]
    
    CMD ["echo", "${FOO}"]
    

    with start.sh :

    #!/bin/bash
    
    /bin/bash -l -c "$*"
    
  • Olivier says:

    Inspect tag from containers/images :

    docker inspect --format '{{ index .Config.Labels "LABEL_NAME"}}'  CONTAINER_ID
    
  • Olivier says:

    Truncate docker JSON log file :

    truncate -s 0 /var/lib/docker/containers/*/*-json.log
    

    see also : https://stackoverflow.com/questions/980283/truncating-a-file-while-its-being-used-linux

  • Olivier says:

    Config Docker daemon for log rotation :

    {
    "log-driver": "json-file",
    "log-opts": {"max-size": "10m", "max-file": "3"}
    }

  • Olivier says:

    How to determine what containers use the docker volume?

    docker ps -a --filter volume=VOLUME_NAME_OR_MOUNT_POINT
    
  • Olivier says:

    When docker cp does not work (docker before 1.8) with Error Path not specified

    cat /local/file/path | docker exec -i <running-container-id> sh -c 'cat > /inside/docker/file/path'
    
  • Olivier says:

    Update restart policy for a running container =

    docker update --restart=on-failure:3 hopeful_morse
    
  • Olivier says:

    Enable the live restore setting to keep containers alive when the daemon becomes unavailable

    "live-restore": true
    
  • Olivier says:

    Reload Docker daemon config (without restart)

    systemctl reload docker
    
  • Olivier says:

    Switch from VirutalBox to Docker :

    As outlined here, Docker for Windows requires Hyper-V. This needs to be disabled before you can run VirtualBox.

    # Run from elevated prompt (admin privileges)
    bcdedit /set hypervisorlaunchtype off
    

    And to start using Docker for Windows again, re-enable Hyper-V:

    # Run from elevated prompt (admin privileges)
    bcdedit /set hypervisorlaunchtype auto
    

    see : https://fredrikaverpil.github.io/2018/03/15/switching-between-docker-and-virtualbox-on-windows-10/

  • Olivier says:

    Install on CentOS7 / RHEL 7 :

    
    yum install -y yum-utils device-mapper-persistent-data lvm2
    
    yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
    
    # For RHEL 7 Only
    subscription-manager repos --enable=rhel-7-server-extras-rpms
    
    yum install docker-ce
    
  • Olivier says:

    Install docker-compose on CentOS7 / RHEL 7 :

    yum install -y python-pip gcc python-devel
    yum upgrade -y python*
    pip install pip --upgrade
    pip install docker-compose
    
  • Error :

    su: /bin/bash: Resource temporarily unavailable
    

    Check the number of running process on the host machine for the docker container user (not the docker daemon)

    Check also the “defunct” processes :

    ps -ef | grep defunct
    
  • Install docker :

    yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo && yum install -y yum-utils device-mapper-persistent-data lvm2 docker-ce-18* docker-ce-cli-18* && systemctl enable docker.service
    

    In case of error with container-selinux :

    Error: Package: 3:docker-ce-18.09.9-3.el7.x86_64 (docker-ce-stable)
               Requires: container-selinux >= 2:2.74
    Error: Package: containerd.io-1.3.7-3.1.el7.x86_64 (docker-ce-stable)
               Requires: container-selinux >= 2:2.74
    

    Execute :

    subscription-manager repos --enable=rhel-7-server-extras-rpms
    
  • As it is not possible to expose a new port on a running container, it’s possible to create a 2nd container to expose a port and forward the traffic to the 1st container :

    docker run --rm --network=MY_NETWORK -p PUBLIC_PORT:1234 verb/socat TCP-LISTEN:1234,fork TCP-CONNECT:CONTAINER_IP:CONTAINER_PORT
    

    For example, to expose port 7001 from container 172.16.0.2 via port 9170 :

    docker run --rm --network=docker_tsoap_builder -p 9170:1234 verb/socat TCP-LISTEN:1234,fork TCP-CONNECT:172.16.0.2:7001
    
  • An easy way to keep a container running in detached mode is is to tail the /dev/null device as the CMD or ENTRYPOINT command of your Docker image.

    FROM ...
    
    CMD tail -f /dev/null
    
  • Crontab and docker
    Error :
    the input device is not a TTY

    Solution :

    docker exec -i containername /path/to/command
    

    do not use “-it”

  • Update docker daemon to use a mirror registry (instead of docker.io / docker hub)

    {
    "registry-mirrors": ["https://MIRROR_ADDRESS"]
    }
    

    and reload docker daemon

    systemctl reload docker
    
  • Remove all images matching a specific string :

    docker rmi $(docker images --format "{{.Repository}}:{{.Tag}}"|grep "MY_VERSION")
    
  • List all containers from a specific network :

    docker network inspect \
      -f '{{ range $key, $value := .Containers }}{{ printf "%s\n" $key}}{{ end }}' \
      <network-id>
    
  • RHEL 7 :

    Error while installing docker-ce:

    Requires: container-selinux >= 2:2.74
    

    Enable RPMS repo :

    subscription-manager repos --enable=rhel-7-server-extras-rpms
    
  • Expose docker daemon:

    Update daemon.json file in /etc/docker:

     {"hosts": ["tcp://0.0.0.0:2375", "unix:///var/run/docker.sock"]}
    

    Add /etc/systemd/system/docker.service.d/override.conf :

     [Service]
     ExecStart=
     ExecStart=/usr/bin/dockerd
    

    Reload the systemd daemon:

     systemctl daemon-reload
    

    Restart docker:

    systemctl restart docker.service
    

    In case of errors like:
    driver failed programming external connectivity on endpoint XXX”

    # Enter below command, it will clear all chains.
    $ sudo iptables -t filter -F
    $ iptables -t filter -X
    # Then restart Docker Service using below comamnd
    $ systemctl restart docker
    
  • Rester docker config when using directlvm

    # Stop & disable docker
    systemctl stop docker
    systemctl disable docker
    
    # remove content
    rm -Rf /var/lib/docker/*
    
    # check vgname
    lsblk
    vgdisplay
    
    # remove VG and LV
    vgremove <VG_NAME>
    
    # restart docker engine
    systemctl enable docker
    systemctl start docker
    
    
  • Remove all dangling images:

    docker rmi -f $(docker images --filter "dangling=true")
    
  • Docker image for Web hello-world:

    https://hub.docker.com/r/tutum/hello-world

    docker pull tutum/hello-world
    
  • docker / docker-compose Error

    Error response from daemon: driver failed programming external connectivity on endpoint ssa-data-lab-denodo-platform-1 (9931d77aef1ed553503b017688806196209ce832039288e4b2b7e9092681d45f):  (iptables failed: iptables --wait -t nat -A DOCKER -p tcp -d 0/0 --dport 9999 -j DNAT --to-destination 172.19.0.2:9999 ! -i br-2e8af153dbd8: iptables: No chain/target/match by that name
    

    Solution:
    Clean iptables chain and restart docker:

    iptables -t filter -F
    iptables -t filter -X
    systemctl restart docker
    

    See https://stackoverflow.com/questions/31667160/running-docker-container-iptables-no-chain-target-match-by-that-name

  • docker-compose on RHEL8:

    curl -L "https://github.com/docker/compose/releases/download/1.23.2/docker-compose-$(uname -s)-$(uname -m)" -o docker-compose
    
    sudo mv docker-compose /usr/local/bin 
    sudo chmod +x /usr/local/bin/docker-compose
    
    sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose 
    
  • Use network “host”:

    docker cli:

    docker run --network=host ...
    

    docker-compose:

    version: "3"
    services:
      web:
        build: .
        container_name: html-container
        network_mode: "host"
    
  • Configure concurrent upload/download:
    in /etc/docker/daemon.json

    {
        "max-concurrent-uploads": 1,
        "max-concurrent-downloads": 1
    }
    

Leave a Reply to Olivier Cancel reply

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