1. Working with command line arguments
run docker images:
docker run alpine
docker run -it alpine
docker run -it alpine sh
docker run -it ubuntu bash
docker run -it openjdk
docker run -it openjdk sh
docker run -it --rm --name my-maven-project -v "$(pwd)":/usr/src/mymaven -w /usr/src/mymaven maven:3.3-jdk-8 mvn clean install
see docker containers:
docker ps
docker ps -a
remove docker container:
docker rm ad0
Inspect dc:
docker inspect ad0
run docker container and detach cli:
docker run -d ubuntu bash
attach cli to docker container:
docker exec -it id bash
attach a folder to a path inside docker:
docker run -it -v path/on/local:/path/on/container ubuntu bash
stop a container:
docker stop id
expose a port:
docker run -p 8080:8080 nginx
2. Creating your own image with Dockerfile
create an image with dockerfile:
From alpine
docker build -t my-image-name .
docker run -it my-image-name
See images:
docker images
Run commands in images:
FROM alpine
RUN touch test-file
FROM alpine
CMD touch test-file
docker run -it my-image-name sh
give multiple commands :
FROM alpine
CMD touch test-file && sh
chain RUN commands:
FROM alpine
RUN mkdir test; \
cd test; \
touch test-file
specify the working directory with the WORKDIR command
use the COPY command:
FROM alpine
COPY app.jar .
copy a jar file for spring boot:
FROM openjdk:8-jdk-alpine
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} app.jar
use the exec way to run commands so that they will not be children of a shell:
FROM alpine
RUN [“touch” “test-file”]
To run a project its recommended to use ENTRYPOINT instead of CMD:
ENTRYPOINT ["java","-jar","/app.jar"]
set environment variables using the ENV command(this will overwrite the properties in application.yml in spring boot)
Run a spring boot app in Docker:
FROM openjdk:8-jdk-alpine
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} app.jar
ENV SPRING_DATA_MONGODB_HOST=198.1.1.1
ENV MQTT-CLIENT_SERVER-URI=199.1.1.1:1883
ENTRYPOINT ["java","-jar","/app.jar"]
To run this use:
docker build -t backend .
docker run -p 8080:8080 backend
3. The third way of using docker is docker compose
version: ‘3’
services:
touchexample:
image: alpine
springexample:
image: openjdk:8-jdk-alpine
ports:
8080:8080
environment:
SPRING_DATA_MONGODB_HOST=198.1.1.1
MQTT-CLIENT_SERVER-URI=199.1.1.1:1883
To start everything use
docker-compose up
use your own image:
version: ‘3’
services:
app:
container-name: backend
build: ./
ports:
“8080:8080”
environment:
SPRING_DATA_MONGODB_HOST=198.1.1.1
MQTT-CLIENT_SERVER-URI=199.1.1.1:1883
To specify an order in which the containers are built you can use the depends_on clause.