DevOps Classroom Series – 02/Feb/2020

Docker Essential Command line

  • Executing the commands inside the running container
# Check the contents of root directory in tomcat container

docker container run --name tomcat1 -d tomcat:8

docker container exec tomcat1 ls /

# check the process list
docker container exec tomcat1 ps -eaf

# executing commands in the interactive mode
docker container exec -it tomcat1 /bin/bash
  • Starting and stopping containers
docker container stop tomcat1
docker container ls
# tomcat1 shouldn't be in the list
docker container ls -a

# start the container
docker container start tomcat1


  • Getting to know the instructions led to the creation of docker image
docker image history <imagename>:<tag>

Dockerfile instructions

  • WORKDIR: workdir changes the default directory of the container
    • Create a Dockerfile and build the image with tag demo:0.1
FROM openjdk:8

RUN mkdir /app && cd /app && wget https://war-jar-files.s3-us-west-2.amazonaws.com/spring-petclinic-2.2.0.BUILD-SNAPSHOT.jar
WORKDIR /app
EXPOSE 8080
CMD ["java", "-jar", "spring-petclinic-2.2.0.BUILD-SNAPSHOT.jar"]
* create a container in the interactive mode to check the work directory
docker container run -it demo:0.1 /bin/bash

pwd

  • USER: By default docker container runs as a root user, if you want to create a user and run as that user
USER qt

Docker Single Host networking and Port forwarding

Preview

  • To acces the application running inside the container we establish port forwarding between host port and container port
  • Now lets examine one docker command
docker container run --name tomcatwn1 -d -p 8081:8080 tomcat:8

docker container run --name jenkinswn1 -d -p 8082:8080 jenkins
  • Lets run this command
docker container run --name jenkinswn2 -d -p 8081:8080 jenkins

docker: Error response from daemon: driver failed programming external connectivity on endpoint jenkinswn2 (1b1e
dac2c349d064d7b5a50ff2e883ebb426d1da1b7afd61e6e2935bc5d9d919): Bind for 0.0.0.0:8081 failed: port is already allocated.
  • To resolve the situation try use -P which will allocate any free port of the docker host to docker container
docker container run --name jenkinswn3 -d -P jenkins
  • One scenario for container communications with in the container network
    • By default, docker containers (in linux) run on bridge network
    • Lets try to create two container c1 and c2.
docker container run --name c1 -d alpine sleep 1d
docker container run --name c2 -d alpine sleep 1d

# inspect the network
docker network inspect bridge

# make a note ip addresses of c1 and c2

Preview

* Login into c1 container and ping c2 by name and ipaddress
docker container exec -it c1 /bin/sh

ping -c 4 172.17.0.3
# it works

# ping by name of the continer

ping -c 4 c2
# it fails

  • Observeration: In the default bridge network, ip address is resolved but not the host name

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About continuous learner

devops & cloud enthusiastic learner