Dockerfile Instructions
- USER: user instruction sets the username for any subsequent RUN, CMD and ENTRYPOINT instructions in Dockerfile. Ensure user is created before USER instruction in dockerfile. Default username is root
- WORKDIR: workdir instruction sets the working directory for any subsequent RUN, CMD and ENTRYPOINT instructions in Dockerfile and the default value would be /
- ARG: this instruction allows variables t be passed while building image
- Build image using the docker file below
FROM openjdk:8 ARG url=https://referenceappkhaja.s3-us-west-2.amazonaws.com/spring-petclinic-2.2.0.BUILD-SNAPSHOT.jar RUN wget ${url} EXPOSE 8080 CMD ["java", "-jar", "spring-petclinic-2.2.0.BUILD-SNAPSHOT.jar"]
- while building the image the value of the url can be changed
docker image build --build-arg url=<otherurl> -t spc:0.1 .
- ARG is available only while building the docker image
- ENV: This instruction will set the environmental varible for all the subsequent instruction and also in containers
- Build the image
FROM openjdk:8 ARG url=https://referenceappkhaja.s3-us-west-2.amazonaws.com/spring-petclinic-2.2.0.BUILD-SNAPSHOT.jar ENV myfilename=spring-petclinic-2.2.0.BUILD-SNAPSHOT.jar RUN wget ${url} EXPOSE 8080 CMD ["java", "-jar", "spring-petclinic-2.2.0.BUILD-SNAPSHOT.jar"]
- Environmental variables can be replaced while creating container
Docker Container
- Docker Container is created from Docker Image. Docker Image is collection of Image Layers. Now while docker container is created all of the Image Layers are mounted as one file system
- States of Docker container
- Running
- Exited/Stopped
- Pause
- Terminated (Deleted)
- When docker container is created CMD/ENTRYPOINT gets executed and as long as this command is running the container will be in running state.
- Create a spc container from the Dockerfile
FROM openjdk:8
ARG url=https://referenceappkhaja.s3-us-west-2.amazonaws.com/spring-petclinic-2.2.0.BUILD-SNAPSHOT.jar
RUN wget ${url}
EXPOSE 8080
CMD ["java", "-jar", "spring-petclinic-2.2.0.BUILD-SNAPSHOT.jar"]
- Run the docker using the following command
docker container run -d spc:1.0
docker container ls
docker container run -d spc:1.0 echo hello
docker container ls
- Every Docker container will have unique container name and id. if the names are not passed while creating, docker engine gives adhoc names
- Creating container with name
docker container run --help
- To stop/start container
docker container stop <c-id or c-name>
docker container start <c-id or c-name>
- To pause container
docker container pause <c-id or c-name>
docker container unpause <c-id or c-name>
- To view the status
docker container ls # This will show only running containers
docker container ls -a