DEV Community

Cover image for Restart Policies Docker Compose
Rohith V
Rohith V

Posted on

28 3 1

Restart Policies Docker Compose

There are mainly 4 different restart policies.

"no"

It means never attempt to restart the container if it stops or crashes.
All we have to do is to add restart: 'no' in our docker-compose.yml file.

Remember to give single quotes 'no' because in yaml file, if we give no without quotes, it is treated as false.

Sample code example



version: '3'
services: 
  redis-server:
    image: 'redis'
  node-app:
    restart: 'no'
    build: .
    ports: 
      - "4001:8081"


Enter fullscreen mode Exit fullscreen mode

always

If our container stops for any reason, always attempt to restart the stopped container.
All we have to do is to add restart: always in docker-compose.yml file.

Sample code example



version: '3'
services: 
  redis-server:
    image: 'redis'
  node-app:
    restart: always
    build: .
    ports: 
      - "4001:8081"


Enter fullscreen mode Exit fullscreen mode

on-failure

Only restart the container, if it stops with an error code.

Error codes are non zero codes like 1, 2, 3, 4,....

Code 0 indicate there are no error and process is exited.

All we have to do is add restart: on-failure to our docker-compose.yml file.

Sample code example



version: '3'
services: 
  redis-server:
    image: 'redis'
  node-app:
    restart: on-failure
    build: .
    ports: 
      - "4001:8081"


Enter fullscreen mode Exit fullscreen mode

unless-stopped

Always restart unless the developers forcibly stop the process.

All we have to do is add restart: unless-stopped in docker-compose.yml file.

Sample code example



version: '3'
services: 
  redis-server:
    image: 'redis'
  node-app:
    restart: unless-stopped
    build: .
    ports: 
      - "4001:8081"


Enter fullscreen mode Exit fullscreen mode

Note : Always run docker-compose up --build after making any changes to the .yml file, or any project directory file.

Docker official Documentation

GitHub logo Rohithv07 / Docker

My Workplay on Docker and Kubernetes. Ref : https://github.com/Rohithv07/DockerCasts

Docker and Kubernetes

My Workplay on docker

Commands to remember :

  • docker run :- runs a command in a new container . docker run = docker create + docker start

  • docker run -p <localhostport>:<containerport> <imagename/id> :- running on ports

  • docker ps :- to list all the running containers

  • docker ps --all :- list all the container ever created

  • docker system prune :- to delete all the containers ever created along with some other properties

  • docker logs <container-id> :- to get the logs

  • docker start :- start stopped container

  • docker stop :- stop the container - gets a sigterm message - terminate signal

  • docker kill :- kills the container or stops the container instantly

  • docker exec -it <container id> <command> :- Execute an additional command in container. -it makes us to provide the input. -it equivalent to -i -t

  • docker exec -it <container id> sh :- provides access to the terminal…

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (2)

Collapse
 
tiagofrancafernandes profile image
Tiago França

Thank you. Helped me a lot

Collapse
 
yifeikong profile image
Yifei Kong

Note that the default value is no, you should probably change it to unless-stopped.

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post