Running a ubuntu container in background using docker compose - docker

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)

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.

Related

Why container based on ubuntu didn't run from docker-compose file, when that work for similar nginx container? [duplicate]

This question already has answers here:
Ubuntu container keep restarting
(1 answer)
How can I run bash in a new container of a docker image?
(1 answer)
Docker compose detached mode not working
(2 answers)
Closed 1 year ago.
I try to run docker container using docker-compose file instead of a long command line.
I want to run docker-compose file based on ubuntu:latest. Container created but can't run.
version: "3.9"
services:
ubuntu:
image: ubuntu:latest
container_name: nginx_from_scratch3
ports:
- "80:80"
But before I've tried add in my docker-compose file line
command: bash
And noting change. I think what after running container continue to work. But that didn't happend.
But on the other side if I use nginx image all run perfectly.
version: "3.9"
services:
nginx1:
image: nginx
container_name: nginx_from_scratch4
ports:
- "80:80"
Why docker-compose file for nginx image work, and doesn’t work for ubuntu image.
Docker container exits if task inside is done. So when you run nginx, it starts nginx automatically and keep it alive. As for ubuntu, there's no any task to keep running and container ended immediately. So if you want to keep it alive even if it does not have any job: add tail -f, like this:
version: "3.9"
services:
ubuntu:
image: ubuntu
command: tail -F anything
After you do docker container ps you will see it running.
And you can move to it with
docker exec -it container_name bash

Install a wheel package inside a docker container

I am creating an airflow docker container using the docker image "puckel/docker-airflow".
I have created a docker-compose file that uses this image and links 2 volumes, one for dags and other for the wheel package.
When I start the container and go to airflow UI it throws "No module named 'custPkg'" error. So I exec into the container using the command
docker exec -ti <container_id> bash
and then installed it using pip. I can use the package if I run a python shell using the command
from custPkg.abc import Base
but it's still not working on the airflow.
The airflow webserver which even refreshes after some time is still showing the same error on the terminal on which I started the conatainer using
docker-compose up
my docker-compose file looks like this
version: "3"
services:
webserver:
image: puckel/docker-airflow:latest
container_name: test_container
volumes:
- /home/ubuntu/dags1/:/usr/local/airflow/dags
- /home/ubuntu/dist/:/usr/local/airflow/dist
ports:
- 8080:8080
restart: always
--------------------NEW UPDATE----------------------------
I just restarted the container and it is working now, but I don't want to go into the container and run the exec command manually. Can I somehow do this using the docker-compose file only?

How to make a docker-compose file from command lline

I run my container by five Docker commands as follows:
docker run --privileged -d -v /root/docker/data:/var/lib/mysql -p 8888:80 testimg:2 init
docker ps ---> to get container ID
docker exec -it container_id bash
docker exec container_id systemctl start mariadb
docker exec container_id systemctl start httpd
I was trying to do these steps by docker-compose but failed.
Can somebody make a docker-compose.yml or Dockerfile to get same result for me?
You're not going to be be able to do this with just a docker-compose.yml, because a compose file doesn't have any mechanism similar to docker exec. Additionally, running systemd (or really any process manager) inside a container is an anti-pattern. It can complicate the management and scaling of your containers, and in most cases doesn't provide you with any benefits.
Why don't you just have two images:
One that starts mariadb
One that starts Apache httpd
That might look something like:
version: "3"
services:
web:
image: httpd
ports:
- "8888:80"
db:
image: mariadb
volumes:
- "/root/docker/data:/var/lib/mysql"
You would probably need a custom image for the web server containing whatever application you're running, but you can definitely use the official mariadb image for your database.

Docker container always restarting

I try to start a Docker container from debian image, with a Docker compose file.
But when I do docker ps - a, the container is always restarting. I don't know why :s
Here my dockerfile :
FROM debian:jessie
ENV DEBIAN_FRONTEND noninteractive
RUN mkdir /home/server
RUN cd /home/server
VOLUME /home/server
CMD /bin/bash
Here my docker compose file :
version: '2'
services:
server:
build: .
restart: always
container_name: server
volumes:
- "/home/binaries:/home/server"
When docker-compose runs your "server" container, it will immediately terminate. A docker container needs at least one running process, otherwise, the container will exit. In your example, you are not starting a process that keeps alive.
As you have configured restart: always, docker-compose will endlessly restart new containers for "server". That should explain the behavior that you describe.
I have seen docker-compose files, where data containers were defined which only mounted images (in combination with volumes_from). They deliberately used /bin/true as a command, which also lead to permanent but harmless restarts. For example:
data:
restart: always
image: postgres:latest
volumes:
- /var/lib/postgresql
command: "true"
If restarts are not what you want, you could start a process in the container that does something useful, like running a web server or a database. But a bash alone is not something that will keep a container alive. A bash running in non-interactive mode will exit immediately.

container exit with code 0 while using docker compose file

I have a dockerfile to install httpd. When i run this dockerfile using the command
docker run -dit /bin/bash,
the container is started and it is running in the background. when i perform docker ps i could see the container running.
I have created a docker-compose.yml file as below,
version: '2'
services:
web:
build:
context: ./web
dockerfile: Dockerfile-apache
image: web:1.0
container_name: web
ports:
- "80:80"
command: service httpd start
i have build this compose file using the
docker-compose build.
Once after that i started the containers using
docker-compose up -d.
The containers are getting exited. i am not sure how to make the containers run at background.
Also i want to make the services running inside the container. For example i need to run the command like service httpd start inside the container and how to do it ?
This is because a Docker container only lives as long as its command runs.
Your command service httpd start will start httpd in the background and then exit. This will terminate httpd and the container.
You will have to run the httpd process directly and in the foreground, see the official image's start script:
httpd -DFOREGROUND
You can't run docker with -dit options together. -d means to run it in background mode and -ti means an interaction with terminal. So, have to run with -d OR with -ti and not both

Resources