Install the docker and Docker Distribution packages. These packages are found in the Extras repo.
yum install -y docker docker-distribution
Enable and start Docker and Docker Distribution.
systemctl enable docker
systemctl enable docker-distribution
systemctl start docker
systemctl start docker-distribution
Configuration
STORAGE
To change the location of the stored docker images amend the rootdirectory config setting in /etc/docker-distribution/registry/config.yml.
vim /etc/docker-distribution/registry/config.yml
rootdirectory: /var/lib/registry
HTTPS/TLS
Create a self signed certificate or purchase a certificate from a trusted CA.
mkdir /certs/
cd /certs/
openssl req -newkey rsa:4096 -nodes -sha256 -keyout server.key -x509 -days 365 -out server.crt
Copy the keys into /etc/docker/certs.d/
cp server.crt server.key /etc/docker/certs.d/.
Edit the registry configuration file and add the following under http.
vim /etc/docker-distribution/registry/config.yml
http:
addr: 192.168.0.1:5000
tls:
certificate: /etc/docker/certs.d/server.crt
key: /etc/docker/certs.d/server.key
Auhentication
There are several methods for authentication but we will use a htpasswd file.
Install httpd-tools
yum install httpd-tools
Create the htpasswd file
mkdir /etc/docker-distribution/registry/auth
chmod 750 /etc/docker-distribution/registry/auth
htpasswd -Bc /etc/docker-distribution/registry/auth/registery_users user_name
Edit the registry configuration file and add the following under Auth.
vim /etc/docker-distribution/registry/config.yml
auth:
htpasswd:
realm: basic-realm
path: /etc/docker-distribution/registry/auth/registery_users
Using the Docker Registry
Pull an image from a remote registry.
docker pull registry.access.redhat.com/rhel7/rhel
List images in Docker
docker images
Load a tarball image into Docker
docker load -i rhel-server-docker-7.2.x86_64.tar.gz
To push an image into your local repository you need to tag it with your registry information first.
docker tag bef54b8f8a2f localhost:5000/docker_image
docker push localhost:5000/docker_image
Run a quick command
docker run -v /usr/sbin:/usr/bin --rm rhel /usr/sbin/ip addr show eth0
# -v Mount host directory to make it available to the container
# --rm Remove the container after execution of the command
docker run -v /usr/sbin:/usr/sbin --name=myipaddr rhel /usr/sbin/ip addr show eth0
# --name Names the container so it can be started again by name next time.
docker start -i myipaddr
# -i Attach container's STDIN
View the docker file in the container
docker run --rm localhost:5000/rhel7 ls /root/buildinfo
Run a shell inside the container. From here you can use yum and other commands
docker run --name=mybash -it rhel /bin/bash
Show containers
docker ps -a
Bind mount log files. This makes the containers messages available to the host
docker run --rm --name=test_log3 -v /dev/log:/dev/log server2.grow4.co.uk:5000/rhel7 logger "Testing logging to the host"
List containers
docker ps # Runninig containers
docker ps -a # All containers
Inspect the metadata of an existing container
docker inspect
docker inspect --format='{{.NetworkSettings.Networks.bridge.IPPrefixLen}}' 8bfde4062140
Investigating within a running Docker container
docker exec will run a command in a container without interrupting the application the container is running.
docker exec -it myrhel_httpd /bin/bash
If you ran a container, but didn’t remove it (--rm), that container is stored on your local system and ready to run again. To start a previously run container that wasn’t removed, use the start option. To stop a running container, use the stop option
Start a non-interactive user
docker start container_name
Start an interactive container
docker start -a -i container_name
# -a attache
# -i interactive
Stopping containers
docker stop container_name
docker kill --signul="SIGHUP"
Removing containers
docker rm container_name
docker rm container_name1 container_name2
# Remove all containers
docker rm $(docker ps -aq)