Training - Docker

Last Updated on: August 17, 2021 pm

Training - Docker

Log in to Linux Server

  1. Turn on the personal linux server on https://student.conygre.com/
  2. Open a terminal and do ssh grads@devopsnama23.conygre.com
  3. Password: c0nygre
  4. run docker run -d -p 8081:8080 nicktodd40/compactdiscs - to launch a docker container
  5. Go to a browser - type in devopsnama23.conygre.com:8081

Docker Overview

Docker containers abstract some of the Linux OS capabilities and the virtualization is done at the OS layer.

Docker Abstraction

Docker containers run in separate namespaces sharing the kernel of the host OS.

  • Makes the containers much faster to launch than a VM
  • Means that containers on the same host are NOT totally isolated – more on that later

Run Docker

docker --version check if installed.

DockerHub

Images are commonly used from DockerHub Repository.

Launch a Container

docker run tomcat:9 Run a container from the Tomcat9 image from the dockerHub.

More useful option:

docker run –d –p 8081:8080 tomcat:9.0

-d Flag

To put the container in the background, use the –d flag

Port Binding

The -p flag allows you to bind ports between Docker Host and the container port.

In the example above:

Port 8081 on the Docker Host is now going to forward to the Container port 8080

What is running?

docker ps -a - show all the running docker containers

Starting & Stopping Containers

docker stop [id]

docker start [id]

The [id] is the first couple of char from the Container ID.

Destroy Containers

docker rm -f [id]

Docker Images

  • Docker images are the template from which we launch container
  • Each image is based on a parent image
  • Each image therefore consists of multiple layers
  • Each layer contains directories and files

Building the Image

docker build -t trades .

-t - you can can provide a tag for the image

  • To enable easy identification
  • In order to push to a repository (more on that later)

. - The period (.) is the directory containing the Dockerfile

Dockerfile

1
2
3
4
FROM openjdk:8
ADD target/SpringBootExample-0.0.1-SNAPSHOT.jar app.jar
EXPOSE 8080
ENTRYPOINT [ "sh", "-c", "java -jar /app.jar" ]
  • FROM - specifies the base image, this base image will be copied down from Artifactory onto your local machine
  • ADD - Files to copy into the image from the local machine
  • EXPOSE - specify which port to expose to the outside world
  • RUN - Command to run at build
  • WORKDIR - Working directories for subsequent commands
  • ENV - Environment Variables
  • CMD - used when you might want to override the command that is used when you launch the container - like ["npm", "start"]
  • ENTRYPOINT - used for images that are running server software where you are unlikely to want to override what it is doing

Manage Images

docker images

Remove Images

docker rmi [imageName]

Sharing Images

docker login

docker push

docker pull

Appendix

Pull Jenkins Server

docker run -p 8081:8080 jenkins/jenkins:lts-jdk11

Then open devopsnama23.conygre.com:8081 to open Jenkins in docker container - because of pulled image

devopsnama23.conygre.com:8080 also a Jenkins running - directly on the Linux server.