Docker
🎳

Docker

🚚 Docker backup and restore DB

PostgreSQL
Backup your databases
Bash
docker exec -t your-db-container pg_dumpall -c -U postgres > dump_`date +%d-%m-%Y"_"%H_%M_%S`.sql
Restore your databases
Bash
cat your_dump.sql | docker exec -i your-db-container psql -U postgres
Mysql
Backup your databases
Bash
docker exec CONTAINER /usr/bin/mysqldump -u root --password=root DATABASE > backup.sql
Restore your databases
Bash
cat backup.sql | docker exec -i CONTAINER /usr/bin/mysql -u root --password=root DATABASE

Container operations

List all containers
Bash
docker ps -a
Stop all running docker containers
Bash
docker kill $(docker ps -q)
List all downloaded images
Bash
docker images
Remove image/containers
Bash
docker rm/rmi
Remove all images
Bash
docker rmi $(docker images -q)
Remove all dangling images (it occurs when building a new image and giving it a tag that already exists)
Bash
docker image prune
Remove all unused images
Bash
docker image prune -a
Search Images
Bash
docker search mysql
Show environment variables
Bash
docker inspect -f '{{range $index, $value :=.Config.Env}}{{println $value}}{{end}}' imageName
Expose ports
Bash
docker run -d -p 80:80 docker/wordPress
Check logs
Bash
docker logs container-id/container-name
Enter into the container
Bash
docker exec -it CONTAINER_ID /bin/bash
Run container and enter dash
Bash
docker container run -it ubuntu:latest /bin/bash
Check processes in a container
Bash
docker top CONTAINER_ID
Check using space
Bash
docker system df

docker search

only official repos
Bash
$ docker search alpine --filter "is-official=true"

docker search

check details about an images
Bash
$ docker image inspect ubuntu:latest

Dockerfile

The difference between copy and add?
In a Dockerfile, the COPY instruction copies files from the build context into the container, while ADD does the same thing but also has some extra features such as the ability to download files and extract tar files. In general, it is recommended to use COPY for copying files, since it is simpler and less prone to unexpected behavior.
 
Java
Docker
# Use the official OpenJDK image as the base image
FROM openjdk:11-jre-slim

# Set the working directory
WORKDIR /app

# Copy the JAR file into the container
COPY target/my-spring-boot-app-*.jar my-spring-boot-app.jar

# Expose the server port
EXPOSE 8080

# Run the Spring Boot JAR package
ENTRYPOINT ["java", "-jar", "my-spring-boot-app.jar"]
Node.js
Docker
FROM alpine
RUN apk add --update nodejs npm
COPY . /src
WORKDIR /src
RUN  npm install
EXPOSE 8080
ENTRYPOINT ["node", "./server.js"]
Build the image
Bash
docker image build -t test:latest .
Run the image
Bash
docker container run -d --name web1 --publish 8080:8080 test:latest
Docker container status can have the following values:
  • created: The container has been created, but it has not been started yet.
  • restarting: The container is currently restarting.
  • running: The container is currently running.
  • paused: The container has been paused.
  • exited: The container has stopped running.
  • dead: The container is no longer running and cannot be restarted.
These statuses can be viewed using the docker ps -a command, which will display a list of all containers and their statuses.
 

Example

Example 1
The Dockerfile has three distinct build stages. Stage 0 is called storefront, stage 1 is called appserver, and stage 2 is called production. Each stage pulls a different image and performs various tasks. An important thing to note is that COPY --from instructions are used to only copy production-related application code from the images built by the previous stages. They do not copy build artifacts that are not needed for production.
Docker
FROM node:latest AS storefront
WORKDIR /usr/src/atsea/app/react-app
COPY react-app .
RUN npm install
RUN npm run build

FROM maven:latest AS appserver
WORKDIR /usr/src/atsea
COPY pom.xml .
RUN mvn -B -f pom.xml -s /usr/share/maven/ref/settings-docker.xml dependency:resolve
COPY . .
RUN mvn -B -s /usr/share/maven/ref/settings-docker.xml package -DskipTests

FROM java:8-jdk-alpine AS production
RUN adduser -Dh /home/gordon gordon
WORKDIR /static
COPY --from=storefront /usr/src/atsea/app/react-app/build/ .
WORKDIR /app
COPY --from=appserver /usr/src/atsea/target/AtSea-0.0.1-SNAPSHOT.jar .
ENTRYPOINT ["java", "-jar", "/app/AtSea-0.0.1-SNAPSHOT.jar"]
CMD ["--spring.profiles.active=postgres"]
Example 2
To create a setup with a Node.js app and Nginx as a load balancer using Docker, you will need two separate Dockerfiles and a docker-compose.yml file to orchestrate the containers. We'll create a simple Node.js app first, then write Dockerfiles for both the app and Nginx, and finally, create the docker-compose.yml file.
Create a simple Node.js app:
Create a new directory for your app, and inside that directory, create the following files:
JavaScript
const http = require('http');
const os = require('os');

const hostname = '0.0.0.0';
const port = process.env.PORT || 3000;

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end(`Hello World! This is served from ${os.hostname()}\n`);
});

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});
app.js
JSON
{
  "name": "node-app",
  "version": "1.0.0",
  "description": "A simple Node.js app",
  "main": "app.js",
  "scripts": {
    "start": "node app.js"
  },
  "dependencies": {
    "express": "^4.17.1"
  }
package.json
Create a Dockerfile for the Node.js app:
In the same directory, create a file named Dockerfile for the Node.js app:
Docker
# Use the official Node.js image as the base
FROM node:14-alpine

# Set the working directory
WORKDIR /usr/src/app

# Copy package.json and package-lock.json
COPY package*.json ./

# Install dependencies
RUN npm install

# Copy the app source code
COPY . .

# Expose the app port
EXPOSE 3000

# Start the app
CMD [ "npm", "start" ]
Create a Dockerfile for Nginx:
Create a new directory for Nginx, and inside that directory, create a file named Dockerfile for Nginx:
Docker
# Use the official Nginx image as the base
FROM nginx:1.21-alpine

# Remove the default Nginx configuration file
RUN rm /etc/nginx/conf.d/default.conf

# Copy the custom Nginx configuration file
COPY nginx.conf /etc/nginx/conf.d/
Create an Nginx configuration file:
In the Nginx directory, create a file named nginx.conf:
Erlang
http {
  upstream nodeapp {
    least_conn;
    server nodeapp1:3000;
    server nodeapp2:3000;
  }

  server {
    listen 80;

    location / {
      proxy_pass http://nodeapp;
      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection 'upgrade';
      proxy_set_header Host $host;
      proxy_cache_bypass $http_upgrade;
    }
  }
}
  1. Create a docker-compose.yml file:
In the root directory, create a docker-compose.yml file:
Docker
version: '3.9'
services:
  nodeapp1:
    build: ./node-app
    container_name: nodeapp1
    environment:
      - PORT=3000
		restart_policy:
      condition: always | unless-stopped | on-failuregrep
  nodeapp2:
    build: ./node-app
    container_name: nodeapp2
    environment:
      - PORT=3000
  nginx:
    build: ./nginx