I am new to Docker. I want to create an image in Docker which to run a microservice that needs RabbitMQ and Redis. For Redis, I include redis in the line RUN apt-get install -y python python-pip libsasl2-dev libmysqlclient-dev redis-server wget inside Dockerfile. To install RabbitMQ, I use
RUN apt-get update
RUN apt-get -y -q --allow-unauthenticated install rabbitmq-server
RUN /usr/sbin/rabbitmq-plugins enable rabbitmq_management
RUN echo "[{rabbit, [{loopback_users, []}]}]." >
/etc/rabbitmq/rabbitmq.config
# Port to expose
EXPOSE 9060 5672 15672
However, the rabbitmq server is not running in default which will prompt error when I run the built image. I have a line ENTRYPOINT ["/docker-entrypoint.sh"] to run my microservice. How can I run the rabbitMQ service from Dockerfile?
Related
OS Version/build: Windows 10 22H2
App version: Docker version 20.10.17, build 100c701
Steps to reproduce:
install Docker Desktop for Windows
a Hello World Ruby on Rails app in Dockerfile (copy&paste this from somewhere on the internet) , this issue happen with every helloworl docker app on my machine like django, nodejs etc…
FROM ruby:2.7
WORKDIR /app
RUN apt-get update; \
apt-get upgrade -y; \
apt-get install -y build-essential libssl-dev libreadline-dev libyaml-dev \
default-libmysqlclient-dev gnupg2 nodejs
RUN gem install rails --no-document; rails new .
ENV RAILS_ENV="development"
EXPOSE 3000
CMD ["bundle", "exec", "rails", "server"]
build and run it with port 3000 mapping
docker build - < Dockerfile -t demo01:1.0
docker run -p 3000:3000 demo01:1.0
try to access from http://localhost:3000 and failed
but when connecting to the container docker exec -it my-container-id-here /bin/sh and use curl localhost:3000 to check, I see the app is working well inside the container
What is this problem and how could I fix it?
I am building a Docker container from an Ubuntu image using Dockerfile. I would like the container to use the mosquitto broker.
In my dockerfile, I have this:
FROM ubuntu:latest
RUN apt-get -y update && apt-get -y upgrade
RUN apt-get install -y mosquitto
CMD ["mosquitto"]
I would expect the mosquitto service to start when I run the image as a container. I tried the -d option with the mosquitto CMD command in dockerfile. However, when I go to top or ps, I do not see mosquitto as a running process. When I, from within the container shell, type mosquitto -d, and subsequently go to top, I see mosquitto started . I also trid systemctl enable mosquitto followed by systemctl restart mosquitto.
Here is my Dockerfile currently:
FROM ubuntu:latest
RUN apt-get -y update && apt-get -y upgrade
RUN apt-get install -y mosquitto
CMD ["mosquitto", "-d"]
Which I then build the image from this and run it as a container.
I would expect the mosquitto broker to just start when the container does, but it does not. I am not sure why this is, based on the Docker documentation.
I have Dockerfile like:
FROM python:3.6.5-jessie
MAINTAINER twitter myname
RUN apt-get update
RUN apt-get install -y git
RUN apt-get install -y nginx
RUN pip install --upgrade pip
RUN git clone https://github.com/hongmingu/requirements
RUN pip install -r /requirements/requirements_django.txt
RUN apt-get install -y vim
RUN mkdir -p /uwsgi_log
RUN git clone https://github.com/hongmingu/smaple_django
RUN apt-get install -y nginx
COPY ./nginx.conf /etc/nginx/nginx.conf
COPY ./uwsgi.ini /uwsgi.ini # it runs in daemonized mode
# These files are just setting files. nginx get request at port 8000 and uwsgi runs django project.
RUN uwsgi --ini /uwsgi.ini
RUN service nginx restart
CMD ["python3"]
I think these 2 lines not work:
RUN uwsgi --ini /uwsgi.ini
RUN service nginx restart
Because When I build it and run it with linux command: sudo docker run --rm -it -p 8080:8000 hongmingu/smaple:0.1 /bin/bash my 127.0.0.1:8080 does not work. But, When I attach container and type command manually like, uwsgi --ini /uwsgi.ini and service nginx restart, It works well.
So, Is it impossible to run uwsgi, nginx in Dockerfile?
I want to do it so that I hope I don't need to run uwsgi and nginx manually.
Where did I make fault? Is there any good way to do this?
This docker image(hongmingu/smaple:0.1) is here: https://cloud.docker.com/u/hongmingu/repository/docker/hongmingu/smaple
You misunderstood the RUN instruction
The RUN instruction will execute any commands in a new layer on top of
the current image and commit the results
It's used to build your image, it is not docker run which executes the command in the container.
The solutions involves to execute those 2 lines in the CMD or ENTRYPOINT with a shell script. uwsgi has also to be daemonized. Checkout this image https://github.com/tiangolo/uwsgi-nginx-docker
I'm trying to create a Docker (LAMP) image with the following
Dockerfile:
FROM ubuntu:latest
RUN apt-get update \
&& DEBIAN_FRONTEND=noninteractive apt-get install -y \
apache2 \
mysql-server \
php7.0 \
php7.0-bcmath \
php7.0-mcrypt
COPY start-script.sh /root/
RUN chmod +x /root/start-script.sh && /root/start-script.sh
start-script.sh:
#!/bin/bash
service mysql start
a2enmod rewrite
service apache2 start
I build it with:
docker build -t resting/ubuntu .
Then run it with:
docker run -it -p 8000:80 -p 5000:3306 -v $(pwd)/html:/var/www/html resting/ubuntu bash
The problem is, the MYSQL and Apache2 service are not started.
If I run /root/start-script.sh manually in the container, port 80 maps fine to port 8000, but I couldn't connect to MYSQL with 127.0.0.1:5000.
How can I ensure that the services are running when I spin up a container with the image, and map MYSQL out to my host machine?
You need to change the execution of the script to a CMD instruction.
FROM ubuntu:latest
RUN apt-get update \
&& DEBIAN_FRONTEND=noninteractive apt-get install -y \
apache2 \
mysql-server \
php7.0 \
php7.0-bcmath \
php7.0-mcrypt
COPY start-script.sh /root/
RUN chmod +x /root/start-script.sh
CMD /root/start-script.sh
Althought this works, this is not the right way to manage containers. You should have one container for your Apache2 and another one for MySQL.
Take a look to this article that build a LAMP stack using Docker-Compose: https://www.kinamo.be/en/support/faq/setting-up-a-development-environment-with-docker-compose
you need multiple images - one for each service or app.
A Docker container is not a virtual machine in which you run an entire stack. It is a virtual application, running one primary process.
If you need php, apache and mysql, then you will need 3 docker containers. one for your php app, one for apache and one for mysql.
I had created the Docker container for RabbitMQ using the Following Docker File, But not able to run the Container, It's automatically stops.
Docker File
FROM centos:centos7
RUN yum -y update
RUN yum -y install wget
RUN wget https://www.rabbitmq.com/releases/erlang/erlang-18.2-1.el6.x86_64.rpm
RUN yum -y install erlang-18.2-1.el6.x86_64.rpm
RUN wget https://www.rabbitmq.com/releases/rabbitmq-server/v3.6.9/rabbitmq-server-3.6.9-1.el7.noarch.rpm
RUN yum -y install rabbitmq-server-3.6.9-1.el7.noarch.rpm
EXPOSE 15672
EXPOSE 5672
RUN rabbitmq-plugins enable rabbitmq_management
CMD /usr/sbin/rabbitmq-server -detached
Docker build Command
docker build -t rabbit .
Docker run command
docker run -d -p 15672:15672 rabbit
Container starts but's its exit's after a second
Thanks in advance