Docker for devs with hands-on| Part-4

  • Introduction to Dockerfile.
  • Generation of DockerImage from Dockerfile.
  • Launching Docker container from DockerImage.
  • Pre-defining instructions from Dockerfile.
  • Copying files from local to Container.
  • Executing simple-service in container using Dockerfile.
  • Difference between ADD & COPY operations.
  • Launching simple tomcat within Docker-container.
  • Deploying full fledged WAR file (Spring based WebApp) inside the Docker container containing tomcat based docker image.
  • Deploying full fledged JAR file (Spring Boot based App) inside the Docker container containing JDK based docker image.
FROM ubuntu:latest
MAINTAINER Aditya Goel "adityagoel123@gmail.com"
RUN apt-get update && apt-get install -y openjdk-8-jdk
CMD ["/bin/bash"]
docker image build -t image-from-docker-file .
docker images
docker container ps
docker container run -ti image-from-docker-file
docker container ps -a
FROM ubuntu:latest
MAINTAINER Aditya Goel "adityagoel123@gmmail.com"
RUN apt-get update && apt-get install -y openjdk-8-jdk
COPY simple-java.jar /usr/local/bin/
CMD ["/bin/bash"]
FROM ubuntu:latest
MAINTAINER Aditya Goel "adityagoel123@gmmail.com"
RUN apt-get update && apt-get install -y openjdk-8-jdk
WORKDIR /usr/local/bin/
COPY simple-java.jar .
CMD ["/bin/bash"]
java -jar <NAME_OF_JAR.jar>
docker image build -t image-from-docker-file .
docker images
docker container run -it <NAME_OF_DOCKER_IMAGE>
  • We had ran the container with “-i” option which means interactive mode.
  • We had ran the container with “-t” option which means, we shall connect to the container’s terminal as well.
  • In the fresh container that we just launched, observe that JDK is already installed, as suggested in the Dockerfile itself.
  • In the fresh container , also observe that a JAR file has also been copied, because the same has been mentioned in the Dockerfile.
docker container ls -a
docker container start <container_id>
docker container exec -it <container_id> bash
java -jar <NAME_OFJAR_FILE.jar>
FROM ubuntu:latest
MAINTAINER Aditya Goel "adityagoel123@gmmail.com"
RUN apt-get update && apt-get install -y openjdk-8-jdk
WORKDIR /usr/local/bin/
COPY simple-java.jar .
CMD ["java", "-jar", "simple-java.jar"]
docker container ls
docker container stop <CONTAINER_ID>
docker container ls -a
docker stop <Container-IDs>
docker container ls -a
docker image build -t image-from-docker-file .docker images
docker container run -it <NAME_OF_DOCKER_IMAGE>
docker container ps -a
docker container start <CONTAINER-ID>docker container logs <CONTAINER-ID>
  • ADD command can work with remote-urls.
  • It can also do things like unzipping or unpacking archives.
  • It looks like ADD is generally more useful and flexible.
  • COPY command is preferred because it’s just simpler and is more obvious what copy is going to do.
  • If you’re working with an image that has CMD, if you wish, you can change the command that gets executed. So, I could instead of running that Java service, I could just get dropped inside the bash terminal of Docker container. And that’s going to start the container from the image, but it overrides that default command. And as you can see, I’m now dropped onto the command line and I could start doing diagnostics or whatever I want to do.
  • The same isn’t true of entry points. Now, this is a hard coded command, we rerun the build and then run a container from it and notice
FROM ubuntu:latest
MAINTAINER Aditya Goel "adityagoel123@gmmail.com"
RUN apt-get update && apt-get install -y openjdk-8-jdk
WORKDIR /usr/local/bin/
ADD simple-java.jar .
ENTRYPOINT ["java", "-jar", "simple-java.jar"]
  • First, we install JAVA/JDK inside our ‘ubuntu’ docker-container.
  • Next, we install tomcat inside our ‘ubuntu’ docker-container.
docker image pull tomcat:8.5.73-jdk8-openjdk
docker images
docker container run -p 8081:8080 -ti tomcat:8.5.73-jdk8-openjdk
docker container exec -it da31ab329d77 bash
docker container stop <container_id>
docker container rm <container_id>
docker ps -a
docker images
docker image rm <Image_ID>
docker images
FROM tomcat:8.5.73-jdk8-openjdk
MAINTAINER Aditya Goel "adityagoel123@gmail.com"
CMD ["catalina.sh", "run"]
RUN mv /usr/local/tomcat/webapps.dist/* /usr/local/tomcat/webapps/
docker image build -t tomcat-df-aditya .
docker images
docker container run -it -p 8086:8080 <NAME_OF_DOCKER_IMAGE>
docker container exec -it <container_id> bash
FROM tomcat:8.5.73-jdk8-openjdk
MAINTAINER Aditya Goel "adityagoel123@gmail.com"
EXPOSE 8080
RUN mv /usr/local/tomcat/webapps.dist/* /usr/local/tomcat/webapps/
WORKDIR /usr/local/tomcat/webapps/
COPY fleetman1.war .
RUN mv ./fleetman1.war ./ROOT.jar
ENV JAVA_OPTS="-Dspring.profiles.active=docker-demo"
CMD ["catalina.sh", "run"]
  • EXPOSE command is just an indication that, tomcat within our container would be exposed on port 8080.
  • With ENV JAVA_OPTS, we are setting the Java environment variables inside the container.
docker image build -t tomcat--webapp-aditya .
docker images
docker container run -itd -p 8088:8080 <NAME_OF_DOCKER_IMAGE>
docker container exec -it 0c bash
docker containers ps
docker container logs 0c
FROM openjdk:8u312-jdk
MAINTAINER Aditya Goel "adityagoel123@gmail.com"
EXPOSE 8080
WORKDIR /usr/local/bin/
COPY fleetman-0.0.1-SNAPSHOT.jar webapp.jar
CMD ["java", "-Dspring.profiles.active=docker-demo", "-jar", "webapp.jar"]
docker image build -t webapp-springboot-aditya .
docker images
docker container run -itd -p 8089:8080 <NAME_OF_DOCKER_IMAGE>
docker container exec -it d1 bash
docker containers ps

--

--

--

Software Engineer for Big Data distributed systems

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Integrating LDAP Server with AssetSonar

Part 2: An easy way to automatically manage the AWS SG with Terraform and Python…

Nervos x Ledger Nano S Tutorial

OOP - Differing Perspectives of the Interviewer and the Interviewee

AWS/Terraform Workshop #7: AWS ECS insights and troubleshooting

How To Host Your Project On Github: A Quick Guide For Beginners

Move Git Repositories From One to Other Organization in Azure DevOps

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
aditya goel

aditya goel

Software Engineer for Big Data distributed systems

More from Medium

Getting Started with Docker Containers pt.2

Orchestrating multiple dockers with Swarm

[1/4] Let’s Create a Spring Boot App With MySQL, Docker, Docker Compose

Getting Started with Docker — Part 01