Given this directory structure:
.
├── backend
├── compose.yaml
└── frontend
└── Dockerfile.dev
2 directories, 2 files
My "compose.yaml" is like so:
services:
frontend_service:
container_name: frontend_container
restart: always
image: frontend_image
build:
context: .
dockerfile: ./frontend/Dockerfile.dev
working_dir: /home/frontend
volumes:
- ./frontend:/home/frontend
My "Dockerfile.dev" is like so:
FROM node:lts
But when I run docker compose up --build -d
The frontend_container
is constantly restarting and I cannot docker exec -it frontend_container bash
into it to verify. I get this error message Error response from daemon: Container 0136df5ba3c803dcded6695f65e51c38f4efe397859e77d001f862ced626d969 is restarting, wait until the container is running
When I docker ps --all
I get this:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0136df5ba3c8 frontend_image "docker-entrypoint.s…" 4 seconds ago Restarting (0) 1 second ago frontend_container
9dbc74fa0bbb postgres_image "docker-entrypoint.s…" 9 days ago Up 33 minutes 0.0.0.0:5432->5432/tcp postgres_container
f717459d3c53 pgadmin4_image "/entrypoint.sh" 9 days ago Up 33 minutes 443/tcp, 0.0.0.0:5050->80/tcp pgadmin4_container
You may be wondering why do I have such a weird compose file. But if you notice, I have a postgres container and a pgadmin container running. I tested this compose file structure with those two first before trying it out with node.
Given the directory structure in another project:
.
├── compose.yaml
├── pgadmin4.Dockerfile
└── postgres.Dockerfile
0 directories, 3 files
The "compose.yaml" is as follows:
services:
postgres_service:
container_name: postgres_container
image: postgres_image
build:
context: .
dockerfile: postgres.Dockerfile
restart: always
environment:
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
ports:
- '5432:5432'
volumes:
- postgres_volume:/var/lib/postgresql/data
networks:
- db_network
pgadmin4_service:
container_name: pgadmin4_container
build:
context: .
dockerfile: pgadmin4.Dockerfile
image: pgadmin4_image
restart: always
environment:
- [email protected]
- PGADMIN_DEFAULT_PASSWORD=root
- PGADMIN_LISTEN_PORT=80
ports:
- '5050:80'
volumes:
- pgadmin4_volume:/var/lib/pgadmin
networks:
- db_network
volumes:
pgadmin4_volume:
name: "pgadmin4_volume"
postgres_volume:
name: "postgres_volume"
networks:
db_network:
name: "db_network"
And the "pgadmin4.Dockerfile" is as follows:
FROM dpage/pgadmin4
And the "postgres.Dockerfile" is as follows:
FROM postgres:latest
I essentially copied the compose file from the postgres/pgadmin project to the NodeJS project and made a few tweaks but the fact that the NodeJS container just crashes when I use docker-compose is just weird. If I use docker run -d --name hello_node node:lts
, I can easily do docker exec -it hello_node bash
just fine and yarn --version
works just fine. Why can't I set up this workflow for NodeJS with docker-compose? Is it impossible? Any help would be appreciated.
CodePudding user response:
Your frontend_container
is not running any process with PID 1. Provide a command in your compose config for frontend_service
so that the container does not exit immediately after you run it. This process could be any service, for isntance a dev server. In this example i am running a command tail -f /dev/null
.
services:
frontend_service:
container_name: frontend_container
restart: always
image: frontend_image
build:
context: .
dockerfile: ./frontend/Dockerfile.dev
working_dir: /home/frontend
volumes:
- ./frontend:/home/frontend
tty: true
command: tail -f /dev/null
PS: After your container is running; login to the bash
or sh
of this container using docker exec
command. Now if you run$ top
command inside this conainter's shell your will find that tail
command is running with PID 1. In docker this process is a main process and when it exits the container exists. I hope it clarifies you to figure out what to run in the main
process inside docker container.