Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Docker Chit Chat

Docker Chit Chat

General introduction to Docker with common use cases. Highlight the difference between Docker and Docker Compose. Cheatsheet with useful commands.

Davide Fantuzzi

April 12, 2021
Tweet

More Decks by Davide Fantuzzi

Other Decks in Programming

Transcript

  1. DOCKER IS NOT A VM • Virtualize software, not hardware

    • It’s faster! At least mom told my so... • It’s Schrödinger simple
  2. IMAGES vs CONTAINERS • An image is a template, containing

    a series of instruction • A container is the executable instance, result such instructions IMAGE CONTAINER CONTAINER CONTAINER
  3. IMAGES vs CONTAINERS • An image is a template, containing

    a series of instruction • A container is the executable instance, result such instructions • Images can be extended BASE_IMAGE IMAGE_ONE IMAGE_TWO FROM utnaf/BASE_IMAGE FROM utnaf/IMAGE_ONE
  4. HOW TO DOCKERFILE? FROM node:14.15.0 WORKDIR /var/www COPY package*.json ./

    RUN npm install COPY . ./ EXPOSE 3000 CMD ["npm", "start"]
  5. HOW TO DOCKERFILE? FROM node:14.15.0 WORKDIR /var/www COPY package*.json ./

    RUN npm install COPY . ./ EXPOSE 3000 CMD ["npm", "start"] Starting image
  6. HOW TO DOCKERFILE? FROM node:14.15.0 WORKDIR /var/www COPY package*.json ./

    RUN npm install COPY . ./ EXPOSE 3000 CMD ["npm", "start"] Starting image Set the base workdir
  7. HOW TO DOCKERFILE? FROM node:14.15.0 WORKDIR /var/www COPY package*.json ./

    RUN npm install COPY . ./ EXPOSE 3000 CMD ["npm", "start"] Starting image Set the base workdir Copy the files for dependencies installation
  8. HOW TO DOCKERFILE? FROM node:14.15.0 WORKDIR /var/www COPY package*.json ./

    RUN npm install COPY . ./ EXPOSE 3000 CMD ["npm", "start"] Starting image Set the base workdir Copy the files for dependencies installation Install dependencies
  9. HOW TO DOCKERFILE? FROM node:14.15.0 WORKDIR /var/www COPY package*.json ./

    RUN npm install COPY . ./ EXPOSE 3000 CMD ["npm", "start"] Starting image Set the base workdir Copy the files for dependencies installation Install dependencies Copy the rest of the files
  10. HOW TO DOCKERFILE? FROM node:14.15.0 WORKDIR /var/www COPY package*.json ./

    RUN npm install COPY . ./ EXPOSE 3000 CMD ["npm", "start"] Starting image Set the base workdir Copy the files for dependencies installation Install dependencies Copy the rest of the files Expose the port that the app is listening to
  11. HOW TO DOCKERFILE? FROM node:14.15.0 WORKDIR /var/www COPY package*.json ./

    RUN npm install COPY . ./ EXPOSE 3000 CMD ["npm", "start"] Starting image Set the base workdir Copy the files for dependencies installation Install dependencies Copy the rest of the files Expose the port that the app is listening to Entrypoint command
  12. HOW TO DOCKERFILE? FROM node:14.15.0 WORKDIR /var/www COPY package*.json ./

    RUN npm install COPY . ./ EXPOSE 3000 CMD ["npm", "start"] Starting image Set the base workdir Copy the files for dependencies installation Install dependencies Copy the rest of the files Expose the port that the app is listening to Entrypoint command docker build --tag my-image:1.0.0 . docker run -p "3000:3000" my-image:1.0.0
  13. docker run -d --network myApp --name frontend my-image:1.0.0 docker run

    -d --name neo4j \ -p "7474:7474" -p "7687:7687" \ -e NEO4J_AUTH='neo4j/password' \ -e NEO4JLABS_PLUGINS='["apoc","graph-data-science"]' \ -v 'neo4j-data:/data' \ --network myApp \ neo4j:4.2
  14. docker run -d --network myApp --name frontend my-image:1.0.0 docker run

    -d --name neo4j \ -p "7474:7474" -p "7687:7687" \ -e NEO4J_AUTH='neo4j/password' \ -e NEO4JLABS_PLUGINS='["apoc","graph-data-science"]' \ -v 'neo4j-data:/data' \ --network myApp \ neo4j:4.2
  15. docker run -d --network myApp --name frontend my-image:1.0.0 docker run

    -d --name neo4j \ -p "7474:7474" -p "7687:7687" \ -e NEO4J_AUTH='neo4j/password' \ -e NEO4JLABS_PLUGINS='["apoc","graph-data-science"]' \ -v 'neo4j-data:/data' \ --network myApp \ neo4j:4.2
  16. docker run -d --network myApp --name frontend my-image:1.0.0 docker run

    -d --name neo4j \ -p "7474:7474" -p "7687:7687" \ -e NEO4J_AUTH='neo4j/password' \ -e NEO4JLABS_PLUGINS='["apoc","graph-data-science"]' \ -v 'neo4j-data:/data' \ --network myApp \ neo4j:4.2
  17. docker run -d --network myApp --name frontend my-image:1.0.0 docker run

    -d --name neo4j -p "7474:7474" -p "7687:7687" \ -e NEO4J_AUTH='neo4j/password' \ -e NEO4JLABS_PLUGINS='["apoc","graph-data-science"]' \ -v 'neo4j-data:/data' \ --network myApp \ neo4j:4.2
  18. docker run -d --network myApp --name frontend my-image:1.0.0 docker run

    -d --name neo4j -p "7474:7474" -p "7687:7687" \ -e NEO4J_AUTH='neo4j/password' \ -e NEO4JLABS_PLUGINS='["apoc","graph-data-science"]' \ -v 'neo4j-data:/data' \ --network myApp \ neo4j:4.2
  19. docker run -d --network myApp --name frontend my-image:1.0.0 docker run

    -d --name neo4j -p "7474:7474" -p "7687:7687" \ -e NEO4J_AUTH='neo4j/password' \ -e NEO4JLABS_PLUGINS='["apoc","graph-data-science"]' \ -v 'neo4j-data:/data' \ --network myApp \ neo4j:4.2
  20. docker run -d --network myApp --name frontend my-image:1.0.0 docker run

    -d --name neo4j -p "7474:7474" -p "7687:7687" \ -e NEO4J_AUTH='neo4j/password' \ -e NEO4JLABS_PLUGINS='["apoc","graph-data-science"]' \ -v 'neo4j-data:/data' \ --network myApp \ neo4j:4.2
  21. docker run -d --network myApp --name frontend my-image:1.0.0 docker run

    -d --name neo4j -p "7474:7474" -p "7687:7687" \ -e NEO4J_AUTH='neo4j/password' \ -e NEO4JLABS_PLUGINS='["apoc","graph-data-science"]' \ -v 'neo4j-data:/data' \ --network myApp \ neo4j:4.2
  22. DOCKER COMPOSE TO THE RESCUE • Tool for multi-container application

    • YAML based • Single command • Works in all environments: dev, prod, CI, etc...
  23. DOCKER COMPOSE TO THE RESCUE version: "2" services: frontend: image:

    my-image:1.0.0 networks: - myApp neo4j: image: neo4j:4.2 networks: - myApp ports: - 7474:7474 - 7687:7687 environment: - NEO4J_AUTH=neo4j/password - NEO4JLABS_PLUGINS=["apoc","graph-data-science"] volumes: - neo4j:/data networks: myApp: volumes: neo4j:
  24. DOCKER COMPOSE TO THE RESCUE version: "2" services: frontend: image:

    my-image:1.0.0 networks: - myApp neo4j: image: neo4j:4.2 networks: - myApp ports: - 7474:7474 - 7687:7687 environment: - NEO4J_AUTH=neo4j/password - NEO4JLABS_PLUGINS=["apoc","graph-data-science"] volumes: - neo4j:/data networks: myApp: volumes: neo4j: docker run -d --network myApp --name frontend my-image:1.0.0 docker run -d --name neo4j -p "7474:7474" -p "7687:7687" \ -e NEO4J_AUTH='neo4j/password' \ -e NEO4JLABS_PLUGINS='["apoc","graph-data-science"]' \ -v 'neo4j-data:/data' \ --network myApp \ neo4j:4.2
  25. DOCKER COMPOSE TO THE RESCUE docker run -d --network myApp

    --name frontend my-image:1.0.0 docker run -d --name neo4j -p "7474:7474" -p "7687:7687" \ -e NEO4J_AUTH='neo4j/password' \ -e NEO4JLABS_PLUGINS='["apoc","graph-data-science"]' \ -v 'neo4j-data:/data' \ --network myApp \ neo4j:4.2 docker-compose up -d
  26. IMAGE Dockerfile docker build . docker run [IMAGE] CONTAINER CONTAINER

    CONTAINER CONTAINER docker-compose.yml docker-compose up
  27. DOCKER # build the image from the given dockerfile docker

    build . docker build -t [image_name]:[version] # tag the image docker build -t [username]/[image_name]:[version] # add a DockerHub repository # run the image docker run [...options] [image_name]:[version] # DockerHub commands docker login docker push [username]/[image_name] docker pull [username]/[image_name]
  28. DOCKER HUB docker push utnaf/myApp docker pull utnaf/myApp docker pull

    utnaf/myApp docker pull utnaf/myApp docker pull utnaf/myApp docker pull utnaf/myApp
  29. PROS • Docker-based deploy • Avoid passing the Dockerfile around

    • Avoid building the image on every machine • Has webhooks
  30. DOCKER COMPOSE # start and stop the containers docker-compose up

    docker-compose up -d # start the container in detached mode docker-compose stop # stop the containers docker-compose down # stop **and remove** the containers # debug containers docker-compose logs # print the last logs docker-compose logs -f # print logs in follow mode docker-compose logs [container] # print the logs of the specified container docker-compose exec [container] [command] # run the command in the container docker-compose exec myApp bash # enter a bash session on the myApp container docker-compose ps # show the current status of the containers
  31. DOCKER COMPOSE # start and stop the containers docker-compose up

    docker-compose up -d # start the container in detached mode docker-compose stop # stop the containers docker-compose down # stop **and remove** the containers # debug containers docker-compose logs # print the last logs docker-compose logs -f # print logs in follow mode docker-compose logs [container] # print the logs of the specified container docker-compose exec [container] [command] # run the command in the container docker-compose exec myApp bash # enter a bash session on the myApp container docker-compose ps # show the current status of the containers Available on docker command as well