Dockerfile


➡ Docker Hub : find containers images and their versions

create dockerfile in your project, an example with node.js version 14.5.0 buster

FROM node:buster
# create dir in the container
WORKDIR /usr/src/smartbrain_API
# copy all the file from the project to the container
COPY ./ ./

RUN npm install

CMD ["/bin/bash"]

Dockerfile reference


Docker Commands


???? Create container:

docker build -t firstcontainer .

???? Access the container:

docker run -it firstcontainer

???? Run Docker on the background

docker run -it -d firstcontainer

???? Show all the containers that are running

docker ps

???? Access a container from the list

docker exec -it [container id] bash

???? Go out of the container

exit

???? Stop the container from running

docker stop [container id]

???? Run the container on port

docker run -it -p 3000:3000 firstcontainer

???? Then run npm start

???? List all the containers

docker container ls -a

???? Remove a container

docker container rm [container id]

???? Remove all stoped containers

docker container prune

???? Removing Docker Images

When you download a Docker image, it is kept on the server until you manually remove it. first, find the image id by this command docker image ls then

docker image rm [image id]

Learn more about removing commands: How To Remove Docker Containers, Images, Volumes, and Networks


Docker Compose


First, we need to create a docker-compose.yml file

version: '3.8'

services: 
  smart-brain-api:
    container_name: backend
    # image: node:buster
    build: ./ #it use the image from dockerfile
    command: npm start
    working_dir: /usr/src/smartbrain_API
    ports:
      - "3000:3000"
    volumes: #listening to changes from ./ to container folder
      - ./:/usr/src/smartbrain_API

???????? In order to find the right version for compose: ➡Compose file versions and upgrading

???? Docker Volumes :

Docker Compose Commands

???? Build the container with compose

docker-compose build
  • ⚠⚠ Every time you change the yml file you need to run the docker-composer build to re-read that file.
  • Learn more about docker-compose build

???? Run docker-compose

docker-compose run smart-brain-api

Learn more about docker-compose run

???? To stop all the containers from running

docker-compose down 

???? Docker compose up

docker-compose up --build

➡ learn more about docker-compose up

docker-compose up -d

In order for it to run in the background

???? To execute the container from the shell

docker-compose exec smart-brain-api bash

PostgreSQL with Docker


???? Server:

const db = knex({
  client: 'pg',
  connection: process.env.POSTGRES_URI
});

???? Docker compose

version: '3.8'

services: 
  # Backend API
  smart-brain-api:
    container_name: backend
    # image: node:buster
    build: ./ #it use the image from dockerfile
    command: npm start
    working_dir: /usr/src/smartbrain_API
    environment: 
      POSTGRES_URI: postgres://sally@postgres:5432/smart-brain-docker
      #URI: unique resource identifier
    links: 
      - postgres
    ports:
      - "3000:3000"
    volumes: #listening to changes from ./ to container folder
      - ./:/usr/src/smartbrain_API
    
    # Postgres
    postgres:
      environment: 
        POSTGRES_USER: sally
        POSTGRES_PASSWORD: secret
        POSTGRES_DB: smart-brain-docker
        POSTGRES_HOST: postgres
      image: postgres
      ports: 
        - "5432:5432"

???? useful links

Leave a comment

Your email address will not be published. Required fields are marked *