Difference between revisions of "Docker"

From Ever changing code
Jump to navigation Jump to search
Line 103: Line 103:
$ cat Dockerfile <<- EOF #'<<-' heredoc with '-' minus ignores <tab> indent
$ cat Dockerfile <<- EOF #'<<-' heredoc with '-' minus ignores <tab> indent
# Defines our base image
# Defines our base image
FROM ubuntu:latest
FROM ubuntu:latest
MAINTAINER user1 <user1@gmail.com>
MAINTAINER user1 <user1@gmail.com>

Revision as of 15:46, 20 August 2018

Containers taking a world J

Installation

To install the latest version of Docker with curl:

curl -sSL https://get.docker.com/ | sh


CentOS install:

sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
sudo yum clean all
sudo yum install -y --setopt=obsoletes=0 docker-ce-17.03.1.ce-1.el7.centos \
         docker-ce-selinux-17.03.1.ce-1.el7.centos
sudo systemctl start docker
yum-config-manager --disable jenkins #disable source to prevent accidental update


Ubuntu 16.04+ install:

#New docker package is called now docker-ce
sudo apt-get remove docker docker-engine docker.io docker-ce #start fresh
sudo apt-get install apt-transport-https ca-certificates curl software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo apt-key fingerprint 0EBFCD88 #verify
#add the repository 
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
sudo apt-get update
sudo apt-cache madison docker-ce #display available versions
sudo apt-get install docker-ce=<VERSION>
sudo apt-get install docker-ce=18.06.0~ce~3-0~ubuntu


Example of how to run Jenkins docker image


Add your user to docker group to be able to run docker commands without need of sudo

sudo usermod -aG docker yourusername

HTTP proxy

Configure docker if you run behind a proxy server. In this example CNTLM proxy runs on the host machine listening on localhost:3128. This example overrides the default docker.service file by adding configuration to the Docker systemd service file.

First, create a systemd drop-in directory for the docker service:

mkdir /etc/systemd/system/docker.service.d
sudo vi /etc/systemd/system/docker.service.d/http-proxy.conf
[Service]
Environment="HTTP_PROXY=http://proxy.example.com:80/"
Environment="HTTP_PROXY=http://172.31.1.1:3128/" #overrides previous entry
Environment="HTTPS_PROXY=http://172.31.1.1:3128/"
# If you have internal Docker registries that you need to contact without proxying you can specify them via the NO_PROXY environment variable
Environment="NO_PROXY=localhost,127.0.0.1,10.6.96.172,proxy.example.com:80"


Flush changes:

$ sudo systemctl daemon-reload

Verify that the configuration has been loaded:

$ systemctl show --property=Environment docker
Environment=HTTP_PROXY=http://proxy.example.com:80/

Restart Docker:

$ sudo systemctl restart docker

Docker run and basic options

docker run -it --name="mycentos" centos:latest /bin/bash
# -i interactive mode                                \command to execute
# -t bind to the current terminal
# -d disconnect mode, daemon mode, detached mode, running the task in the background
# --name="name_your_container"
# -p flag shows which ports we are using

Docker inspect

Shows current configuration state of a docker container.

docker inspect <container_name> | grep IPAddress
           "SecondaryIPAddresses": null,
           "IPAddress": "172.17.0.3",
                   "IPAddress": "172.17.0.3",

Attach to a docker process

If you are running eg. /bin/bash as a command you can get attached to this running docker process

docker attach mycentos

Mount directory in container

We can mount host directory into docker container so the content will be available from the container

docker run -it -v /mnt/sdb1:/opt/java pio2pio/java8
# syntax: -v /path/on/host:/path/in/container

Build image

Dockerfile

Each line RUN creates a container so if possible, we should join lines so it ends up with less layers.

$ wget jkd1.8.0_111.tar.gz
$ cat Dockerfile <<- EOF #'<<-' heredoc with '-' minus ignores <tab> indent
# Defines our base image

FROM ubuntu:latest
MAINTAINER user1 <user1@gmail.com>
# Define environment variables with syntax ENV space EnvironmetVariable space Value
ENV SHARE /usr/local/share
ENV JAVA_HOME $SHARE/java
# Copy files into the image root folder
ADD jkd1.8.0_111.tar.gz /
# Executes commands in a new layer E.g., it is often used for installing software packages
RUN mv /jkd1.8.0_111.tar.gz $JAVA_HOME 
RUN apt-get update
RUN apt-get upgrade -y
#Add CMD, a single command that will run after the image has been created
CMD ["/bin/bash"]
EOF

Build an image

sudo docker build -t pio2pio/java8 .
# pio2pio -dockerhub username, java8 -image name, . directory where the Dockerfile
sudo docker images  #list images
sudo push pio2pio/java8 #upload the image to DockerHub repository

Create a container

sudo docker run -it pio2pio/java8  
# -i run in interactive mode, because I used CMD=/bin/bash the prompt will be present straight away

List

docker ps -a #list containers
docker image ls #list images
docker images #short form of the command above

References