Dockerfile and Compose

Notes Single

Dockerfile

It is a file that contains the script with the steps to execute the image.

# Create the image from an Alpine Linux
FROM alpine:3.10

# -Install Python 3 y pip
RUN apk add python3-dev \
&& pip3 install --upgrade pip

# Create the directory at the container
WORKDIR /app

# Copy whole content of this folder to "/app" at the server
COPY . /app

# Install the packages indicated in requirements.txt
RUN pip3 --no-cache-dir install -r requirements.txt

# Commands to run when starting the container
CMD ["python3","src/app.py"]

Docker compose

Mechanism that allows with a single command to create/download images, create a network with the containers and run the containers, all these procedures based on the instructions of a "docker-compose.ylm" file that must be located in the root of the project.

Example 1:

version: "3.9"
services:
  chanchito:
    build: .
    ports:
      - "3000:3000"
    links:
      - monguito
  monguito:
    image: mongo
    ports:
      - "27017:27017"
    environment:
      - MONGO_INITDB_ROOT_USERNAME=nico
      - MONGO_INITDB_ROOT_PASSWORD=password
    volumes:
      - mongo-data:/data/db
      # mysql -> /var/lib/mysql
      # postgres -> /var/lib/postgresql/data

volumes:
  mongo-data:

Example 2:

services:
  app:
    image: node:18-alpine
    command: sh -c "yarn install && yarn run dev"
    ports:
      - 3000:3000
    working_dir: /app
    volumes:
      - ./:/app
    environment:
      MYSQL_HOST: mysql
      MYSQL_USER: root
      MYSQL_PASSWORD: secret
      MYSQL_DB: todos

  mysql:
    image: mysql:8.0
    volumes:
      - todo-mysql-data:/var/lib/mysql
    environment:
      MYSQL_ROOT_PASSWORD: secret
      MYSQL_DATABASE: todos

volumes:
  todo-mysql-data:

Run the container

docker compose up

Revert docker compose

docker compose down

Thanks for reading :)
I invite you to continue reading other entries and visiting us again soon.

Related Posts: