Docker Compose file vs Dockerfile

Dockerfile vs Docker compose

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.

Docker Compose file vs 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.

The image used by docker-compose.yml can be on the network. This is the case. The locally existing image is preferred. If there is no local image, it will be downloaded from the Docker Hub website. If you want to use the private warehouse image directly, you need to download it in advance. The private warehouse mirrors can be pulled locally


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

//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 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`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)=>{
        return client.set(['visit',String(count)],function(err){
        return res.status(200).send(`Total Visit ${count}`);


3. Write the Dockerfile file:

FROM node:14
ADD . /code
RUN npm install express redis
COPY . ./code
CMD ["node", "index.js"]

4. Write the docker-compose.yml file

version: '3'
    image: 'redis'
    build: "."
       - "5001:3001"
      - redis-server     
      - redis-server  

5. Execute the docker-compose project

docker-compose up

Description of yml template file:

version: '3'
    image: yoogr/phpfpm:0.0.1
    container_name: ct-phpfpm
      context: .
      dockerfile: Dockerfile
      - "9000"
      - ${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
      - 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'


    image: wordpress
      - 8080:80
      WORDPRESS_DB_PASSWORD: examplepass
      - my-bridge

    image: mysql:5.7
      MYSQL_DATABASE: wordpress
      - mysql-data:/var/lib/mysql
      - my-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” blog. Please subscribe to our blog for getting updates on email for the upcoming blogs.

Happy Coding!!

Be the first to comment on "Docker Compose file vs Dockerfile"

Leave a comment

Your email address will not be published.