I am able to run a docker container using following docker command:
docker run -it ubuntu /bin/bash
Now I am trying to do it by using docker-compose:
version: "3"
services:
ubuntu:
container_name: ubuntu
image: ubuntu
restart: on-failure
command: "/bin/bash"
Now when I do :
docker-compose up -d
Can see docker container starting and exiting immediately.
I tried looking at the logs :
docker logs b8 //b8 is container id
But there are no error logs.
How do I keep ubuntu container running in background using docker. ( I am using docker on windows , linux version)
The simplest way to keep the container running is to pass a command that never ends. We can use never-ending commands in any of the following ways: ENTRYPOINT or CMD directive in the Dockerfile. Overriding ENTRYPOINT or CMD in the docker run command.
To run a docker container in the background or the detached mode from the terminal, you can use the docker run command followed by the -d flag (or detached flag) and followed by the name of the docker image you need to use in the terminal.
You can start Docker Compose in the background using the command docker-compose up -d . If using this method, you'll need to run docker-compose stop to shut it down.
The docker compose up command aggregates the output of each container (like docker compose logs --follow does). When the command exits, all containers are stopped. Running docker compose up --detach starts the containers in the background and leaves them running.
This is normal.
You are starting an ubuntu container with bash
as the command (thus the root process). The thing is to keep bash alive you need to attach it with a terminal. This is why when you want to get a bash in a container, you're using -ti
with your command :
docker container exec -ti [my_container_id] bash
So if you want to keep your ubuntu container alive and don't want to attach it to a terminal, you'll have to use a process that will stay alive for as long as you want.
Below is an example with sleep infinity
as your main process
version: "3"
services:
ubuntu:
container_name: ubuntu
image: ubuntu
restart: on-failure
command: ["sleep","infinity"]
With this example, you container will stay running indefinitely.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With