Skip to main content

Docker Compose file vs Dockerfile - Differences - Explained

Docker Compose file vs Dockerfile - Differences - Explained
The Dockerfile is used to build a custom image and does not directly generate a container. It’s just that you can run the container while running the image.
The container orchestration to deploy the environment is done using the docker-compose.yml file, which may require a Dockerfile.

Dockerfile is used to build a mirror. If you want to use this mirror, you need to use the docker run command to run the mirror to generate and run a container.

If you have multiple containers and each container is dependent on another container then with docker-compose we can link.

Dockerfile

Write each layer of modification, installation, construction, and operation commands into a script. Use this script to build and customize the image. This script is the Dockerfile.

Dockerfile part of the instructions:

// FROM Specify the base image
FROM nginx

// Excuting an order. Each RUN will generate a layer, and use && as much as possible for a requirement, so as to reduce the RUN, that is, reduce the layering 

RUN echo '<h1>Hello, Docker!</h1>' > /usr/share/nginx/html/index.html
RUN yum update && yum install -y vim python-dev

// COPY: Copy package.json in the source path to the mirror path /usr/src/app on the new layer
COPY package.json /usr/src/app/

// WORKDIR Specify the working directory. Specify the lower-level working directory as /data in the container, try to use an absolute directory
WORKDIR /data

//ADD can automatically decompress files. The following example finally hello under /data/test
ADD hello test/ 

// Copy is similar to ADD, except that files cannot be decompressed
COPY hello test/

// CMD Excuting an order
CMD ["node", "index.js"]

// ENV Set environment variables and define NAME=Happy Feet, then you can use $NAME to execute

ENV VERSION=1.0 DEBUG=on NAME="Happy Feet" // VOLUMES mount


// EXPOSE Port exposure

EXPOSE <Port 1> [<Port 2>...]

Dockerfile example


// 1、创建 Dockerfile
mkdir mynginx
cd mynginx
vim Dockerfile

// 2、输入以下内容并保存:
FROM nginx
RUN echo '<h1>Hello, Docker!</h1>' > /usr/share/nginx/html/index.html

// 在 Dockerfile 目录下执行,生成新的自定义 images
docker build -t nginx:v3 .

Docker-compose

docker-compose is an official open source project, responsible for the rapid orchestration of Docker container clusters and the deployment of distributed applications. Define a group of related application containers as a project through a single docker-compose.yml template file (YAML format)

Install docker-compose

It has been installed by default under Mac and Window. However, it must be installed manually under Linux. The binary package is used here

sudo curl -L https://github.com/docker/compose/releases/download/1.26.0/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose

// Test docker-compose
$ docker-compose --version

General steps

1. Create an empty directory.
2. Define Dockerfile to facilitate migration to any place
3. Write docker-compose.yml file
4. Run docker-compose up to start the service

Examples of using docker-compose

Next, we use Node JS to build a website that can record the number of page visits.
1. Create an empty directory: mkdir -p /data/test
2. Create index.js under the empty file and enter the following. In createClient() you can apply a hostname that you have mentioned in docker-compose.yml

const express = require('express');
const app = express();
const redis  = require('redis');
app.get('/count',async function (req,res){
    const client = redis.createClient({host: 'redis-server'});
    return client.get('visit',(err, count)=>{
        count=Number(count)+2;
        return client.set(['visit',String(count)],function(err){
        return res.status(200).send(`Total Visit ${count}`);
        })
    });
})
app.get('/ping',(req,res)=>res.send("OK"));
app.listen(3001);
3. Write the Dockerfile file:

FROM node:14
ADD . /code
WORKDIR /code
RUN npm install express redis
COPY . ./code
CMD ["node", "index.js"]
4. Write the docker-compose.yml file

version: '3'
services: 
  redis-server:
    image: 'redis'
  web: 
    build: "."
    ports: 
       - "5001:3001"
    depends_on:
      - redis-server     
    links:
      - redis-server   
5. Execute the docker-compose project

docker-compose up

Description of yml template file:


version: '3'
services:
    phpfpm:
    image: yoogr/phpfpm:0.0.1
    container_name: ct-phpfpm
    build:
      context: .
      dockerfile: Dockerfile
    expose:
      - "9000"
    volumes:
      - ${DIR_WWW}:${DIR_WWW}:rw
      - ./conf/php/php.ini:/usr/local/etc/php/php.ini:ro
      - ./conf/php/php-fpm.d/www.conf:/usr/local/etc/php-fpm.d/www.conf:rw
      - ./conf/supervisor/conf.d:/etc/supervisor/conf.d/:ro
      - ./log/php-fpm/:/var/log/php-fpm/:rw
      - ./log/supervisor/:/var/log/supervisor/:rw
    command: supervisord -n
    links:
      - mysql:mysql
      - redis:redis
Each service represents a container. The container can be created through the image of dockerhub, or it can be created from the image built from the local Dockerfile. If a service needs to use the image built by Dockerfile, specify the text location and file name of the build-in docker-compose. The yml file can specify volume and network. 

The following is an example of using network and volumes parameters (placed in the same layer relationship with service):

version: '3.0'
services:

  wordpress:
    image: wordpress
    ports:
      - 8080:80
    environment:
      WORDPRESS_DB_HOST: db
      WORDPRESS_DB_PASSWORD: examplepass
    network:
      - my-bridge

  db:
    image: mysql:5.7
    environment:
      MYSQL_DATABASE: wordpress
      MYSQL_ROOT_PASSWORD: 123456
    volumes:
      - mysql-data:/var/lib/mysql
    networks:
      - my-bridge

volumes:
  my-data

networks:
  my-bridge:
    driver:bridge
The definition of network and volume is similar to docker network create and docker volume create. If you do not specify the connection network, then link will be used. 

Hope You like our “Docker Compose file vs Dockerfile” post. Please subscribe to our blog for getting updates on email for the upcoming blogs.

Comments

Popular posts from this blog

Netflix Premium Cookies 2024 - Netflix free Premium Cookies

As streaming services continue to dominate our entertainment landscape, Netflix still remains a top streaming service. But Netflix is not free. But what if I told you there’s a free way to access Netflix’s premium content without paying any money. 

Blogger Landing Page Template - Free Landing Page Templates

If you are looking for free Blogger (Blogspot) landing page templates, then you have come to the right place. There are a lot of available templates in WordPress with landing pages, but there is a scarcity of Blogger landing page templates.