Containers doesn't start after doing docker-compose up -d - docker

i'm having some problems using dockers.
First of all, i did a docker-compose.yml:
version: "3.9"
services:
web:
build: .
ports:
- 8000:80
volumes:
- $HOME/sitios:/var/www/html
db:
build: .
ports:
- 3000:3306
volumes:
- $HOME/"mariadb copia":/var/lib
As you can see here, i want to make a docker with two volumes, one with HTTP and other with mariadb server.
Here is my Dockerfile:
FROM ubuntu:latest
RUN apt-get update
RUN apt-get upgrade -y
RUN apt-get install nano mariadb-server apache2 -y
Then, i use the command sudo docker-compose up -d, however, the docker doesn't start at all, i try sudo docker start <name> but it doesn't work.
I already googled and i already looked into the official docker documentation but i can't find anything.
Thanks for your help.

Because you're using ubuntu:latest you're missing an entrypoint.
So it starts but immediately exits with exit 0.
Another thing: do not do apt upgrade inside the dockerfile, just take another Image (with a higher version or whatever you looking for).
One more thing: you use two different services, in the same image, it's pretty weird.
And one last thing: use the official Images, so you do not have to build them yourself.

Related

How can I manage a daemon service inside a Docker container

I'm running avahi-daemon inside of a Docker container. Currently I'm starting this by simply running it from the compose file. Is there a way to start it in a "managed" fashion, so it automatically restarts if it fails? Currently, due to the lack of an init process if it fails it becomes defunct and a replacement cannot be started.
It looks like you can just run it without a --daemonize option; then it will be a foreground process that can be the main container process. You can then use a Docker restart policy to restart the container if it fails.
A minimal Dockerfile could look like:
FROM ubuntu:20.04
RUN apt-get update \
&& DEBIAN_FRONTEND=noninteractive \
apt-get install --no-install-recommends --assume-yes \
avahi-daemon
CMD ["avahi-daemon", "--no-chroot"]
And the corresponding Compose setup:
version: '3.8'
services:
avahi-daemon:
build:
context: .
dockerfile: Dockerfile.avahi-daemon
restart: on-failure

Docker: How to use a Dockerfile in addition to docker-compose.yml

I am using this repo to set up a local wordpress development environment:
https://github.com/mjstealey/wordpress-nginx-docker#tldr
I hijacked the docker-compose to change the nginx port, but also to try to install openssl and vim on the nginx server. But when I do a docker-compose up the nginx server never starts properly.
This is what i see:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
57cf3dff059f nginx:latest "bash" About a minute ago Restarting (0) 14 seconds ago nginx
I tried to reference a Dockerfile inside the docker-compose like this:
nginx:
image: nginx:${NGINX_VERSION:-latest}
container_name: nginx
ports:
- '8085:8085'
- '443:443'
build: .
volumes:
- ${NGINX_CONF_DIR:-./nginx}:/etc/nginx/conf.d
- ${NGINX_LOG_DIR:-./logs/nginx}:/var/log/nginx
- ${WORDPRESS_DATA_DIR:-./wordpress}:/var/www/html
- ${SSL_CERTS_DIR:-./certs}:/etc/letsencrypt
- ${SSL_CERTS_DATA_DIR:-./certs-data}:/data/letsencrypt
depends_on:
- wordpress
restart: always
Notice the line that says "build: .".
Here's the contents of my Dockerfile:
FROM debian:buster-slim
RUN apt-get update
# installing vim isn't necessary. but just handy.
RUN apt-get -y install openssl
RUN apt-get -y install vim
Clearly, I'm doing something wrong. Maybe I should be defining tasks directly in the docker-compose for the nginx server?
I wanted to find a way to make a clean separation between our customizations and the original code. But maybe this isn't possible.
Thanks
EDIT 1
This is what the Dockerfile looks like:
FROM nginx:latest
RUN apt-get update
&& apt-get -y install openssl
&& apt-get -y install vim
And the nginx section of the docker-compose.yml:
nginx:
#image: nginx:${NGINX_VERSION:-latest}
container_name: nginx
ports:
- '8085:8085'
- '443:443'
build: .
volumes:
- ${NGINX_CONF_DIR:-./nginx}:/etc/nginx/conf.d
- ${NGINX_LOG_DIR:-./logs/nginx}:/var/log/nginx
- ${WORDPRESS_DATA_DIR:-./wordpress}:/var/www/html
- ${SSL_CERTS_DIR:-./certs}:/etc/letsencrypt
- ${SSL_CERTS_DATA_DIR:-./certs-data}:/data/letsencrypt
depends_on:
- wordpress
restart: always
I think you maybe need to change the base image that you are using in the docker file?:
FROM nginx:latest
Then in the docker file, because you are creating your own customised version of nginx image, you should either give custom name or tag.

Docker-compose build misses some package content in container

I'm working on build containers exploiting a monitoring application (Centreon).
When i build my container manually (with docker run) and when i build my docker file, i have different results. Somes scripts used by the application are missing.
Here's my dockerfile :
FROM centos:centos7
LABEL Author = "AurelienH."
LABEL Description = "DOCKERFILE : Creates a Docker Container for a Centreon poller"
#Update and install requirements
RUN yum update -y
RUN yum install -y wget nano httpd git
#Install Centreon repo
RUN yum install -y --nogpgcheck http://yum.centreon.com/standard/3.4/el7/stable/noarch/RPMS/centreon-release-3.4-4.el7.centos.noarch.rpm
#Install Centreon
RUN yum install -y centreon-base-config-centreon-engine centreon centreon-pp-manager centreon-clapi
RUN yum install -y centreon-widget*
RUN yum clean all
#PHP Time Zone
RUN echo -n "date.timezone = Europe/Paris" > /etc/php.d/php-timezone.ini
#Supervisor
RUN yum install -y python-setuptools
RUN easy_install supervisor
COPY /cfg/supervisord.conf /etc/
RUN yum clean all
EXPOSE 22 80 5667 5669
CMD ["/usr/bin/supervisord", "--configuration=/etc/supervisord.conf"]
The difference i see is in the /usr/lib/nagios/plugins folder. I miss some scripts here. And when i execute the exact same commands but in a container i'm running i can find my files.
Maybe it has something to do with writing permissions for the user that executes the commands with docker-compose ?
EDIT :
docker-compose file :
version: "3"
services:
centreon:
build: ./centreon
depends_on:
- mariadb
container_name: sinelis-central
volumes:
- ./central-broker-config:/etc/centreon-broker
- ./central-centreon-plugins:/usr/lib/centreon/plugins
- ./central-engine-config:/etc/centreon-engine
- ./central-logs-broker:/var/log/centreon-broker
- ./central-logs-engine:/var/log/centreon-engine
- ./central-nagios-plugins:/usr/lib/nagios/plugins
- ./central-ssh-key:/home/centreon/.ssh/
ports:
- "80:80"
- "5667:5667"
- "5669:5669"
- "22:22"
deploy:
restart_policy:
window: 300s
links:
- mariadb
mariadb:
image: mariadb
container_name: sinelis-mariadb
environment:
MYSQL_ROOT_PASSWORD: passwd2017
deploy:
restart_policy:
window: 300s
To run the container I use the docker run -it centos:centos7 command
It doesn't matter what you put in your image at that location, you will always see the contents of your volume mount:
- ./central-nagios-plugins:/usr/lib/nagios/plugins
Docker does not initialize host volumes to the contents of the image, and once you have data in the volume, docker does an initialization with any volume type.
Keep in mind the build happens on an image without any of the other configurations in the compose file applied, no volumes are mounted for you to update. Then when you run your container, you overlay the directories of the image with the volumes you select. Build time and run time are two separate phases.
Edit: to have a named volume point to a host directory, you can defined a bind mount volume. This will not create the directory if it does not already exist (the attempt to mount the volume will fail and the container would not start). But if the directory is empty, it will be initialized to the contents of your image:
version: "3"
volumes:
central-nagios-plugins:
driver: local
driver_opts:
type: none
o: bind
device: /usr/lib/nagios/plugins
services:
centreon:
....
volumes:
...
- central-nagios-plugins:/usr/lib/nagios/plugins
...
It will be up to you to empty the contents of this volume when you want it to be reinitialized with the contents of your image, and merging multiple versions of this directory would also be a process you'd need to create yourself.

How to install packages from Docker compose?

Hi there I am new to Docker. I have an docker-compose.yml which looks like this:
version: "3"
services:
lmm-website:
image: lmm/lamp:php${PHP_VERSION:-71}
container_name: ${CONTAINER_NAME:-lmm-website}
environment:
HOME: /home/user
command: supervisord -n
volumes:
- ..:/builds/lmm/website
- db_website:/var/lib/mysql
ports:
- 8765:80
- 12121:443
- 3309:3306
networks:
- ntw
volumes:
db_website:
networks:
ntw:
I want to install the Yarn package manager from within the docker-compose file:
sudo apt-get update && sudo apt-get install yarn
I could not figure out how to declare this, I have tried
command: supervisord -n && sudo apt-get update && sudo apt-get install yarn
which fails silently. How do I declare this correctly? Or is docker-compose.yml the wrong place for this?
Why not use Dockerfile which is specifically designed for this task?
Change your "image" property to "build" property to link a Dockerfile.
Your docker-compose.yml would look like this:
services:
lmm-website:
build:
context: .
dockerfile: Dockerfile
container_name: ${CONTAINER_NAME:-lmm-website}
environment:
HOME: /home/user
command: supervisord -n
volumes:
- ..:/builds/lmm/website
- db_website:/var/lib/mysql
ports:
- 8765:80
- 12121:443
- 3309:3306
networks:
- ntw
volumes:
db_website:
networks:
Then create a text file named Dockerfile in the same path as docker-compose.yml with the following content:
FROM lmm/lamp:php${PHP_VERSION:-71}
RUN apt-get update && apt-get install -y bash
You can add as much SO commands as you want using Dockerfile's RUN (cp, mv, ls, bash...), apart from other Dockerfile capabilities like ADD, COPY, etc.
+info:
https://docs.docker.com/engine/reference/builder/
+live-example:
I made a github project called hello-docker-react. As it's name says is a docker-react box, and can serve you as an example as I am installing yarn plus other tools using the procedure I explained above.
In addition to that, I also start yarn using an entrypoint bash script linked to docker-compose.yml file using docker-compose entrypoint property.
https://github.com/lopezator/hello-docker-react
You can only do it with a Dockerfile, because the command operator in docker-compose.yml only keeps the container alive during the time the command is executed, and then it stops.
Try this
command: supervisord -n && apt-get update && apt-get install yarn
Because sudo doesn't work in docker.
My first time trying to help out:
would like you to give it a try (I found it on the internet)
FROM lmm/lamp:php${PHP_VERSION:-71}
USER root
RUN apt-get update && apt-get install -y bash

Docker-compose linking service into dockerfile

I am pretty new to docker, and I am trying to make a container with multiple apps.
Let say that my docker-compose file is like this :
version: '2'
services:
myapp:
build: ./dockerfiles/myapp
volumes:
- ./www:/var/www
- ./logs:/var/log
- ./mysql-data:/var/lib/mysql
- ./php:/etc/php5
- ./nginx:/etc/nginx
ports:
- "8082:8000"
- "6606:3306"
links:
- mysql:mysql
- php:php
- nginx:nginx
mysql:
image: mysql
environment:
MYSQL_ROOT_PASSWORD: M#yW3Bw35t
MYSQL_USER: replymwp
MYSQL_PASSWORD: ZSzLPoOi9wlhFaiJ
php:
image: php:5.6-fpm
links:
- mysql:db
nginx:
image: nginx
links:
- php:php
Now, in myapp DockerFile, I want to install a package that needs mysql.
FROM debian:jessie
RUN apt-get update
RUN apt-get update
RUN apt-get install -y apt-show-versions
RUN apt-get install -y wget
RUN wget http://repo.ajenti.org/debian/key -O- | apt-key add -
RUN echo "deb http://repo.ajenti.org/ng/debian main main ubuntu" >> /etc/apt/sources.list
RUN apt-get update
RUN apt-get install -y ajenti
RUN apt-get install -y ajenti-v ajenti-v-ftp-vsftpd ajenti-v-php-fpm ajenti-v-mysql
EXPOSE 8000
ENTRYPOINT ["ajenti-panel"]
Now the problem is, when docker try to build my image, it install php, mysql etc... even if I link it in my docker-compose file. And secondly, when it try to install mysql, It prompt for a master password and stay blocked at this step, even if I fill something...
Maybe am I totally wrong in my way of using it?
Any help would be appreciate.
I suppose your ajenti has a dependency on mysql, so if you do apt-get install ajenti, it tries to satisfy that dependency. Specifically you are installing ajenti-v-mysql, which does seem to have a mysql dependency
Because you want to run mysql seperate, you might need to do --no-install-recommends ? This is a flag voor apt-get, so you'd get something like
apt-get install <packagename> --no-install-recommends
This would mean you get NO dependencies, so you might need to figure out which other depenencies you need.
The php-fpm has the same issue, I suppose that whole line which includes ajenti-v-php-fpm is a bit too much?
If you're planning on using separate mysql and php containers, then why are you still including the installation in the mpapp dockerfile on this line:
RUN apt-get install -y ajenti-v ajenti-v-ftp-vsftpd ajenti-v-php-fpm ajenti-v-mysql
If you're going to use mysql and php containers then you don't need them in your app. This should also take care of your second problem about being prompted for mysql password.
Keep in mind that you will need to change the hostnames of mysql and your php configuration from your myapp configuration. I think you might be better off looking for a tutorial for setting up docker compose, you'll have to look yourself to find the most suitable but something like this would give you a good start.

Resources